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
LinkinStar483c51bab84a
diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.goindex 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.AddCommentreturn 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.AddCommentreturn 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 commentfunc (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, replyUsenotice_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, mentionUsernamsg.ObjectType = constant.CommentObjectTypemsg.NotificationAction = constant.MentionYounotice_queue.AddNotification(msg)+ alreadyNotifiedUserIDs = append(alreadyNotifiedUserIDs, userInfo.ID)}}+ return alreadyNotifiedUserIDs}