Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added discussion report api #10

Open
wants to merge 1 commit into
base: 4.8.20-dev-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ public ResponseEntity<ApiResponse> downVote(@PathVariable String discussionId,
ApiResponse response = discussionService.downVote(discussionId, token);
return new ResponseEntity<>(response, response.getResponseCode());
}

@PostMapping("/report")
public ResponseEntity<ApiResponse> report(@RequestBody Map<String, Object> reportData,
@RequestHeader(Constants.X_AUTH_TOKEN) String token) {
ApiResponse response = discussionService.report(token, reportData);
return new ResponseEntity<>(response, response.getResponseCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface DiscussionService {
ApiResponse upVote(String discussionId, String token);

ApiResponse downVote(String discussionId, String token);

ApiResponse report(String token, Map<String, Object> reportData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public ApiResponse createDiscussion(JsonNode discussionDetails, String token) {
((ObjectNode) discussionDetails).put(Constants.UP_VOTE_COUNT,0L);
((ObjectNode) discussionDetails).put(Constants.DOWN_VOTE_COUNT,0L);
((ObjectNode) discussionDetails).put(Constants.MEDIA,discussionDetails.get(Constants.MEDIA));
((ObjectNode) discussionDetails).put(Constants.STATUS,Constants.ACTIVE);

DiscussionEntity jsonNodeEntity = new DiscussionEntity();
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
Expand Down Expand Up @@ -663,4 +664,95 @@ public ApiResponse upVote(String discussionId, String token) {
public ApiResponse downVote(String discussionId, String token) {
return vote(discussionId, token, Constants.DOWN);
}

@Override
public ApiResponse report(String token, Map<String, Object> reportData) {
log.info("DiscussionService::report: Reporting discussion");
ApiResponse response = ProjectUtil.createDefaultResponse("discussion.report");
String errorMsg = validateReportPayload(reportData);
if (StringUtils.isNotEmpty(errorMsg)) {
return returnErrorMsg(errorMsg, HttpStatus.BAD_REQUEST, response, Constants.FAILED);
}

String userId = accessTokenValidator.verifyUserToken(token);
if (StringUtils.isBlank(userId) || Constants.UNAUTHORIZED.equals(userId)) {
return returnErrorMsg(Constants.INVALID_AUTH_TOKEN, HttpStatus.UNAUTHORIZED, response, Constants.FAILED);
}

try {
String discussionId = (String) reportData.get(Constants.DISCUSSION_ID);
Optional<DiscussionEntity> discussionDbData = discussionRepository.findById(discussionId);
if (!discussionDbData.isPresent()) {
return returnErrorMsg(Constants.DISCUSSION_NOT_FOUND, HttpStatus.NOT_FOUND, response, Constants.FAILED);
}

DiscussionEntity discussionEntity = discussionDbData.get();
if (!discussionEntity.getIsActive()) {
return returnErrorMsg(Constants.DISCUSSION_IS_INACTIVE, HttpStatus.CONFLICT, response, Constants.FAILED);
}

JsonNode data = discussionEntity.getData();
String currentStatus = data.has(Constants.STATUS) ? data.get(Constants.STATUS).asText() : null;

if (Constants.SUSPENDED.equals(currentStatus)) {
return returnErrorMsg(Constants.DISCUSSION_SUSPENDED, HttpStatus.ALREADY_REPORTED, response, Constants.FAILED);
}

((ObjectNode) data).put(Constants.STATUS, Constants.SUSPENDED);
ArrayNode reportedByNode = data.has(Constants.REPORTED_BY) ? (ArrayNode) data.get(Constants.REPORTED_BY) : objectMapper.createArrayNode();
reportedByNode.add(userId);
((ObjectNode) data).put(Constants.REPORTED_REASON, objectMapper.valueToTree(reportData.get(Constants.REPORTED_REASON)));
((ObjectNode) data).put(Constants.REPORTED_BY, reportedByNode);

discussionEntity.setData(data);
discussionRepository.save(discussionEntity);
log.info("DiscussionService::report: Discussion entity updated successfully");

ObjectNode jsonNode = objectMapper.createObjectNode();
jsonNode.setAll((ObjectNode) discussionEntity.getData());
Map<String, Object> map = objectMapper.convertValue(jsonNode, Map.class);
esUtilService.addDocument(cbServerProperties.getDiscussionEntity(), Constants.INDEX_TYPE, discussionId, map, cbServerProperties.getElasticDiscussionJsonPath());
cacheService.putCache(Constants.DISCUSSION_CACHE_PREFIX + discussionId, jsonNode);
return response;
} catch (Exception e) {
log.error("DiscussionService::report: Failed to report discussion", e);
return returnErrorMsg(Constants.DISCUSSION_REPORT_FAILED, HttpStatus.INTERNAL_SERVER_ERROR, response, Constants.FAILED);
}
}

private String validateReportPayload(Map<String, Object> reportData) {
StringBuffer errorMsg = new StringBuffer();
List<String> errList = new ArrayList<>();

if (reportData.containsKey(Constants.DISCUSSION_ID) && StringUtils.isBlank((String) reportData.get(Constants.DISCUSSION_ID))){
errList.add(Constants.DISCUSSION_ID);
}
if (reportData.containsKey(Constants.REPORTED_REASON)) {
Object reportedReasonObj = reportData.get(Constants.REPORTED_REASON);
if (reportedReasonObj instanceof List) {
List<String> reportedReasonList = (List<String>) reportedReasonObj;
if (reportedReasonList.isEmpty()) {
errList.add(Constants.REPORTED_REASON);
} else if (reportedReasonList.contains("Others")) {
if (!reportData.containsKey(Constants.OTHER_REASON) ||
StringUtils.isBlank((String) reportData.get(Constants.OTHER_REASON))) {
errList.add(Constants.OTHER_REASON);
}
}
} else {
errList.add(Constants.REPORTED_REASON);
}
}
if (!errList.isEmpty()) {
errorMsg.append("Failed Due To Missing Params - ").append(errList).append(".");
}
return errorMsg.toString();
}

private ApiResponse returnErrorMsg(String error, HttpStatus type, ApiResponse response, String status) {
response.setResponseCode(type);
response.getParams().setErr(error);
response.setMessage(status);
return response;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/igot/cb/pores/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public class Constants {
public static final String UP_VOTE_COUNT = "upVoteCount";
public static final String DOWN_VOTE_COUNT = "downVoteCount";
public static final String DISCUSSION_VOTE_API = "discussion.vote";
public static final String STATUS = "status";
public static final String REPORTED_BY = "reportedBy";
public static final String SUSPENDED = "suspended";
public static final String REPORTED_REASON = "reportedDueTo";
public static final String OTHER_REASON = "otherReasons";
public static final String ACTIVE = "active";
public static final String DISCUSSION_SUSPENDED = "discussion Already suspended";
public static final String DISCUSSION_REPORT_FAILED = "Failed to report discussion";
private Constants() {
}
}
3 changes: 3 additions & 0 deletions src/main/resources/EsFieldsmapping/discussionEsMapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@
},
"parentDiscussionId": {
"type": "keyword"
},
"status": {
"type": "keyword"
}
}