如何阻止自己的通知

huangapple go评论59阅读模式
英文:

How to block your own notifications

问题

CommentController.java

@Controller
public class CommentController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private NotificationMapper notificationMapper;

    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public Object post(@RequestBody CommentCreateDto commentCreateDto,
                       HttpServletRequest request) {
        // ...
        // Comment insertion into the database

        if (commentCreateDto.getType() == 2) {
            // Notification for replying to a comment
            Notification notification = new Notification();
            notification.setNotifier(comment.getCommentor());

            Comment comment2 = commentMapper.getparentbyid(commentCreateDto.getParent_id());
            notification.setReceiver(comment2.getCommentor());
            notification.setOuterid(commentCreateDto.getParent_id());
            notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
            notification.setCreatetime(System.currentTimeMillis());
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            notificationMapper.inserts(notification);

            // Update comment count
            commentMapper.updatecommentcount(commentCreateDto.getParent_id());

        } else {
            // Notification for replying to a question
            Question question = questionMapper.getbyId(commentCreateDto.getParent_id());
            Notification notification = new Notification();
            notification.setNotifier(user.getId());
            notification.setReceiver(question.getCreateid());
            notification.setOuterid(commentCreateDto.getParent_id());
            notification.setType(notificationEnum.NOTIFICATION_QUESTION.getType());
            notification.setCreatetime(System.currentTimeMillis());
            notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
            notificationMapper.inserts(notification);

            // Update question comment count
            questionMapper.updatecomment(commentCreateDto.getParent_id());
        }
        ResultDto resultDto = new ResultDto();
        return resultDto.success();
    }

    @ResponseBody
    @RequestMapping(value = "/comment/{id}", method = RequestMethod.GET)
    public ResultDto<List<CommentDto>> comments(@PathVariable(name = "id") int id,
                                                HttpServletRequest request) {
        // ...
        // Retrieving and preparing comments for display
        ResultDto resultDto = new ResultDto();
        return resultDto.success(commentDto);
    }
}

NotificationController.java

@Controller
public class NotificationController {

    @Resource
    private NotificationMapper notificationMapper;
    @Resource
    private CommentMapper commentMapper;

    @GetMapping("/notification/{action}")
    public String notification(@PathVariable("action") int id,
                               HttpServletRequest request) {
        // ...
        // Updating notification status and determining the type
        return "redirect:/question/" + questionid;
    }
}

QuestionController.java

@Controller
public class QuestionController {

    @Resource
    private QuestionService questionService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentService commentService;
    @Resource
    private NotificationMapper notificationMapper;

    @GetMapping("/question/{id}")
    public String question(@PathVariable(name = "id") int id,
                           Model model,
                           HttpServletRequest request) {
        // ...
        // Fetching user information and related question data
        return "question";
    }
}

(Note: This translation might not exactly match your original code's formatting due to limitations in the text-based format. If you need further assistance, please feel free to ask.)

英文:

I am trying to learn how to build a forum from an available source. However the problem I have is the function to reply to comments and answer questions. When I reply to my question or comment, notify me that "vkhacbao answered vkhacbao's question" (vkhacbao is the account I log into). How can I block or set the condition so that when I comment, it's not sent to myself. Please help me, I tried everything for 3 days but with no success. Thanks very much
Code:

CommentController.java

@Controller
public class CommentController {
@Resource
private UserMapper userMapper;
@Resource
private CommentMapper commentMapper;
@Resource
private QuestionMapper questionMapper;
@Resource
private NotificationMapper notificationMapper;
@ResponseBody
@RequestMapping(value = &quot;/comment&quot;,method = RequestMethod.POST)
public Object post(@RequestBody CommentCreateDto commentCreateDto,
HttpServletRequest request){
//把User写进session
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return &quot;login&quot;;
}
User user = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(&quot;token&quot;)) {
String token = cookie.getValue();
user = userMapper.findBytoken(token);
if (user != null) {
request.getSession().setAttribute(&quot;user&quot;, user);
//获取未读的消息数量
int unreadnum=notificationMapper.getunreadcount(user.getId());
request.getSession().setAttribute(&quot;unreadnum&quot;,unreadnum);
}
break;
}
}
//把评论插入数据库
Comment comment=new Comment();
comment.setParent_id(commentCreateDto.getParent_id());
comment.setContent(commentCreateDto.getContent());
comment.setType(commentCreateDto.getType());
comment.setCreatetime(System.currentTimeMillis());
comment.setCommentor(user.getId());
commentMapper.insert(comment);
if (commentCreateDto.getType()==2){
//把回复评论的通知插入数据库
Notification notification=new Notification();
notification.setNotifier(comment.getCommentor());
Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
notification.setReceiver(comment2.getCommentor());
notification.setOuterid(commentCreateDto.getParent_id());
notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
notification.setCreatetime(System.currentTimeMillis());
notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
notificationMapper.inserts(notification);
//增加评论数
commentMapper.updatecommentcount(commentCreateDto.getParent_id());
}
else {
//把回复问题的通知插入数据库
Question question=questionMapper.getbyId(commentCreateDto.getParent_id());
Notification notification=new Notification();
notification.setNotifier(user.getId());
notification.setReceiver(question.getCreateid());
notification.setOuterid(commentCreateDto.getParent_id());
notification.setType(notificationEnum.NOTIFICATION_QUESTION.getType());
notification.setCreatetime(System.currentTimeMillis());
notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
notificationMapper.inserts(notification);
//增加问题回复量
questionMapper.updatecomment(commentCreateDto.getParent_id());
}
ResultDto resultDto=new ResultDto();
return resultDto.success();
}
@ResponseBody
@RequestMapping(value = &quot;/comment/{id}&quot;,method = RequestMethod.GET)
public ResultDto&lt;List&lt;CommentDto&gt;&gt; comments(@PathVariable(name = &quot;id&quot;) int id,
HttpServletRequest request){
//查找type=2,即是回复评论的评论
List&lt;Comment&gt; comments = commentMapper.getCommentByid(id,2);
List&lt;CommentDto&gt; commentDto=new ArrayList&lt;&gt;();
//找到User
Cookie[] cookies = request.getCookies();
User user = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(&quot;token&quot;)) {
String token = cookie.getValue();
user = userMapper.findBytoken(token);
break;
}
}
//把二级评论和对应的User写进每个CommentDto集合中
for (Comment comment:comments){
CommentDto dto=new CommentDto();
BeanUtils.copyProperties(comment,dto);
dto.setUser(user);
commentDto.add(dto);
}
ResultDto resultDto=new ResultDto();
//返回数据给前端
return resultDto.success(commentDto);
}
}

NotificationController.java

@Controller
public class NotificationController {
@Resource
private NotificationMapper notificationMapper;
@Resource
private CommentMapper commentMapper;
@GetMapping(&quot;/notification/{action}&quot;)
public String notification(@PathVariable(&quot;action&quot;)int id,
HttpServletRequest request){
//将通知设置为已读
notificationMapper.updatestatus(id);
//获取type,检验是回复评论还是回复问题
int type=notificationMapper.gettypebyid(id);
int outerid=notificationMapper.getouteridbyid(id);
int questionid;
if(type== notificationEnum.NOTIFICATION_QUESTION.getType()){
questionid=outerid;
}else {
questionid=commentMapper.getparentidbyid(id);
}
return &quot;redirect:/question/&quot;+questionid;
}
}

QuestionController.java

@Controller
public class QuestionController {
@Resource
private QuestionService questionService;
@Resource
private UserMapper userMapper;
@Resource
private CommentService commentService;
@Resource
private NotificationMapper notificationMapper;
@GetMapping(&quot;/question/{id}&quot;)
public String question(@PathVariable(name = &quot;id&quot;)int id,
Model model,
HttpServletRequest request){
//查找cookies,观察是否有token存在
Cookie[] cookies = request.getCookies();
if (cookies == null) {
return &quot;login&quot;;
}
User user = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(&quot;token&quot;)) {
String token = cookie.getValue();
user = userMapper.findBytoken(token);
if (user != null) {
request.getSession().setAttribute(&quot;user&quot;, user);
//获取未读的消息数量
int unreadnum=notificationMapper.getunreadcount(user.getId());
request.getSession().setAttribute(&quot;unreadnum&quot;,unreadnum);
}
break;
}
}
Questiondto questiondto=questionService.getbyid(id);
//增加阅读数
questionService.increaseview(id);
model.addAttribute(&quot;questionDto&quot;,questiondto);
//展示回复数据
List&lt;CommentDto&gt; comments=commentService.getByid(id);
model.addAttribute(&quot;comments&quot;,comments);
//相关问题
String[] tags=questiondto.getTag().split(&quot;,&quot;);
StringBuilder msg=new StringBuilder();
for (String tag:tags){
msg.append(tag);
msg.append(&quot;|&quot;);
}
String result=msg.substring(0,msg.length()-1);
List&lt;Question&gt; relativequestion =questionService.getbytag(id,result);
model.addAttribute(&quot;relativequestion&quot;,relativequestion);
return &quot;question&quot;;
}
}

答案1

得分: 1

假设原始消息的所有者和评论者都在CommentController类中,代码部分如下:

if (commentCreateDto.getType()==2){
    Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
       
    if ( !comment2.getCommentor().equals(comment.getCommentor()) ) {
        Notification notification=new Notification();
        notification.setNotifier(comment.getCommentor());
        notification.setReceiver(comment2.getCommentor());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);
        commentMapper.updatecommentcount(commentCreateDto.getParent_id());
    }
}

不幸的是,如果这对您不起作用,您需要更多地了解该语言和代码,以确定在何处进行更改。

英文:

Assuming the owners of the original message and the commenter are in the CommentController class, it would be this section

if (commentCreateDto.getType()==2){
    Comment comment2=commentMapper.getparentbyid(commentCreateDto.getParent_id());
       
    if ( !comment2.getCommentor().equals(comment.getCommentor()) ) {
        Notification notification=new Notification();
        notification.setNotifier(comment.getCommentor());
        notification.setReceiver(comment2.getCommentor());
        notification.setOuterid(commentCreateDto.getParent_id());
        notification.setType(notificationEnum.NOTIFICATION_COMMENT.getType());
        notification.setCreatetime(System.currentTimeMillis());
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notificationMapper.inserts(notification);
        commentMapper.updatecommentcount(commentCreateDto.getParent_id());
    }
}

Unfortunately, if that doesn't work for you, you'll need to learn more about the language and the code to determine where to make the change.

huangapple
  • 本文由 发表于 2020年9月2日 12:14:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定