Search in sources :

Example 1 with CommentsVo

use of top.hcode.hoj.pojo.vo.CommentsVo in project HOJ by HimitZH.

the class CommentController method addComment.

@PostMapping("/comment")
@RequiresPermissions("comment_add")
@RequiresAuthentication
@Transactional
public CommonResult addComment(@RequestBody Comment comment, HttpServletRequest request) {
    if (StringUtils.isEmpty(comment.getContent().trim())) {
        return CommonResult.errorResponse("评论内容不能为空!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 比赛外的评论 除管理员外 只有AC 10道以上才可评论
    if (comment.getCid() == null) {
        if (!SecurityUtils.getSubject().hasRole("root") && !SecurityUtils.getSubject().hasRole("admin") && !SecurityUtils.getSubject().hasRole("problem_admin")) {
            QueryWrapper<UserAcproblem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", userRolesVo.getUid()).select("distinct pid");
            int userAcProblemCount = userAcproblemService.count(queryWrapper);
            if (userAcProblemCount < 10) {
                return CommonResult.errorResponse("对不起,您暂时不能评论!请先去提交题目通过10道以上~", CommonResult.STATUS_FORBIDDEN);
            }
        }
    }
    comment.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
    if (SecurityUtils.getSubject().hasRole("root")) {
        comment.setFromRole("root");
    } else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        comment.setFromRole("admin");
    } else {
        comment.setFromRole("user");
    }
    // 带有表情的字符串转换为编码
    comment.setContent(EmojiUtil.toHtml(comment.getContent()));
    boolean isOk = commentService.saveOrUpdate(comment);
    if (isOk) {
        CommentsVo commentsVo = new CommentsVo();
        commentsVo.setContent(comment.getContent());
        commentsVo.setId(comment.getId());
        commentsVo.setFromAvatar(comment.getFromAvatar());
        commentsVo.setFromName(comment.getFromName());
        commentsVo.setFromUid(comment.getFromUid());
        commentsVo.setLikeNum(0);
        commentsVo.setGmtCreate(comment.getGmtCreate());
        commentsVo.setReplyList(new LinkedList<>());
        // 如果是讨论区的回复,发布成功需要添加统计该讨论的回复数
        if (comment.getDid() != null) {
            Discussion discussion = discussionService.getById(comment.getDid());
            if (discussion != null) {
                discussion.setCommentNum(discussion.getCommentNum() + 1);
                discussionService.updateById(discussion);
                // 更新消息
                commentService.updateCommentMsg(discussion.getUid(), userRolesVo.getUid(), comment.getContent(), comment.getDid());
            }
        }
        return CommonResult.successResponse(commentsVo, "评论成功");
    } else {
        return CommonResult.errorResponse("评论失败,请重新尝试!");
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CommentsVo

use of top.hcode.hoj.pojo.vo.CommentsVo in project HOJ by HimitZH.

the class CommentServiceImpl method getCommentList.

@Override
public IPage<CommentsVo> getCommentList(int limit, int currentPage, Long cid, Integer did, Boolean isRoot, String uid) {
    // 新建分页
    Page<CommentsVo> page = new Page<>(currentPage, limit);
    if (cid != null) {
        Contest contest = contestService.getById(cid);
        boolean onlyMineAndAdmin = contest.getStatus().equals(Constants.Contest.STATUS_RUNNING.getCode()) && !isRoot && !contest.getUid().equals(uid);
        if (onlyMineAndAdmin) {
            // 自己和比赛管理者评论可看
            List<UserInfo> superAdminList = contestRecordService.getSuperAdminList();
            List<String> myAndAdminUidList = superAdminList.stream().map(UserInfo::getUuid).collect(Collectors.toList());
            myAndAdminUidList.add(uid);
            myAndAdminUidList.add(contest.getUid());
            return commentMapper.getCommentList(page, cid, did, true, myAndAdminUidList);
        }
    }
    return commentMapper.getCommentList(page, cid, did, false, null);
}
Also used : CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) Contest(top.hcode.hoj.pojo.entity.contest.Contest)

Example 3 with CommentsVo

use of top.hcode.hoj.pojo.vo.CommentsVo in project HOJ by HimitZH.

the class CommentController method getComments.

@GetMapping("/comments")
public CommonResult getComments(@RequestParam(value = "cid", required = false) Long cid, @RequestParam(value = "did", required = false) Integer did, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage, HttpServletRequest request) {
    // 如果有登录,则获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    IPage<CommentsVo> commentList = commentService.getCommentList(limit, currentPage, cid, did, isRoot, userRolesVo != null ? userRolesVo.getUid() : null);
    HashMap<Integer, Boolean> commentLikeMap = new HashMap<>();
    if (userRolesVo != null) {
        // 如果是有登录 需要检查是否对评论有点赞
        List<Integer> commentIdList = new LinkedList<>();
        for (CommentsVo commentsVo : commentList.getRecords()) {
            commentIdList.add(commentsVo.getId());
        }
        if (commentIdList.size() > 0) {
            QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
            commentLikeQueryWrapper.in("cid", commentIdList);
            List<CommentLike> commentLikeList = commentLikeService.list(commentLikeQueryWrapper);
            // 如果存在记录需要修正Map为true
            for (CommentLike tmp : commentLikeList) {
                commentLikeMap.put(tmp.getCid(), true);
            }
        }
    }
    return CommonResult.successResponse(MapUtil.builder().put("commentList", commentList).put("commentLikeMap", commentLikeMap).map(), "获取成功");
}
Also used : HashMap(java.util.HashMap) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) LinkedList(java.util.LinkedList) CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo)

Aggregations

CommentsVo (top.hcode.hoj.pojo.vo.CommentsVo)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 HttpSession (javax.servlet.http.HttpSession)2 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 Transactional (org.springframework.transaction.annotation.Transactional)1 Contest (top.hcode.hoj.pojo.entity.contest.Contest)1 CommentLike (top.hcode.hoj.pojo.entity.discussion.CommentLike)1 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)1 UserAcproblem (top.hcode.hoj.pojo.entity.user.UserAcproblem)1 UserInfo (top.hcode.hoj.pojo.entity.user.UserInfo)1