Skip to content

Commit efb7812

Browse files
committed
feat(notification): remove user info when meet down vote notification.
1 parent 276e87a commit efb7812

File tree

10 files changed

+80
-66
lines changed

10 files changed

+80
-66
lines changed
Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
package constant
22

33
const (
4-
// UpdateQuestion update question
5-
UpdateQuestion = "notification.action.update_question"
6-
// AnswerTheQuestion answer the question
7-
AnswerTheQuestion = "notification.action.answer_the_question"
8-
// UpVotedTheQuestion up voted the question
9-
UpVotedTheQuestion = "notification.action.up_voted_question"
10-
// DownVotedTheQuestion down voted the question
11-
DownVotedTheQuestion = "notification.action.down_voted_question"
12-
// UpdateAnswer update answer
13-
UpdateAnswer = "notification.action.update_answer"
14-
// AcceptAnswer accept answer
15-
AcceptAnswer = "notification.action.accept_answer"
16-
// UpVotedTheAnswer up voted the answer
17-
UpVotedTheAnswer = "notification.action.up_voted_answer"
18-
// DownVotedTheAnswer down voted the answer
19-
DownVotedTheAnswer = "notification.action.down_voted_answer"
20-
// CommentQuestion comment question
21-
CommentQuestion = "notification.action.comment_question"
22-
// CommentAnswer comment answer
23-
CommentAnswer = "notification.action.comment_answer"
24-
// UpVotedTheComment up voted the comment
25-
UpVotedTheComment = "notification.action.up_voted_comment"
26-
// ReplyToYou reply to you
27-
ReplyToYou = "notification.action.reply_to_you"
28-
// MentionYou mention you
29-
MentionYou = "notification.action.mention_you"
30-
// YourQuestionIsClosed your question is closed
31-
YourQuestionIsClosed = "notification.action.your_question_is_closed"
32-
// YourQuestionWasDeleted your question was deleted
33-
YourQuestionWasDeleted = "notification.action.your_question_was_deleted"
34-
// YourAnswerWasDeleted your answer was deleted
35-
YourAnswerWasDeleted = "notification.action.your_answer_was_deleted"
36-
// YourCommentWasDeleted your comment was deleted
37-
YourCommentWasDeleted = "notification.action.your_comment_was_deleted"
4+
// NotificationUpdateQuestion update question
5+
NotificationUpdateQuestion = "notification.action.update_question"
6+
// NotificationAnswerTheQuestion answer the question
7+
NotificationAnswerTheQuestion = "notification.action.answer_the_question"
8+
// NotificationUpVotedTheQuestion up voted the question
9+
NotificationUpVotedTheQuestion = "notification.action.up_voted_question"
10+
// NotificationDownVotedTheQuestion down voted the question
11+
NotificationDownVotedTheQuestion = "notification.action.down_voted_question"
12+
// NotificationUpdateAnswer update answer
13+
NotificationUpdateAnswer = "notification.action.update_answer"
14+
// NotificationAcceptAnswer accept answer
15+
NotificationAcceptAnswer = "notification.action.accept_answer"
16+
// NotificationUpVotedTheAnswer up voted the answer
17+
NotificationUpVotedTheAnswer = "notification.action.up_voted_answer"
18+
// NotificationDownVotedTheAnswer down voted the answer
19+
NotificationDownVotedTheAnswer = "notification.action.down_voted_answer"
20+
// NotificationCommentQuestion comment question
21+
NotificationCommentQuestion = "notification.action.comment_question"
22+
// NotificationCommentAnswer comment answer
23+
NotificationCommentAnswer = "notification.action.comment_answer"
24+
// NotificationUpVotedTheComment up voted the comment
25+
NotificationUpVotedTheComment = "notification.action.up_voted_comment"
26+
// NotificationReplyToYou reply to you
27+
NotificationReplyToYou = "notification.action.reply_to_you"
28+
// NotificationMentionYou mention you
29+
NotificationMentionYou = "notification.action.mention_you"
30+
// NotificationYourQuestionIsClosed your question is closed
31+
NotificationYourQuestionIsClosed = "notification.action.your_question_is_closed"
32+
// NotificationYourQuestionWasDeleted your question was deleted
33+
NotificationYourQuestionWasDeleted = "notification.action.your_question_was_deleted"
34+
// NotificationYourAnswerWasDeleted your answer was deleted
35+
NotificationYourAnswerWasDeleted = "notification.action.your_answer_was_deleted"
36+
// NotificationYourCommentWasDeleted your comment was deleted
37+
NotificationYourCommentWasDeleted = "notification.action.your_comment_was_deleted"
3838
)

internal/repo/activity/answer_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (ar *AnswerActivityRepo) AcceptAnswer(ctx context.Context,
216216
if act.UserID != questionUserID {
217217
msg.TriggerUserID = questionUserID
218218
msg.ObjectType = constant.AnswerObjectType
219-
msg.NotificationAction = constant.AcceptAnswer
219+
msg.NotificationAction = constant.NotificationAcceptAnswer
220220
notice_queue.AddNotification(msg)
221221
}
222222
}

internal/repo/activity/vote_repo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,21 @@ func (vr *VoteRepo) sendVoteInboxNotification(triggerUserID, receiverUserID, obj
467467
}
468468
if objectType == constant.QuestionObjectType {
469469
if upvote {
470-
msg.NotificationAction = constant.UpVotedTheQuestion
470+
msg.NotificationAction = constant.NotificationUpVotedTheQuestion
471471
} else {
472-
msg.NotificationAction = constant.DownVotedTheQuestion
472+
msg.NotificationAction = constant.NotificationDownVotedTheQuestion
473473
}
474474
}
475475
if objectType == constant.AnswerObjectType {
476476
if upvote {
477-
msg.NotificationAction = constant.UpVotedTheAnswer
477+
msg.NotificationAction = constant.NotificationUpVotedTheAnswer
478478
} else {
479-
msg.NotificationAction = constant.DownVotedTheAnswer
479+
msg.NotificationAction = constant.NotificationDownVotedTheAnswer
480480
}
481481
}
482482
if objectType == constant.CommentObjectType {
483483
if upvote {
484-
msg.NotificationAction = constant.UpVotedTheComment
484+
msg.NotificationAction = constant.NotificationUpVotedTheComment
485485
}
486486
}
487487
if len(msg.NotificationAction) > 0 {

internal/service/answer_service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func (as *AnswerService) AdminSetAnswerStatus(ctx context.Context, req *schema.A
476476
msg.ReceiverUserID = answerInfo.UserID
477477
msg.TriggerUserID = answerInfo.UserID
478478
msg.ObjectType = constant.AnswerObjectType
479-
msg.NotificationAction = constant.YourAnswerWasDeleted
479+
msg.NotificationAction = constant.NotificationYourAnswerWasDeleted
480480
notice_queue.AddNotification(msg)
481481

482482
return nil
@@ -566,7 +566,7 @@ func (as *AnswerService) notificationUpdateAnswer(ctx context.Context, questionU
566566
ObjectID: answerID,
567567
}
568568
msg.ObjectType = constant.AnswerObjectType
569-
msg.NotificationAction = constant.UpdateAnswer
569+
msg.NotificationAction = constant.NotificationUpdateAnswer
570570
notice_queue.AddNotification(msg)
571571
}
572572

@@ -583,7 +583,7 @@ func (as *AnswerService) notificationAnswerTheQuestion(ctx context.Context,
583583
ObjectID: answerID,
584584
}
585585
msg.ObjectType = constant.AnswerObjectType
586-
msg.NotificationAction = constant.AnswerTheQuestion
586+
msg.NotificationAction = constant.NotificationAnswerTheQuestion
587587
notice_queue.AddNotification(msg)
588588

589589
userInfo, exist, err := as.userRepo.GetByUserID(ctx, questionUserID)

internal/service/comment/comment_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ func (cs *CommentService) notificationQuestionComment(ctx context.Context, quest
471471
ObjectID: commentID,
472472
}
473473
msg.ObjectType = constant.CommentObjectType
474-
msg.NotificationAction = constant.CommentQuestion
474+
msg.NotificationAction = constant.NotificationCommentQuestion
475475
notice_queue.AddNotification(msg)
476476

477477
receiverUserInfo, exist, err := cs.userRepo.GetByUserID(ctx, questionUserID)
@@ -526,7 +526,7 @@ func (cs *CommentService) notificationAnswerComment(ctx context.Context,
526526
ObjectID: commentID,
527527
}
528528
msg.ObjectType = constant.CommentObjectType
529-
msg.NotificationAction = constant.CommentAnswer
529+
msg.NotificationAction = constant.NotificationCommentAnswer
530530
notice_queue.AddNotification(msg)
531531

532532
receiverUserInfo, exist, err := cs.userRepo.GetByUserID(ctx, answerUserID)
@@ -578,7 +578,7 @@ func (cs *CommentService) notificationCommentReply(ctx context.Context, replyUse
578578
ObjectID: commentID,
579579
}
580580
msg.ObjectType = constant.CommentObjectType
581-
msg.NotificationAction = constant.ReplyToYou
581+
msg.NotificationAction = constant.NotificationReplyToYou
582582
notice_queue.AddNotification(msg)
583583
}
584584

@@ -599,7 +599,7 @@ func (cs *CommentService) notificationMention(
599599
ObjectID: commentID,
600600
}
601601
msg.ObjectType = constant.CommentObjectType
602-
msg.NotificationAction = constant.MentionYou
602+
msg.NotificationAction = constant.NotificationMentionYou
603603
notice_queue.AddNotification(msg)
604604
alreadyNotifiedUserIDs = append(alreadyNotifiedUserIDs, userInfo.ID)
605605
}

internal/service/notification/notification_service.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"encoding/json"
66
"fmt"
77

8+
"github.com/answerdev/answer/internal/base/constant"
89
"github.com/answerdev/answer/internal/base/data"
910
"github.com/answerdev/answer/internal/base/handler"
1011
"github.com/answerdev/answer/internal/base/pager"
1112
"github.com/answerdev/answer/internal/base/translator"
13+
"github.com/answerdev/answer/internal/entity"
1214
"github.com/answerdev/answer/internal/schema"
1315
notficationcommon "github.com/answerdev/answer/internal/service/notification_common"
1416
"github.com/answerdev/answer/internal/service/revision_common"
@@ -126,35 +128,47 @@ func (ns *NotificationService) GetNotificationPage(ctx context.Context, searchCo
126128
if err != nil {
127129
return nil, err
128130
}
131+
resp, err = ns.formatNotificationPage(ctx, notifications)
132+
if err != nil {
133+
return nil, err
134+
}
135+
return pager.NewPageModel(total, resp), nil
136+
}
137+
138+
func (ns *NotificationService) formatNotificationPage(ctx context.Context, notifications []*entity.Notification) (
139+
resp []*schema.NotificationContent, err error) {
140+
lang := handler.GetLangByCtx(ctx)
129141
for _, notificationInfo := range notifications {
130142
item := &schema.NotificationContent{}
131-
err := json.Unmarshal([]byte(notificationInfo.Content), item)
132-
if err != nil {
143+
if err := json.Unmarshal([]byte(notificationInfo.Content), item); err != nil {
133144
log.Error("NotificationContent Unmarshal Error", err.Error())
134145
continue
135146
}
136-
lang := handler.GetLangByCtx(ctx)
137-
item.NotificationAction = translator.Tr(lang, item.NotificationAction)
147+
// If notification is downvote, the user info is not needed.
148+
if item.NotificationAction == constant.NotificationDownVotedTheQuestion ||
149+
item.NotificationAction == constant.NotificationDownVotedTheAnswer {
150+
item.UserInfo = nil
151+
}
152+
138153
item.ID = notificationInfo.ID
154+
item.NotificationAction = translator.Tr(lang, item.NotificationAction)
139155
item.UpdateTime = notificationInfo.UpdatedAt.Unix()
140-
if notificationInfo.IsRead == schema.NotificationRead {
141-
item.IsRead = true
142-
}
143-
answerID, ok := item.ObjectInfo.ObjectMap["answer"]
144-
if ok {
156+
item.IsRead = notificationInfo.IsRead == schema.NotificationRead
157+
158+
if answerID, ok := item.ObjectInfo.ObjectMap["answer"]; ok {
145159
if item.ObjectInfo.ObjectID == answerID {
146160
item.ObjectInfo.ObjectID = uid.EnShortID(item.ObjectInfo.ObjectMap["answer"])
147161
}
148162
item.ObjectInfo.ObjectMap["answer"] = uid.EnShortID(item.ObjectInfo.ObjectMap["answer"])
149163
}
150-
questionID, ok := item.ObjectInfo.ObjectMap["question"]
151-
if ok {
164+
if questionID, ok := item.ObjectInfo.ObjectMap["question"]; ok {
152165
if item.ObjectInfo.ObjectID == questionID {
153166
item.ObjectInfo.ObjectID = uid.EnShortID(item.ObjectInfo.ObjectMap["question"])
154167
}
155168
item.ObjectInfo.ObjectMap["question"] = uid.EnShortID(item.ObjectInfo.ObjectMap["question"])
156169
}
170+
157171
resp = append(resp, item)
158172
}
159-
return pager.NewPageModel(total, resp), nil
173+
return resp, nil
160174
}

internal/service/notification_common/notification.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ func (ns *NotificationCommon) SendNotificationToAllFollower(ctx context.Context,
193193
if msg.NoNeedPushAllFollow {
194194
return
195195
}
196-
if msg.NotificationAction != constant.UpdateQuestion &&
197-
msg.NotificationAction != constant.AnswerTheQuestion &&
198-
msg.NotificationAction != constant.UpdateAnswer &&
199-
msg.NotificationAction != constant.AcceptAnswer {
196+
if msg.NotificationAction != constant.NotificationUpdateQuestion &&
197+
msg.NotificationAction != constant.NotificationAnswerTheQuestion &&
198+
msg.NotificationAction != constant.NotificationUpdateAnswer &&
199+
msg.NotificationAction != constant.NotificationAcceptAnswer {
200200
return
201201
}
202202
condObjectID := msg.ObjectID

internal/service/question_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ func (qs *QuestionService) AdminSetQuestionStatus(ctx context.Context, questionI
10631063
msg.ReceiverUserID = questionInfo.UserID
10641064
msg.TriggerUserID = questionInfo.UserID
10651065
msg.ObjectType = constant.QuestionObjectType
1066-
msg.NotificationAction = constant.YourQuestionWasDeleted
1066+
msg.NotificationAction = constant.NotificationYourQuestionWasDeleted
10671067
notice_queue.AddNotification(msg)
10681068
return nil
10691069
}

internal/service/report_handle_admin/report_handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Repor
6666
switch req.FlaggedType {
6767
case reasonDelete:
6868
err = rh.commentRepo.RemoveComment(ctx, objectID)
69-
rh.sendNotification(ctx, reportedUserID, objectID, constant.YourCommentWasDeleted)
69+
rh.sendNotification(ctx, reportedUserID, objectID, constant.NotificationYourCommentWasDeleted)
7070
}
7171
}
7272
return

internal/service/revision_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func (rs *RevisionService) revisionAuditAnswer(ctx context.Context, revisionitem
209209
ObjectID: answerinfo.ID,
210210
}
211211
msg.ObjectType = constant.AnswerObjectType
212-
msg.NotificationAction = constant.UpdateAnswer
212+
msg.NotificationAction = constant.NotificationUpdateAnswer
213213
notice_queue.AddNotification(msg)
214214

215215
activity_queue.AddActivity(&schema.ActivityMsg{

0 commit comments

Comments
 (0)