use of top.hcode.hoj.pojo.entity.discussion.Reply in project HOJ by HimitZH.
the class UserMessageManager method getUserReplyMsgList.
private IPage<UserMsgVo> getUserReplyMsgList(IPage<UserMsgVo> userMsgList) {
for (UserMsgVo userMsgVo : userMsgList.getRecords()) {
if ("Discussion".equals(userMsgVo.getSourceType())) {
Discussion discussion = discussionEntityService.getById(userMsgVo.getSourceId());
if (discussion != null) {
userMsgVo.setSourceTitle(discussion.getTitle());
} else {
userMsgVo.setSourceTitle("原讨论帖已被删除!【The original discussion post has been deleted!】");
}
} else if ("Contest".equals(userMsgVo.getSourceType())) {
Contest contest = contestEntityService.getById(userMsgVo.getSourceId());
if (contest != null) {
userMsgVo.setSourceTitle(contest.getTitle());
} else {
userMsgVo.setSourceTitle("原比赛已被删除!【The original contest has been deleted!】");
}
}
if ("Comment".equals(userMsgVo.getQuoteType())) {
Comment comment = commentEntityService.getById(userMsgVo.getQuoteId());
if (comment != null) {
String content;
if (comment.getContent().length() < 100) {
content = comment.getFromName() + " : " + comment.getContent();
} else {
content = comment.getFromName() + " : " + comment.getContent().substring(0, 100) + "...";
}
userMsgVo.setQuoteContent(content);
} else {
userMsgVo.setQuoteContent("您的原评论信息已被删除!【Your original comments have been deleted!】");
}
} else if ("Reply".equals(userMsgVo.getQuoteType())) {
Reply reply = replyEntityService.getById(userMsgVo.getQuoteId());
if (reply != null) {
String content;
if (reply.getContent().length() < 100) {
content = reply.getFromName() + " : @" + reply.getToName() + ":" + reply.getContent();
} else {
content = reply.getFromName() + " : @" + reply.getToName() + ":" + reply.getContent().substring(0, 100) + "...";
}
userMsgVo.setQuoteContent(content);
} else {
userMsgVo.setQuoteContent("您的原回复信息已被删除!【Your original reply has been deleted!】");
}
}
}
applicationContext.getBean(UserMessageManager.class).updateUserMsgRead(userMsgList);
return userMsgList;
}
use of top.hcode.hoj.pojo.entity.discussion.Reply in project HOJ by HimitZH.
the class CommentManager method deleteReply.
public void deleteReply(ReplyDto replyDto) throws StatusForbiddenException, StatusFailException, AccessException {
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
Reply reply = replyDto.getReply();
Comment comment = commentEntityService.getById(reply.getCommentId());
Long cid = comment.getCid();
if (cid == null) {
Discussion discussion = discussionEntityService.getById(comment.getDid());
Long gid = discussion.getGid();
if (gid == null) {
accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !isProblemAdmin && !isAdmin) {
throw new StatusForbiddenException("无权删除该回复");
}
} else {
accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !groupValidator.isGroupAdmin(userRolesVo.getUid(), gid)) {
throw new StatusForbiddenException("无权删除该回复");
}
}
} else {
accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
Contest contest = contestEntityService.getById(cid);
if (!reply.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
throw new StatusForbiddenException("无权删除该回复");
}
}
boolean isOk = replyEntityService.removeById(reply.getId());
if (isOk) {
// 如果是讨论区的回复,删除成功需要减少统计该讨论的回复数
if (replyDto.getDid() != null) {
UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num-1");
discussionEntityService.update(discussionUpdateWrapper);
}
} else {
throw new StatusFailException("删除失败,请重新尝试");
}
}
use of top.hcode.hoj.pojo.entity.discussion.Reply in project HOJ by HimitZH.
the class CommentManager method addReply.
public ReplyVo addReply(ReplyDto replyDto) throws StatusFailException, StatusForbiddenException, AccessException {
if (StringUtils.isEmpty(replyDto.getReply().getContent().trim())) {
throw new StatusFailException("回复内容不能为空!");
}
// 获取当前登录的用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
Reply reply = replyDto.getReply();
Comment comment = commentEntityService.getById(reply.getCommentId());
Long cid = comment.getCid();
if (cid == null) {
QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
discussionQueryWrapper.select("id", "gid").eq("id", comment.getDid());
Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
Long gid = discussion.getGid();
if (gid != null) {
accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
if (!groupValidator.isGroupMember(userRolesVo.getUid(), gid) && !isRoot) {
throw new StatusForbiddenException("对不起,您无权限回复!");
}
} else {
accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
}
if (!isRoot && !isProblemAdmin && !isAdmin) {
QueryWrapper<UserAcproblem> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("uid", userRolesVo.getUid()).select("distinct pid");
int userAcProblemCount = userAcproblemEntityService.count(queryWrapper);
if (userAcProblemCount < configVo.getDefaultCreateCommentACInitValue()) {
throw new StatusForbiddenException("对不起,您暂时不能回复!请先去提交题目通过" + configVo.getDefaultCreateCommentACInitValue() + "道以上!");
}
}
} else {
accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
Contest contest = contestEntityService.getById(cid);
Long gid = contest.getGid();
if (!comment.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), gid))) {
throw new StatusForbiddenException("对不起,您无权限回复!");
}
}
reply.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
if (SecurityUtils.getSubject().hasRole("root")) {
reply.setFromRole("root");
} else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
reply.setFromRole("admin");
} else {
reply.setFromRole("user");
}
// 带有表情的字符串转换为编码
reply.setContent(EmojiUtil.toHtml(reply.getContent()));
boolean isOk = replyEntityService.saveOrUpdate(reply);
if (isOk) {
// 如果是讨论区的回复,发布成功需要增加统计该讨论的回复数
if (replyDto.getDid() != null) {
UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num+1");
discussionEntityService.update(discussionUpdateWrapper);
// 更新消息
replyEntityService.updateReplyMsg(replyDto.getDid(), "Discussion", reply.getContent(), replyDto.getQuoteId(), replyDto.getQuoteType(), reply.getToUid(), reply.getFromUid());
}
ReplyVo replyVo = new ReplyVo();
BeanUtil.copyProperties(reply, replyVo);
replyVo.setFromTitleName(userRolesVo.getTitleName());
replyVo.setFromTitleColor(userRolesVo.getTitleColor());
return replyVo;
} else {
throw new StatusFailException("回复失败,请重新尝试!");
}
}
Aggregations