diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index d63b765f..6d7cdd55 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -194,7 +194,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, reportAdminService := report_admin.NewReportAdminService(reportRepo, userCommon, answerRepo, questionRepo, commentCommonRepo, reportHandle, configService, objService) controller_adminReportController := controller_admin.NewReportController(reportAdminService) userAdminRepo := user.NewUserAdminRepo(dataData, authRepo) - userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService) + userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo) userAdminController := controller_admin.NewUserAdminController(userAdminService) reasonRepo := reason.NewReasonRepo(configService) reasonService := reason2.NewReasonService(reasonRepo) @@ -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/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go index a2fe119e..cd04a8ec 100644 --- a/internal/repo/answer/answer_repo.go +++ b/internal/repo/answer/answer_repo.go @@ -3,6 +3,7 @@ package answer import ( "context" "github.com/answerdev/answer/plugin" + "github.com/segmentfault/pacman/log" "time" "github.com/answerdev/answer/internal/base/constant" @@ -79,6 +80,40 @@ func (ar *answerRepo) RemoveAnswer(ctx context.Context, id string) (err error) { return nil } +// RemoveAllUserAnswer remove all user answer +func (ar *answerRepo) RemoveAllUserAnswer(ctx context.Context, userID string) (err error) { + // find all answer id that need to be deleted + answerIDs := make([]string, 0) + session := ar.data.DB.Context(ctx).Where("user_id = ?", userID) + session.Where("status != ?", entity.AnswerStatusDeleted) + err = session.Select("id").Table("answer").Find(&answerIDs) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if len(answerIDs) == 0 { + return nil + } + + log.Infof("find %d answers need to be deleted for user %s", len(answerIDs), userID) + + // delete all question + session = ar.data.DB.Context(ctx).Where("user_id = ?", userID) + session.Where("status != ?", entity.AnswerStatusDeleted) + _, err = session.Cols("status", "updated_at").Update(&entity.Answer{ + UpdatedAt: time.Now(), + Status: entity.AnswerStatusDeleted, + }) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + + // update search content + for _, id := range answerIDs { + _ = ar.updateSearch(ctx, id) + } + return nil +} + // UpdateAnswer update answer func (ar *answerRepo) UpdateAnswer(ctx context.Context, answer *entity.Answer, Colar []string) (err error) { answer.ID = uid.DeShortID(answer.ID) diff --git a/internal/repo/comment/comment_repo.go b/internal/repo/comment/comment_repo.go index f1863fb3..cd689213 100644 --- a/internal/repo/comment/comment_repo.go +++ b/internal/repo/comment/comment_repo.go @@ -2,6 +2,7 @@ package comment import ( "context" + "github.com/segmentfault/pacman/log" "github.com/answerdev/answer/internal/base/data" "github.com/answerdev/answer/internal/base/pager" @@ -108,3 +109,15 @@ func (cr *commentRepo) GetCommentPage(ctx context.Context, commentQuery *comment } return } + +// RemoveAllUserComment remove all user comment +func (cr *commentRepo) RemoveAllUserComment(ctx context.Context, userID string) (err error) { + session := cr.data.DB.Context(ctx).Where("user_id = ?", userID) + session.Where("status != ?", entity.CommentStatusDeleted) + affected, err := session.Update(&entity.Comment{Status: entity.CommentStatusDeleted}) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + log.Infof("delete user comment, userID: %s, affected: %d", userID, affected) + return +} diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go index 129af992..8420eb45 100644 --- a/internal/repo/question/question_repo.go +++ b/internal/repo/question/question_repo.go @@ -475,3 +475,36 @@ func (qr *questionRepo) updateSearch(ctx context.Context, questionID string) (er err = s.UpdateContent(ctx, content) return } + +func (qr *questionRepo) RemoveAllUserQuestion(ctx context.Context, userID string) (err error) { + // get all question id that need to be deleted + questionIDs := make([]string, 0) + session := qr.data.DB.Context(ctx).Where("user_id = ?", userID) + session.Where("status != ?", entity.QuestionStatusDeleted) + err = session.Select("id").Table("question").Find(&questionIDs) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if len(questionIDs) == 0 { + return nil + } + + log.Infof("find %d questions need to be deleted for user %s", len(questionIDs), userID) + + // delete all question + session = qr.data.DB.Context(ctx).Where("user_id = ?", userID) + session.Where("status != ?", entity.QuestionStatusDeleted) + _, err = session.Cols("status", "updated_at").Update(&entity.Question{ + UpdatedAt: time.Now(), + Status: entity.QuestionStatusDeleted, + }) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + + // update search content + for _, id := range questionIDs { + _ = qr.updateSearch(ctx, id) + } + return nil +} diff --git a/internal/repo/repo_test/comment_repo_test.go b/internal/repo/repo_test/comment_repo_test.go index 8885de7c..2f64cabd 100644 --- a/internal/repo/repo_test/comment_repo_test.go +++ b/internal/repo/repo_test/comment_repo_test.go @@ -65,7 +65,7 @@ func Test_commentRepo_UpdateComment(t *testing.T) { assert.NoError(t, err) testCommentEntity.ParsedText = "test" - err = commentRepo.UpdateCommentContent(context.TODO(), testCommentEntity, "", "") + err = commentRepo.UpdateCommentContent(context.TODO(), testCommentEntity.ID, "test", "test") assert.NoError(t, err) newComment, exist, err := commonCommentRepo.GetComment(context.TODO(), testCommentEntity.ID) diff --git a/internal/repo/repo_test/tag_repo_test.go b/internal/repo/repo_test/tag_repo_test.go index 895b1972..6d4de838 100644 --- a/internal/repo/repo_test/tag_repo_test.go +++ b/internal/repo/repo_test/tag_repo_test.go @@ -93,7 +93,7 @@ func Test_tagRepo_GetTagListByName(t *testing.T) { tagOnce.Do(addTagList) tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource)) - gotTags, err := tagCommonRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, false) + gotTags, err := tagCommonRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, false, false) assert.NoError(t, err) assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName) } diff --git a/internal/repo/tag_common/tag_common_repo.go b/internal/repo/tag_common/tag_common_repo.go index 9218ba47..3160d428 100644 --- a/internal/repo/tag_common/tag_common_repo.go +++ b/internal/repo/tag_common/tag_common_repo.go @@ -117,10 +117,9 @@ func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*ent // GetTagListByNames get tag list all like name func (tr *tagCommonRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) session := tr.data.DB.Context(ctx).In("slug_name", names).UseBool("recommend", "reserved") - // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList) if err != nil { err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() @@ -176,20 +175,50 @@ func (tr *tagCommonRepo) GetTagPage(ctx context.Context, page, pageSize int, tag // AddTagList add tag func (tr *tagCommonRepo) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) { + addTags := make([]*entity.Tag, 0) for _, item := range tagList { + exist, err := tr.updateDeletedTag(ctx, item) + if err != nil { + return err + } + if exist { + continue + } + addTags = append(addTags, item) item.ID, err = tr.uniqueIDRepo.GenUniqueIDStr(ctx, item.TableName()) if err != nil { return err } item.RevisionID = "0" } - _, err = tr.data.DB.Context(ctx).Insert(tagList) + if len(addTags) == 0 { + return nil + } + _, err = tr.data.DB.Context(ctx).Insert(addTags) if err != nil { err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() } return } +func (tr *tagCommonRepo) updateDeletedTag(ctx context.Context, tag *entity.Tag) (exist bool, err error) { + old := &entity.Tag{SlugName: tag.SlugName} + exist, err = tr.data.DB.Context(ctx).Get(old) + if err != nil { + return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !exist || old.Status != entity.TagStatusDeleted { + return false, nil + } + tag.ID = old.ID + tag.Status = entity.TagStatusAvailable + tag.RevisionID = "0" + if _, err = tr.data.DB.Context(ctx).ID(tag.ID).Update(tag); err != nil { + return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return true, nil +} + // UpdateTagQuestionCount update tag question count func (tr *tagCommonRepo) UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) { cond := &entity.Tag{QuestionCount: questionCount} diff --git a/internal/schema/backyard_user_schema.go b/internal/schema/backyard_user_schema.go index e064b192..fd4b96b6 100644 --- a/internal/schema/backyard_user_schema.go +++ b/internal/schema/backyard_user_schema.go @@ -13,9 +13,10 @@ import ( // UpdateUserStatusReq update user request type UpdateUserStatusReq struct { - UserID string `validate:"required" json:"user_id"` - Status string `validate:"required,oneof=normal suspended deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"` - LoginUserID string `json:"-"` + UserID string `validate:"required" json:"user_id"` + Status string `validate:"required,oneof=normal suspended deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"` + RemoveAllContent bool `validate:"omitempty" json:"remove_all_content"` + LoginUserID string `json:"-"` } func (r *UpdateUserStatusReq) IsNormal() bool { return r.Status == constant.UserNormal } diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go index 52fd7f96..122a534b 100644 --- a/internal/schema/tag_schema.go +++ b/internal/schema/tag_schema.go @@ -208,9 +208,6 @@ type UpdateTagReq struct { } func (r *UpdateTagReq) Check() (errFields []*validator.FormErrorField, err error) { - if len(r.EditSummary) == 0 { - r.EditSummary = "tag.edit.summary" - } r.ParsedText = converter.Markdown2HTML(r.OriginalText) return nil, nil } 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/answer_common/answer.go b/internal/service/answer_common/answer.go index 4da92c07..4851fd79 100644 --- a/internal/service/answer_common/answer.go +++ b/internal/service/answer_common/answer.go @@ -26,6 +26,7 @@ type AnswerRepo interface { AdminSearchList(ctx context.Context, search *schema.AdminAnswerPageReq) ([]*entity.Answer, int64, error) UpdateAnswerStatus(ctx context.Context, answer *entity.Answer) (err error) GetAnswerCount(ctx context.Context) (count int64, err error) + RemoveAllUserAnswer(ctx context.Context, userID string) (err error) } // AnswerCommon user service diff --git a/internal/service/comment_common/comment_service.go b/internal/service/comment_common/comment_service.go index a7250a83..a7a6de6b 100644 --- a/internal/service/comment_common/comment_service.go +++ b/internal/service/comment_common/comment_service.go @@ -13,6 +13,7 @@ import ( type CommentCommonRepo interface { GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error) GetCommentCount(ctx context.Context) (count int64, err error) + RemoveAllUserComment(ctx context.Context, userID string) (err error) } // CommentCommonService user service 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/question_common/question.go b/internal/service/question_common/question.go index 8aebb1e9..1286130f 100644 --- a/internal/service/question_common/question.go +++ b/internal/service/question_common/question.go @@ -51,6 +51,7 @@ type QuestionRepo interface { GetQuestionCount(ctx context.Context) (count int64, err error) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) SitemapQuestions(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error) + RemoveAllUserQuestion(ctx context.Context, userID string) (err error) } // QuestionCommon user service diff --git a/internal/service/siteinfo_common/siteinfo_service.go b/internal/service/siteinfo_common/siteinfo_service.go index cf7a8be8..1cfdb0ae 100644 --- a/internal/service/siteinfo_common/siteinfo_service.go +++ b/internal/service/siteinfo_common/siteinfo_service.go @@ -27,7 +27,7 @@ type SiteInfoCommonService interface { GetSiteInterface(ctx context.Context) (resp *schema.SiteInterfaceResp, err error) GetSiteBranding(ctx context.Context) (resp *schema.SiteBrandingResp, err error) GetSiteUsers(ctx context.Context) (resp *schema.SiteUsersResp, err error) - FormatAvatar(ctx context.Context, originalAvatarData, email string) *schema.AvatarInfo + FormatAvatar(ctx context.Context, originalAvatarData, email string, userStatus int) *schema.AvatarInfo FormatListAvatar(ctx context.Context, userList []*entity.User) (userID2AvatarMapping map[string]*schema.AvatarInfo) GetSiteWrite(ctx context.Context) (resp *schema.SiteWriteResp, err error) GetSiteLegal(ctx context.Context) (resp *schema.SiteLegalResp, err error) @@ -82,9 +82,9 @@ func (s *siteInfoCommonService) GetSiteUsers(ctx context.Context) (resp *schema. } // FormatAvatar format avatar -func (s *siteInfoCommonService) FormatAvatar(ctx context.Context, originalAvatarData, email string) *schema.AvatarInfo { +func (s *siteInfoCommonService) FormatAvatar(ctx context.Context, originalAvatarData, email string, userStatus int) *schema.AvatarInfo { gravatarBaseURL, defaultAvatar := s.getAvatarDefaultConfig(ctx) - return s.selectedAvatar(originalAvatarData, defaultAvatar, gravatarBaseURL, email) + return s.selectedAvatar(originalAvatarData, defaultAvatar, gravatarBaseURL, email, userStatus) } // FormatListAvatar format avatar @@ -93,7 +93,7 @@ func (s *siteInfoCommonService) FormatListAvatar(ctx context.Context, userList [ gravatarBaseURL, defaultAvatar := s.getAvatarDefaultConfig(ctx) avatarMapping = make(map[string]*schema.AvatarInfo) for _, user := range userList { - avatarMapping[user.ID] = s.selectedAvatar(user.Avatar, defaultAvatar, gravatarBaseURL, user.EMail) + avatarMapping[user.ID] = s.selectedAvatar(user.Avatar, defaultAvatar, gravatarBaseURL, user.EMail, user.Status) } return avatarMapping } @@ -114,10 +114,18 @@ func (s *siteInfoCommonService) getAvatarDefaultConfig(ctx context.Context) (str } func (s *siteInfoCommonService) selectedAvatar( - originalAvatarData string, defaultAvatar string, gravatarBaseURL string, email string) *schema.AvatarInfo { + originalAvatarData, + defaultAvatar, gravatarBaseURL, + email string, userStatus int) *schema.AvatarInfo { avatarInfo := &schema.AvatarInfo{} _ = json.Unmarshal([]byte(originalAvatarData), avatarInfo) + if userStatus == entity.UserStatusDeleted { + return &schema.AvatarInfo{ + Type: constant.DefaultAvatar, + } + } + if len(avatarInfo.Type) == 0 && defaultAvatar == constant.AvatarTypeGravatar { avatarInfo.Type = constant.AvatarTypeGravatar avatarInfo.Gravatar = gravatar.GetAvatarURL(gravatarBaseURL, email) diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go index 67f57159..831730fe 100644 --- a/internal/service/user_admin/user_backyard.go +++ b/internal/service/user_admin/user_backyard.go @@ -7,7 +7,10 @@ import ( "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/base/validator" + answercommon "github.com/answerdev/answer/internal/service/answer_common" + "github.com/answerdev/answer/internal/service/comment_common" "github.com/answerdev/answer/internal/service/export" + questioncommon "github.com/answerdev/answer/internal/service/question_common" "github.com/google/uuid" "net/mail" "strings" @@ -50,6 +53,9 @@ type UserAdminService struct { userActivity activity.UserActiveActivityRepo siteInfoCommonService siteinfo_common.SiteInfoCommonService emailService *export.EmailService + questionCommonRepo questioncommon.QuestionRepo + answerCommonRepo answercommon.AnswerRepo + commentCommonRepo comment_common.CommentCommonRepo } // NewUserAdminService new user admin service @@ -61,6 +67,9 @@ func NewUserAdminService( userActivity activity.UserActiveActivityRepo, siteInfoCommonService siteinfo_common.SiteInfoCommonService, emailService *export.EmailService, + questionCommonRepo questioncommon.QuestionRepo, + answerCommonRepo answercommon.AnswerRepo, + commentCommonRepo comment_common.CommentCommonRepo, ) *UserAdminService { return &UserAdminService{ userRepo: userRepo, @@ -70,6 +79,9 @@ func NewUserAdminService( userActivity: userActivity, siteInfoCommonService: siteInfoCommonService, emailService: emailService, + questionCommonRepo: questionCommonRepo, + answerCommonRepo: answerCommonRepo, + commentCommonRepo: commentCommonRepo, } } @@ -111,6 +123,11 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up return err } + // remove all content that user created, such as question, answer, comment, etc. + if req.RemoveAllContent { + us.removeAllUserCreatedContent(ctx, userInfo.ID) + } + // if user reputation is zero means this user is inactive, so try to activate this user. if req.IsNormal() && userInfo.Rank == 0 { return us.userActivity.UserActive(ctx, userInfo.ID) @@ -118,6 +135,19 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up return nil } +// removeAllUserCreatedContent remove all user created content +func (us *UserAdminService) removeAllUserCreatedContent(ctx context.Context, userID string) { + if err := us.questionCommonRepo.RemoveAllUserQuestion(ctx, userID); err != nil { + log.Errorf("remove all user question error: %v", err) + } + if err := us.answerCommonRepo.RemoveAllUserAnswer(ctx, userID); err != nil { + log.Errorf("remove all user answer error: %v", err) + } + if err := us.commentCommonRepo.RemoveAllUserComment(ctx, userID); err != nil { + log.Errorf("remove all user comment error: %v", err) + } +} + // UpdateUserRole update user role func (us *UserAdminService) UpdateUserRole(ctx context.Context, req *schema.UpdateUserRoleReq) (err error) { // Users cannot modify their roles diff --git a/internal/service/user_common/user.go b/internal/service/user_common/user.go index 34ab7a75..7cb65e8f 100644 --- a/internal/service/user_common/user.go +++ b/internal/service/user_common/user.go @@ -69,7 +69,7 @@ func (us *UserCommon) GetUserBasicInfoByID(ctx context.Context, ID string) ( return nil, exist, err } info := us.FormatUserBasicInfo(ctx, userInfo) - info.Avatar = us.siteInfoCommonService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + info.Avatar = us.siteInfoCommonService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() return info, exist, nil } @@ -79,7 +79,7 @@ func (us *UserCommon) GetUserBasicInfoByUserName(ctx context.Context, username s return nil, exist, err } info := us.FormatUserBasicInfo(ctx, userInfo) - info.Avatar = us.siteInfoCommonService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + info.Avatar = us.siteInfoCommonService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() return info, exist, nil } @@ -133,11 +133,10 @@ 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 = "" - userBasicInfo.DisplayName = "Anonymous" + userBasicInfo.DisplayName = "user" + userInfo.ID } return userBasicInfo } diff --git a/internal/service/user_service.go b/internal/service/user_service.go index 25db3422..c2553141 100644 --- a/internal/service/user_service.go +++ b/internal/service/user_service.go @@ -92,7 +92,7 @@ func (us *UserService) GetUserInfoByUserID(ctx context.Context, token, userID st if err != nil { log.Error(err) } - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail) + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status) resp.AccessToken = token resp.HavePassword = len(userInfo.Pass) > 0 return resp, nil @@ -109,7 +109,7 @@ func (us *UserService) GetOtherUserInfoByUsername(ctx context.Context, username } resp = &schema.GetOtherUserInfoByUsernameResp{} resp.ConvertFromUserEntity(userInfo) - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() return resp, nil } @@ -145,7 +145,7 @@ func (us *UserService) EmailLogin(ctx context.Context, req *schema.UserEmailLogi resp = &schema.UserLoginResp{} resp.ConvertFromUserEntity(userInfo) - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() userCacheInfo := &entity.UserCacheInfo{ UserID: userInfo.ID, EmailStatus: userInfo.MailStatus, @@ -426,7 +426,7 @@ func (us *UserService) UserRegisterByEmail(ctx context.Context, registerUserInfo // return user info and token resp = &schema.UserLoginResp{} resp.ConvertFromUserEntity(userInfo) - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() userCacheInfo := &entity.UserCacheInfo{ UserID: userInfo.ID, EmailStatus: userInfo.MailStatus, @@ -511,7 +511,7 @@ func (us *UserService) UserVerifyEmail(ctx context.Context, req *schema.UserVeri resp = &schema.UserLoginResp{} resp.ConvertFromUserEntity(userInfo) - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() resp.AccessToken = accessToken // User verified email will update user email status. So user status cache should be updated. if err = us.authService.SetUserStatus(ctx, userCacheInfo); err != nil { @@ -630,7 +630,7 @@ func (us *UserService) UserChangeEmailVerify(ctx context.Context, content string resp = &schema.UserLoginResp{} resp.ConvertFromUserEntity(userInfo) - resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail).GetURL() + resp.Avatar = us.siteInfoService.FormatAvatar(ctx, userInfo.Avatar, userInfo.EMail, userInfo.Status).GetURL() userCacheInfo := &entity.UserCacheInfo{ UserID: userInfo.ID, EmailStatus: entity.EmailStatusAvailable,