Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(comment): Notification of comments based on different priorities

LinkinStarb56d7cdad043
diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.go
index cb45c7d1..18b755ab 100644
--- a/internal/service/comment/comment_service.go
+++ b/internal/service/comment/comment_service.go
@@ -121,34 +121,14 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
return nil, err
}
- if objInfo.ObjectType == constant.QuestionObjectType {
- cs.notificationQuestionComment(ctx, objInfo.ObjectCreatorUserID,
- objInfo.QuestionID, objInfo.Title, comment.ID, req.UserID, comment.OriginalText)
- } else if objInfo.ObjectType == constant.AnswerObjectType {
- cs.notificationAnswerComment(ctx, objInfo.QuestionID, objInfo.Title, objInfo.AnswerID,
- objInfo.ObjectCreatorUserID, comment.ID, req.UserID, comment.OriginalText)
- }
- if len(req.MentionUsernameList) > 0 {
- cs.notificationMention(ctx, req.MentionUsernameList, comment.ID, req.UserID)
- }
-
resp = &schema.GetCommentResp{}
resp.SetFromComment(comment)
resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
time.Now(), req.CanEdit, req.CanDelete)
- // get reply user info
- if len(resp.ReplyUserID) > 0 && resp.ReplyUserID != req.UserID {
- replyUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.ReplyUserID)
- if err != nil {
- return nil, err
- }
- if exist {
- resp.ReplyUsername = replyUser.Username
- resp.ReplyUserDisplayName = replyUser.DisplayName
- resp.ReplyUserStatus = replyUser.Status
- }
- cs.notificationCommentReply(ctx, replyUser.ID, comment.ID, req.UserID)
+ commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
+ if err != nil {
+ return commentResp, err
}
// get user info
@@ -179,6 +159,50 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
return resp, nil
}
+func (cs *CommentService) addCommentNotification(
+ ctx context.Context, req *schema.AddCommentReq, resp *schema.GetCommentResp,
+ comment *entity.Comment, objInfo *schema.SimpleObjectInfo) (*schema.GetCommentResp, error) {
+ // The priority of the notification
+ // 1. reply to user
+ // 2. comment mention to user
+ // 3. answer or question was commented
+ alreadyNotifiedUserID := make(map[string]bool)
+
+ // get reply user info
+ if len(resp.ReplyUserID) > 0 && resp.ReplyUserID != req.UserID {
+ replyUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.ReplyUserID)
+ if err != nil {
+ return nil, err
+ }
+ if exist {
+ resp.ReplyUsername = replyUser.Username
+ resp.ReplyUserDisplayName = replyUser.DisplayName
+ resp.ReplyUserStatus = replyUser.Status
+ }
+ cs.notificationCommentReply(ctx, replyUser.ID, comment.ID, req.UserID)
+ alreadyNotifiedUserID[replyUser.ID] = true
+ return nil, nil
+ }
+
+ if len(req.MentionUsernameList) > 0 {
+ alreadyNotifiedUserIDs := cs.notificationMention(
+ ctx, req.MentionUsernameList, comment.ID, req.UserID, alreadyNotifiedUserID)
+ for _, userID := range alreadyNotifiedUserIDs {
+ alreadyNotifiedUserID[userID] = true
+ }
+ return nil, nil
+ }
+
+ if objInfo.ObjectType == constant.QuestionObjectType && !alreadyNotifiedUserID[objInfo.ObjectCreatorUserID] {
+ cs.notificationQuestionComment(ctx, objInfo.ObjectCreatorUserID,
+ objInfo.QuestionID, objInfo.Title, comment.ID, req.UserID, comment.OriginalText)
+ } else if objInfo.ObjectType == constant.AnswerObjectType && !alreadyNotifiedUserID[objInfo.ObjectCreatorUserID] {
+ cs.notificationAnswerComment(ctx, objInfo.QuestionID, objInfo.Title, objInfo.AnswerID,
+ objInfo.ObjectCreatorUserID, comment.ID, req.UserID, comment.OriginalText)
+ }
+ return nil, nil
+}
+
// RemoveComment delete comment
func (cs *CommentService) RemoveComment(ctx context.Context, req *schema.RemoveCommentReq) (err error) {
return cs.commentRepo.RemoveComment(ctx, req.CommentID)
@@ -536,14 +560,16 @@ func (cs *CommentService) notificationCommentReply(ctx context.Context, replyUse
notice_queue.AddNotification(msg)
}
-func (cs *CommentService) notificationMention(ctx context.Context, mentionUsernameList []string, commentID, commentUserID string) {
+func (cs *CommentService) notificationMention(
+ ctx context.Context, mentionUsernameList []string, commentID, commentUserID string,
+ alreadyNotifiedUserID map[string]bool) (alreadyNotifiedUserIDs []string) {
for _, username := range mentionUsernameList {
userInfo, exist, err := cs.userCommon.GetUserBasicInfoByUserName(ctx, username)
if err != nil {
log.Error(err)
continue
}
- if exist {
+ if exist && !alreadyNotifiedUserID[userInfo.ID] {
msg := &schema.NotificationMsg{
ReceiverUserID: userInfo.ID,
TriggerUserID: commentUserID,
@@ -553,6 +579,8 @@ func (cs *CommentService) notificationMention(ctx context.Context, mentionUserna
msg.ObjectType = constant.CommentObjectType
msg.NotificationAction = constant.MentionYou
notice_queue.AddNotification(msg)
+ alreadyNotifiedUserIDs = append(alreadyNotifiedUserIDs, userInfo.ID)
}
}
+ return alreadyNotifiedUserIDs
}