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(user): anonymising notifications of deleted users

LinkinStars99a61fcf7c48
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index 54df5ca1..6d7cdd55 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -205,7 +205,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
controllerSiteInfoController := controller.NewSiteInfoController(siteInfoCommonService)
notificationRepo := notification2.NewNotificationRepo(dataData)
notificationCommon := notificationcommon.NewNotificationCommon(dataData, notificationRepo, userCommon, activityRepo, followRepo, objService, notificationQueueService)
- notificationService := notification.NewNotificationService(dataData, notificationRepo, notificationCommon, revisionService)
+ notificationService := notification.NewNotificationService(dataData, notificationRepo, notificationCommon, revisionService, userRepo)
notificationController := controller.NewNotificationController(notificationService, rankService)
dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configService, siteInfoCommonService, serviceConf, dataData)
dashboardController := controller.NewDashboardController(dashboardService)
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 36c69782..82a8ab2f 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -59,8 +59,6 @@ type UserLoginResp struct {
Website string `json:"website"`
// location
Location string `json:"location"`
- // ip info
- IPInfo string `json:"ip_info"`
// language
Language string `json:"language"`
// access token
@@ -313,7 +311,6 @@ type UserBasicInfo struct {
Avatar string `json:"avatar"`
Website string `json:"website"`
Location string `json:"location"`
- IPInfo string `json:"ip_info"`
Status string `json:"status"`
}
diff --git a/internal/service/notification/notification_service.go b/internal/service/notification/notification_service.go
index 98475e19..c85be9fa 100644
--- a/internal/service/notification/notification_service.go
+++ b/internal/service/notification/notification_service.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
+ usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
@@ -25,6 +26,7 @@ type NotificationService struct {
notificationRepo notficationcommon.NotificationRepo
notificationCommon *notficationcommon.NotificationCommon
revisionService *revision_common.RevisionService
+ userRepo usercommon.UserRepo
}
func NewNotificationService(
@@ -32,13 +34,14 @@ func NewNotificationService(
notificationRepo notficationcommon.NotificationRepo,
notificationCommon *notficationcommon.NotificationCommon,
revisionService *revision_common.RevisionService,
-
+ userRepo usercommon.UserRepo,
) *NotificationService {
return &NotificationService{
data: data,
notificationRepo: notificationRepo,
notificationCommon: notificationCommon,
revisionService: revisionService,
+ userRepo: userRepo,
}
}
@@ -147,6 +150,8 @@ func (ns *NotificationService) formatNotificationPage(ctx context.Context, notif
resp []*schema.NotificationContent, err error) {
lang := handler.GetLangByCtx(ctx)
enableShortID := handler.GetEnableShortID(ctx)
+ userIDs := make([]string, 0)
+ userMapping := make(map[string]bool)
for _, notificationInfo := range notifications {
item := &schema.NotificationContent{}
if err := json.Unmarshal([]byte(notificationInfo.Content), item); err != nil {
@@ -179,7 +184,40 @@ func (ns *NotificationService) formatNotificationPage(ctx context.Context, notif
}
}
+ if item.UserInfo != nil && !userMapping[item.UserInfo.ID] {
+ userIDs = append(userIDs, item.UserInfo.ID)
+ userMapping[item.UserInfo.ID] = true
+ }
resp = append(resp, item)
}
+
+ if len(userIDs) == 0 {
+ return resp, nil
+ }
+
+ users, err := ns.userRepo.BatchGetByID(ctx, userIDs)
+ if err != nil {
+ log.Error(err)
+ return resp, nil
+ }
+ userIDMapping := make(map[string]*entity.User, len(users))
+ for _, user := range users {
+ userIDMapping[user.ID] = user
+ }
+ for _, item := range resp {
+ if item.UserInfo == nil {
+ continue
+ }
+ userInfo, ok := userIDMapping[item.UserInfo.ID]
+ if !ok {
+ continue
+ }
+ if userInfo.Status == entity.UserStatusDeleted {
+ item.UserInfo = &schema.UserBasicInfo{
+ DisplayName: "user" + userInfo.ID,
+ Status: constant.UserDeleted,
+ }
+ }
+ }
return resp, nil
}
diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go
index 5648f9b8..7cb65e8f 100644
--- a/internal/service/user_common/user.go
+++ b/internal/service/user_common/user.go
@@ -133,7 +133,6 @@ func (us *UserCommon) FormatUserBasicInfo(ctx context.Context, userInfo *entity.
userBasicInfo.DisplayName = userInfo.DisplayName
userBasicInfo.Website = userInfo.Website
userBasicInfo.Location = userInfo.Location
- userBasicInfo.IPInfo = userInfo.IPInfo
userBasicInfo.Status = constant.ConvertUserStatus(userInfo.Status, userInfo.MailStatus)
if userBasicInfo.Status == constant.UserDeleted {
userBasicInfo.Avatar = ""