示例#1
0
  @RequestMapping(value = PREFIX_API_1_COMMENTS + "destroy", method = RequestMethod.POST)
  @ResponseBody
  public Object destroy(@RequestParam(value = "id") String id) {
    Comment comment = commentService.getComment(id);
    if (comment == null) {
      return ErrorDto.badRequest(String.format("评论不存在(id=%s)", id));
    }

    boolean permission = false;
    // 评论作者有权限删除
    if (comment.getUserId().equals(accountService.getCurrentUserId())) {
      permission = true;
    }

    // 帖子作者有权限删除
    if (!permission) {
      Post post = statusService.getPost(comment.getPostId());
      if (accountService.getCurrentUserId().equals(post.getUserId())) {
        permission = true;
      }
    }

    if (permission) {
      commentService.deleteComment(comment);
      return commentService.toBasicCommentDto(comment);
    } else {
      return ErrorDto.forbidden();
    }
  }
示例#2
0
  private Object createComment(
      String text,
      String replyCommentId,
      String postId,
      FileInfo videoFileInfo,
      FileInfo imageFileInfo,
      FileInfo audioFileInfo) {

    Comment comment = new Comment();
    comment.setUserId(accountService.getCurrentUserId());
    comment.setText(text);
    // 回复Comment
    if (StringUtils.isNotBlank(replyCommentId)) {
      Comment replyComment = commentService.getComment(replyCommentId);
      if (replyComment == null) {
        return ErrorDto.badRequest(String.format("replyCommentId[%s]无效", replyCommentId));
      }
      comment.setReplyCommentId(replyCommentId);
      comment.setReplyCommentUserId(replyComment.getUserId());
      comment.setPostId(replyComment.getPostId());
    }
    // 回复Post
    else {
      Post post = statusService.getPost(postId);
      if (post == null) {
        return ErrorDto.badRequest(String.format("postId[%s]不存在", postId));
      }

      if (!post.isPublic()) {
        return ErrorDto.badRequest("不能评论非所有人可见的视频");
      }

      // 回复原始贴
      if (post.getOriginPostId() == null) {
        comment.setPostId(postId);
      }
      // 回复转发贴,归结到原始贴上
      else {
        comment.setPostId(post.getOriginPostId());
      }
    }

    comment = commentService.create(comment, videoFileInfo, imageFileInfo, audioFileInfo);
    return commentService.toCommentDto(comment);
  }
示例#3
0
  // TODO 优化代码,视频评论和文字评论大部分重复
  @RequestMapping(value = PREFIX_API_1_COMMENTS + "create", method = RequestMethod.POST)
  @ResponseBody
  public Object create(
      @RequestParam(value = "text") String text,
      @RequestParam(value = "postId", required = false) String postId,
      @RequestParam(value = "replyCommentId", required = false) String replyCommentId) {

    try {
      logger.debug("正在发布文字评论:");
      logger.debug("            text : {}", text);
      logger.debug("          postId : {}", postId);
      logger.debug("  replyCommentId : {}", replyCommentId);

      if (StringUtils.isBlank(postId) && StringUtils.isBlank(replyCommentId)) {
        return ErrorDto.badRequest("参数postId和replyCommentId不能都为空");
      }

      Comment comment = new Comment();

      // 回复Comment
      if (StringUtils.isNotBlank(replyCommentId)) {
        Comment replyComment = commentService.getComment(replyCommentId);
        if (replyComment == null) {
          return ErrorDto.badRequest(String.format("replyCommentId[%s]无效", replyCommentId));
        }
        comment.setReplyCommentId(replyCommentId);
        comment.setReplyCommentUserId(replyComment.getUserId());
        comment.setPostId(replyComment.getPostId());
        // TODO 存被回复者screenName以提高性能
      }
      // 回复Post
      else {
        Post post = statusService.getPost(postId);

        if (post == null) {
          return ErrorDto.badRequest(String.format("postId[%s]不存在", postId));
        }

        if (!post.isPublic()) {
          return ErrorDto.badRequest("不能评论非所有人可见的视频");
        }

        if (post.getOriginPostId() == null) {
          comment.setPostId(postId);
        } else {
          comment.setPostId(post.getOriginPostId());
        }
      }

      comment.setUserId(accountService.getCurrentUserId());
      comment.setText(text);

      comment = commentService.create(comment, null, null, null);
      return commentService.toCommentDto(comment);

    } catch (DuplicateException e) {
      return ErrorDto.duplicate();
    } catch (Exception e) {
      logger.info("发布评论失败", e);
      return ErrorDto.internalServerError(e.getMessage());
    }
  }