diff --git a/internal/base/constant/notification.go b/internal/base/constant/notification.go index 3d29a40b..8f8568a8 100644 --- a/internal/base/constant/notification.go +++ b/internal/base/constant/notification.go @@ -1,38 +1,38 @@ package constant const ( - // UpdateQuestion update question - UpdateQuestion = "notification.action.update_question" - // AnswerTheQuestion answer the question - AnswerTheQuestion = "notification.action.answer_the_question" - // UpVotedTheQuestion up voted the question - UpVotedTheQuestion = "notification.action.up_voted_question" - // DownVotedTheQuestion down voted the question - DownVotedTheQuestion = "notification.action.down_voted_question" - // UpdateAnswer update answer - UpdateAnswer = "notification.action.update_answer" - // AcceptAnswer accept answer - AcceptAnswer = "notification.action.accept_answer" - // UpVotedTheAnswer up voted the answer - UpVotedTheAnswer = "notification.action.up_voted_answer" - // DownVotedTheAnswer down voted the answer - DownVotedTheAnswer = "notification.action.down_voted_answer" - // CommentQuestion comment question - CommentQuestion = "notification.action.comment_question" - // CommentAnswer comment answer - CommentAnswer = "notification.action.comment_answer" - // UpVotedTheComment up voted the comment - UpVotedTheComment = "notification.action.up_voted_comment" - // ReplyToYou reply to you - ReplyToYou = "notification.action.reply_to_you" - // MentionYou mention you - MentionYou = "notification.action.mention_you" - // YourQuestionIsClosed your question is closed - YourQuestionIsClosed = "notification.action.your_question_is_closed" - // YourQuestionWasDeleted your question was deleted - YourQuestionWasDeleted = "notification.action.your_question_was_deleted" - // YourAnswerWasDeleted your answer was deleted - YourAnswerWasDeleted = "notification.action.your_answer_was_deleted" - // YourCommentWasDeleted your comment was deleted - YourCommentWasDeleted = "notification.action.your_comment_was_deleted" + // NotificationUpdateQuestion update question + NotificationUpdateQuestion = "notification.action.update_question" + // NotificationAnswerTheQuestion answer the question + NotificationAnswerTheQuestion = "notification.action.answer_the_question" + // NotificationUpVotedTheQuestion up voted the question + NotificationUpVotedTheQuestion = "notification.action.up_voted_question" + // NotificationDownVotedTheQuestion down voted the question + NotificationDownVotedTheQuestion = "notification.action.down_voted_question" + // NotificationUpdateAnswer update answer + NotificationUpdateAnswer = "notification.action.update_answer" + // NotificationAcceptAnswer accept answer + NotificationAcceptAnswer = "notification.action.accept_answer" + // NotificationUpVotedTheAnswer up voted the answer + NotificationUpVotedTheAnswer = "notification.action.up_voted_answer" + // NotificationDownVotedTheAnswer down voted the answer + NotificationDownVotedTheAnswer = "notification.action.down_voted_answer" + // NotificationCommentQuestion comment question + NotificationCommentQuestion = "notification.action.comment_question" + // NotificationCommentAnswer comment answer + NotificationCommentAnswer = "notification.action.comment_answer" + // NotificationUpVotedTheComment up voted the comment + NotificationUpVotedTheComment = "notification.action.up_voted_comment" + // NotificationReplyToYou reply to you + NotificationReplyToYou = "notification.action.reply_to_you" + // NotificationMentionYou mention you + NotificationMentionYou = "notification.action.mention_you" + // NotificationYourQuestionIsClosed your question is closed + NotificationYourQuestionIsClosed = "notification.action.your_question_is_closed" + // NotificationYourQuestionWasDeleted your question was deleted + NotificationYourQuestionWasDeleted = "notification.action.your_question_was_deleted" + // NotificationYourAnswerWasDeleted your answer was deleted + NotificationYourAnswerWasDeleted = "notification.action.your_answer_was_deleted" + // NotificationYourCommentWasDeleted your comment was deleted + NotificationYourCommentWasDeleted = "notification.action.your_comment_was_deleted" ) diff --git a/internal/repo/activity/answer_repo.go b/internal/repo/activity/answer_repo.go index 15271da8..02e9c511 100644 --- a/internal/repo/activity/answer_repo.go +++ b/internal/repo/activity/answer_repo.go @@ -216,7 +216,7 @@ func (ar *AnswerActivityRepo) AcceptAnswer(ctx context.Context, if act.UserID != questionUserID { msg.TriggerUserID = questionUserID msg.ObjectType = constant.AnswerObjectType - msg.NotificationAction = constant.AcceptAnswer + msg.NotificationAction = constant.NotificationAcceptAnswer notice_queue.AddNotification(msg) } } diff --git a/internal/repo/activity/vote_repo.go b/internal/repo/activity/vote_repo.go index 724a128e..1303f3c7 100644 --- a/internal/repo/activity/vote_repo.go +++ b/internal/repo/activity/vote_repo.go @@ -467,21 +467,21 @@ func (vr *VoteRepo) sendVoteInboxNotification(triggerUserID, receiverUserID, obj } if objectType == constant.QuestionObjectType { if upvote { - msg.NotificationAction = constant.UpVotedTheQuestion + msg.NotificationAction = constant.NotificationUpVotedTheQuestion } else { - msg.NotificationAction = constant.DownVotedTheQuestion + msg.NotificationAction = constant.NotificationDownVotedTheQuestion } } if objectType == constant.AnswerObjectType { if upvote { - msg.NotificationAction = constant.UpVotedTheAnswer + msg.NotificationAction = constant.NotificationUpVotedTheAnswer } else { - msg.NotificationAction = constant.DownVotedTheAnswer + msg.NotificationAction = constant.NotificationDownVotedTheAnswer } } if objectType == constant.CommentObjectType { if upvote { - msg.NotificationAction = constant.UpVotedTheComment + msg.NotificationAction = constant.NotificationUpVotedTheComment } } if len(msg.NotificationAction) > 0 { diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go index 39433b81..2fa5c261 100644 --- a/internal/service/answer_service.go +++ b/internal/service/answer_service.go @@ -476,7 +476,7 @@ func (as *AnswerService) AdminSetAnswerStatus(ctx context.Context, req *schema.A msg.ReceiverUserID = answerInfo.UserID msg.TriggerUserID = answerInfo.UserID msg.ObjectType = constant.AnswerObjectType - msg.NotificationAction = constant.YourAnswerWasDeleted + msg.NotificationAction = constant.NotificationYourAnswerWasDeleted notice_queue.AddNotification(msg) return nil @@ -566,7 +566,7 @@ func (as *AnswerService) notificationUpdateAnswer(ctx context.Context, questionU ObjectID: answerID, } msg.ObjectType = constant.AnswerObjectType - msg.NotificationAction = constant.UpdateAnswer + msg.NotificationAction = constant.NotificationUpdateAnswer notice_queue.AddNotification(msg) } @@ -583,7 +583,7 @@ func (as *AnswerService) notificationAnswerTheQuestion(ctx context.Context, ObjectID: answerID, } msg.ObjectType = constant.AnswerObjectType - msg.NotificationAction = constant.AnswerTheQuestion + msg.NotificationAction = constant.NotificationAnswerTheQuestion notice_queue.AddNotification(msg) userInfo, exist, err := as.userRepo.GetByUserID(ctx, questionUserID) diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.go index 1b65c48d..654ee9f3 100644 --- a/internal/service/comment/comment_service.go +++ b/internal/service/comment/comment_service.go @@ -471,7 +471,7 @@ func (cs *CommentService) notificationQuestionComment(ctx context.Context, quest ObjectID: commentID, } msg.ObjectType = constant.CommentObjectType - msg.NotificationAction = constant.CommentQuestion + msg.NotificationAction = constant.NotificationCommentQuestion notice_queue.AddNotification(msg) receiverUserInfo, exist, err := cs.userRepo.GetByUserID(ctx, questionUserID) @@ -526,7 +526,7 @@ func (cs *CommentService) notificationAnswerComment(ctx context.Context, ObjectID: commentID, } msg.ObjectType = constant.CommentObjectType - msg.NotificationAction = constant.CommentAnswer + msg.NotificationAction = constant.NotificationCommentAnswer notice_queue.AddNotification(msg) receiverUserInfo, exist, err := cs.userRepo.GetByUserID(ctx, answerUserID) @@ -578,7 +578,7 @@ func (cs *CommentService) notificationCommentReply(ctx context.Context, replyUse ObjectID: commentID, } msg.ObjectType = constant.CommentObjectType - msg.NotificationAction = constant.ReplyToYou + msg.NotificationAction = constant.NotificationReplyToYou notice_queue.AddNotification(msg) } @@ -599,7 +599,7 @@ func (cs *CommentService) notificationMention( ObjectID: commentID, } msg.ObjectType = constant.CommentObjectType - msg.NotificationAction = constant.MentionYou + msg.NotificationAction = constant.NotificationMentionYou notice_queue.AddNotification(msg) alreadyNotifiedUserIDs = append(alreadyNotifiedUserIDs, userInfo.ID) } diff --git a/internal/service/notification/notification_service.go b/internal/service/notification/notification_service.go index 554ea132..3a8cce13 100644 --- a/internal/service/notification/notification_service.go +++ b/internal/service/notification/notification_service.go @@ -5,10 +5,12 @@ import ( "encoding/json" "fmt" + "github.com/answerdev/answer/internal/base/constant" "github.com/answerdev/answer/internal/base/data" "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/pager" "github.com/answerdev/answer/internal/base/translator" + "github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/schema" notficationcommon "github.com/answerdev/answer/internal/service/notification_common" "github.com/answerdev/answer/internal/service/revision_common" @@ -126,35 +128,47 @@ func (ns *NotificationService) GetNotificationPage(ctx context.Context, searchCo if err != nil { return nil, err } + resp, err = ns.formatNotificationPage(ctx, notifications) + if err != nil { + return nil, err + } + return pager.NewPageModel(total, resp), nil +} + +func (ns *NotificationService) formatNotificationPage(ctx context.Context, notifications []*entity.Notification) ( + resp []*schema.NotificationContent, err error) { + lang := handler.GetLangByCtx(ctx) for _, notificationInfo := range notifications { item := &schema.NotificationContent{} - err := json.Unmarshal([]byte(notificationInfo.Content), item) - if err != nil { + if err := json.Unmarshal([]byte(notificationInfo.Content), item); err != nil { log.Error("NotificationContent Unmarshal Error", err.Error()) continue } - lang := handler.GetLangByCtx(ctx) - item.NotificationAction = translator.Tr(lang, item.NotificationAction) - item.ID = notificationInfo.ID - item.UpdateTime = notificationInfo.UpdatedAt.Unix() - if notificationInfo.IsRead == schema.NotificationRead { - item.IsRead = true + // If notification is downvote, the user info is not needed. + if item.NotificationAction == constant.NotificationDownVotedTheQuestion || + item.NotificationAction == constant.NotificationDownVotedTheAnswer { + item.UserInfo = nil } - answerID, ok := item.ObjectInfo.ObjectMap["answer"] - if ok { + + item.ID = notificationInfo.ID + item.NotificationAction = translator.Tr(lang, item.NotificationAction) + item.UpdateTime = notificationInfo.UpdatedAt.Unix() + item.IsRead = notificationInfo.IsRead == schema.NotificationRead + + if answerID, ok := item.ObjectInfo.ObjectMap["answer"]; ok { if item.ObjectInfo.ObjectID == answerID { item.ObjectInfo.ObjectID = uid.EnShortID(item.ObjectInfo.ObjectMap["answer"]) } item.ObjectInfo.ObjectMap["answer"] = uid.EnShortID(item.ObjectInfo.ObjectMap["answer"]) } - questionID, ok := item.ObjectInfo.ObjectMap["question"] - if ok { + if questionID, ok := item.ObjectInfo.ObjectMap["question"]; ok { if item.ObjectInfo.ObjectID == questionID { item.ObjectInfo.ObjectID = uid.EnShortID(item.ObjectInfo.ObjectMap["question"]) } item.ObjectInfo.ObjectMap["question"] = uid.EnShortID(item.ObjectInfo.ObjectMap["question"]) } + resp = append(resp, item) } - return pager.NewPageModel(total, resp), nil + return resp, nil } diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go index ecc0a125..5deda1de 100644 --- a/internal/service/notification_common/notification.go +++ b/internal/service/notification_common/notification.go @@ -193,10 +193,10 @@ func (ns *NotificationCommon) SendNotificationToAllFollower(ctx context.Context, if msg.NoNeedPushAllFollow { return } - if msg.NotificationAction != constant.UpdateQuestion && - msg.NotificationAction != constant.AnswerTheQuestion && - msg.NotificationAction != constant.UpdateAnswer && - msg.NotificationAction != constant.AcceptAnswer { + if msg.NotificationAction != constant.NotificationUpdateQuestion && + msg.NotificationAction != constant.NotificationAnswerTheQuestion && + msg.NotificationAction != constant.NotificationUpdateAnswer && + msg.NotificationAction != constant.NotificationAcceptAnswer { return } condObjectID := msg.ObjectID diff --git a/internal/service/question_service.go b/internal/service/question_service.go index 7bfb908d..860710c4 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -1063,7 +1063,7 @@ func (qs *QuestionService) AdminSetQuestionStatus(ctx context.Context, questionI msg.ReceiverUserID = questionInfo.UserID msg.TriggerUserID = questionInfo.UserID msg.ObjectType = constant.QuestionObjectType - msg.NotificationAction = constant.YourQuestionWasDeleted + msg.NotificationAction = constant.NotificationYourQuestionWasDeleted notice_queue.AddNotification(msg) return nil } diff --git a/internal/service/report_handle_admin/report_handle.go b/internal/service/report_handle_admin/report_handle.go index c638228a..a7a57272 100644 --- a/internal/service/report_handle_admin/report_handle.go +++ b/internal/service/report_handle_admin/report_handle.go @@ -66,7 +66,7 @@ func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Repor switch req.FlaggedType { case reasonDelete: err = rh.commentRepo.RemoveComment(ctx, objectID) - rh.sendNotification(ctx, reportedUserID, objectID, constant.YourCommentWasDeleted) + rh.sendNotification(ctx, reportedUserID, objectID, constant.NotificationYourCommentWasDeleted) } } return diff --git a/internal/service/revision_service.go b/internal/service/revision_service.go index 3e8c3605..20d3c9ca 100644 --- a/internal/service/revision_service.go +++ b/internal/service/revision_service.go @@ -209,7 +209,7 @@ func (rs *RevisionService) revisionAuditAnswer(ctx context.Context, revisionitem ObjectID: answerinfo.ID, } msg.ObjectType = constant.AnswerObjectType - msg.NotificationAction = constant.UpdateAnswer + msg.NotificationAction = constant.NotificationUpdateAnswer notice_queue.AddNotification(msg) activity_queue.AddActivity(&schema.ActivityMsg{