Search in sources :

Example 1 with ArticleComment

use of org.ambraproject.wombat.model.ArticleComment in project wombat by PLOS.

the class ArticleController method receiveNewComment.

/**
 * @param parentArticleDoi null if a reply to another comment
 * @param parentCommentUri null if a direct reply to an article
 */
@RequestMapping(name = "postComment", method = RequestMethod.POST, value = "/article/comments/new")
@ResponseBody
public Object receiveNewComment(HttpServletRequest request, @SiteParam Site site, @RequestParam("commentTitle") String commentTitle, @RequestParam("comment") String commentBody, @RequestParam("isCompetingInterest") boolean hasCompetingInterest, @RequestParam(value = "authorEmailAddress", required = false) String authorEmailAddress, @RequestParam(value = "authorName", required = false) String authorName, @RequestParam(value = "authorPhone", required = false) String authorPhone, @RequestParam(value = "authorAffiliation", required = false) String authorAffiliation, @RequestParam(value = "ciStatement", required = false) String ciStatement, @RequestParam(value = "target", required = false) String parentArticleDoi, @RequestParam(value = "inReplyTo", required = false) String parentCommentUri) throws IOException {
    if (honeypotService.checkHoneypot(request, authorPhone, authorAffiliation)) {
        return ImmutableMap.of("status", "success");
    }
    checkCommentsAreEnabled();
    Map<String, Object> validationErrors = commentValidationService.validateComment(site, commentTitle, commentBody, hasCompetingInterest, ciStatement);
    if (!validationErrors.isEmpty()) {
        return ImmutableMap.of("validationErrors", validationErrors);
    }
    if (parentArticleDoi == null) {
        Map<String, Object> comment = getComment(parentCommentUri);
        parentArticleDoi = getParentArticleDoiFromComment(comment);
    }
    ApiAddress address = ApiAddress.builder("articles").embedDoi(parentArticleDoi).addToken("comments").build();
    String authId = request.getRemoteUser();
    final String creatorUserId = authId == null ? null : userApi.getUserIdFromAuthId(authId);
    ArticleComment comment = new ArticleComment(parentArticleDoi, creatorUserId, parentCommentUri, commentTitle, commentBody, ciStatement, authorEmailAddress, authorName);
    HttpResponse response = articleApi.postObject(address, comment);
    String responseJson = EntityUtils.toString(response.getEntity());
    Map<String, Object> commentJson = gson.fromJson(responseJson, HashMap.class);
    return ImmutableMap.of("createdCommentUri", commentJson.get("commentUri"));
}
Also used : ArticleComment(org.ambraproject.wombat.model.ArticleComment) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(org.apache.http.HttpResponse) ApiAddress(org.ambraproject.wombat.service.remote.ApiAddress) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ArticleComment (org.ambraproject.wombat.model.ArticleComment)1 ApiAddress (org.ambraproject.wombat.service.remote.ApiAddress)1 HttpResponse (org.apache.http.HttpResponse)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1