From cc2ccdf83f3ef7298d4b2704804d6e30aa59172d Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Mon, 30 Dec 2024 13:19:42 +0800 Subject: [PATCH 1/2] feat: remove user config --- cmd/wire_gen.go | 8 ++-- .../repo/activity_common/activity_repo.go | 2 +- internal/repo/badge/badge_repo.go | 9 ++++ .../repo/notification/notification_repo.go | 16 ++++++++ .../plugin_config/plugin_user_config_repo.go | 8 ++++ internal/service/activity_common/activity.go | 2 +- internal/service/badge/badge_service.go | 2 + internal/service/content/question_service.go | 2 +- .../notification_common/notification.go | 2 + .../plugin_common/plugin_common_service.go | 1 + internal/service/user_admin/user_backyard.go | 41 +++++++++++++++++-- 11 files changed, 82 insertions(+), 11 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 3f2679847..4363f08bb 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -227,8 +227,11 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, contentRevisionService := content.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService, notificationQueueService, activityQueueService, reportRepo, reviewService, reviewActivityRepo) revisionController := controller.NewRevisionController(contentRevisionService, rankService) rankController := controller.NewRankController(rankService) + badgeRepo := badge.NewBadgeRepo(dataData, uniqueIDRepo) + notificationRepo := notification2.NewNotificationRepo(dataData) + pluginUserConfigRepo := plugin_config.NewPluginUserConfigRepo(dataData) userAdminRepo := user.NewUserAdminRepo(dataData, authRepo) - userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo) + userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo, notificationRepo, pluginUserConfigRepo, badgeRepo) userAdminController := controller_admin.NewUserAdminController(userAdminService) reasonRepo := reason.NewReasonRepo(configService) reasonService := reason2.NewReasonService(reasonRepo) @@ -237,9 +240,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configService, questionCommon) siteInfoController := controller_admin.NewSiteInfoController(siteInfoService) controllerSiteInfoController := controller.NewSiteInfoController(siteInfoCommonService) - notificationRepo := notification2.NewNotificationRepo(dataData) notificationCommon := notificationcommon.NewNotificationCommon(dataData, notificationRepo, userCommon, activityRepo, followRepo, objService, notificationQueueService, userExternalLoginRepo, siteInfoCommonService) - badgeRepo := badge.NewBadgeRepo(dataData, uniqueIDRepo) notificationService := notification.NewNotificationService(dataData, notificationRepo, notificationCommon, revisionService, userRepo, reportRepo, reviewService, badgeRepo) notificationController := controller.NewNotificationController(notificationService, rankService) dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configService, siteInfoCommonService, serviceConf, reviewService, revisionRepo, dataData) @@ -253,7 +254,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, activityController := controller.NewActivityController(activityService) roleController := controller_admin.NewRoleController(roleService) pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData) - pluginUserConfigRepo := plugin_config.NewPluginUserConfigRepo(dataData) importerService := importer.NewImporterService(questionService, rankService, userCommon) pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, pluginUserConfigRepo, configService, dataData, importerService) pluginController := controller_admin.NewPluginController(pluginCommonService) diff --git a/internal/repo/activity_common/activity_repo.go b/internal/repo/activity_common/activity_repo.go index 9f0a59e4d..0fce5a74b 100644 --- a/internal/repo/activity_common/activity_repo.go +++ b/internal/repo/activity_common/activity_repo.go @@ -107,7 +107,7 @@ func (ar *ActivityRepo) GetActivity(ctx context.Context, session *xorm.Session, return } -func (ar *ActivityRepo) GetUserActivitysByActivityType(ctx context.Context, userID string, activityType int) ( +func (ar *ActivityRepo) GetUserActivitiesByActivityType(ctx context.Context, userID string, activityType int) ( activityList []*entity.Activity, err error) { activityList = make([]*entity.Activity, 0) err = ar.data.DB.Context(ctx).Where("user_id = ?", userID). diff --git a/internal/repo/badge/badge_repo.go b/internal/repo/badge/badge_repo.go index d52fc1229..4c5e51ae1 100644 --- a/internal/repo/badge/badge_repo.go +++ b/internal/repo/badge/badge_repo.go @@ -149,3 +149,12 @@ func (r *badgeRepo) UpdateAwardCount(ctx context.Context, badgeID string, awardC } return } + +// DeleteUserBadge delete user badge +func (r *badgeRepo) DeleteUserBadge(ctx context.Context, userID string) (err error) { + _, err = r.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.BadgeAward{}) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} diff --git a/internal/repo/notification/notification_repo.go b/internal/repo/notification/notification_repo.go index 5a2a6cc7a..09ee9f351 100644 --- a/internal/repo/notification/notification_repo.go +++ b/internal/repo/notification/notification_repo.go @@ -137,3 +137,19 @@ func (nr *notificationRepo) CountNotificationByUser(ctx context.Context, cond *e } return count, err } + +func (nr *notificationRepo) DeleteNotification(ctx context.Context, userID string) (err error) { + _, err = nr.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.Notification{}) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} + +func (nr *notificationRepo) DeleteUserNotificationConfig(ctx context.Context, userID string) (err error) { + _, err = nr.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.UserNotificationConfig{}) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} diff --git a/internal/repo/plugin_config/plugin_user_config_repo.go b/internal/repo/plugin_config/plugin_user_config_repo.go index 5b3b54dea..3ab011efb 100644 --- a/internal/repo/plugin_config/plugin_user_config_repo.go +++ b/internal/repo/plugin_config/plugin_user_config_repo.go @@ -97,3 +97,11 @@ func (ur *pluginUserConfigRepo) GetPluginUserConfigPage(ctx context.Context, pag } return } + +func (ur *pluginUserConfigRepo) DeleteUserPluginConfig(ctx context.Context, userID string) (err error) { + _, err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.PluginUserConfig{}) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} diff --git a/internal/service/activity_common/activity.go b/internal/service/activity_common/activity.go index 6b3557c0d..62a8bb887 100644 --- a/internal/service/activity_common/activity.go +++ b/internal/service/activity_common/activity.go @@ -37,7 +37,7 @@ type ActivityRepo interface { GetActivityTypeByObjectType(ctx context.Context, objectKey, action string) (activityType int, err error) GetActivity(ctx context.Context, session *xorm.Session, objectID, userID string, activityType int) ( existsActivity *entity.Activity, exist bool, err error) - GetUserActivitysByActivityType(ctx context.Context, userID string, activityType int) (activityList []*entity.Activity, err error) + GetUserActivitiesByActivityType(ctx context.Context, userID string, activityType int) (activityList []*entity.Activity, err error) GetUserIDObjectIDActivitySum(ctx context.Context, userID, objectID string) (int, error) GetActivityTypeByConfigKey(ctx context.Context, configKey string) (activityType int, err error) AddActivity(ctx context.Context, activity *entity.Activity) (err error) diff --git a/internal/service/badge/badge_service.go b/internal/service/badge/badge_service.go index 0ab538a3b..5ed4c1573 100644 --- a/internal/service/badge/badge_service.go +++ b/internal/service/badge/badge_service.go @@ -45,6 +45,8 @@ type BadgeRepo interface { UpdateStatus(ctx context.Context, id string, status int8) (err error) UpdateAwardCount(ctx context.Context, badgeID string, awardCount int) (err error) + + DeleteUserBadge(ctx context.Context, userID string) (err error) } type BadgeService struct { diff --git a/internal/service/content/question_service.go b/internal/service/content/question_service.go index 145f7da1b..1a282142d 100644 --- a/internal/service/content/question_service.go +++ b/internal/service/content/question_service.go @@ -1445,7 +1445,7 @@ func (qs *QuestionService) GetRecommendQuestionPage(ctx context.Context, req *sc if err != nil { return nil, 0, err } - activities, err := qs.activityRepo.GetUserActivitysByActivityType(ctx, req.LoginUserID, activityType) + activities, err := qs.activityRepo.GetUserActivitiesByActivityType(ctx, req.LoginUserID, activityType) if err != nil { return nil, 0, err } diff --git a/internal/service/notification_common/notification.go b/internal/service/notification_common/notification.go index 9d08e2582..b967970be 100644 --- a/internal/service/notification_common/notification.go +++ b/internal/service/notification_common/notification.go @@ -55,6 +55,8 @@ type NotificationRepo interface { UpdateNotificationContent(ctx context.Context, notification *entity.Notification) (err error) GetById(ctx context.Context, id string) (*entity.Notification, bool, error) CountNotificationByUser(ctx context.Context, cond *entity.Notification) (int64, error) + DeleteNotification(ctx context.Context, userID string) (err error) + DeleteUserNotificationConfig(ctx context.Context, userID string) (err error) } type NotificationCommon struct { diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.go index ea6701bd9..044f39cbb 100644 --- a/internal/service/plugin_common/plugin_common_service.go +++ b/internal/service/plugin_common/plugin_common_service.go @@ -49,6 +49,7 @@ type PluginUserConfigRepo interface { pluginUserConfig *entity.PluginUserConfig, exist bool, err error) GetPluginUserConfigPage(ctx context.Context, page, pageSize int) ( pluginUserConfigs []*entity.PluginUserConfig, total int64, err error) + DeleteUserPluginConfig(ctx context.Context, userID string) (err error) } // PluginCommonService user service diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go index e7f63d5f9..6f436b0c7 100644 --- a/internal/service/user_admin/user_backyard.go +++ b/internal/service/user_admin/user_backyard.go @@ -27,8 +27,11 @@ import ( "github.com/apache/incubator-answer/internal/base/translator" "github.com/apache/incubator-answer/internal/base/validator" answercommon "github.com/apache/incubator-answer/internal/service/answer_common" + "github.com/apache/incubator-answer/internal/service/badge" "github.com/apache/incubator-answer/internal/service/comment_common" "github.com/apache/incubator-answer/internal/service/export" + notificationcommon "github.com/apache/incubator-answer/internal/service/notification_common" + "github.com/apache/incubator-answer/internal/service/plugin_common" questioncommon "github.com/apache/incubator-answer/internal/service/question_common" "github.com/apache/incubator-answer/pkg/token" "net/mail" @@ -79,6 +82,9 @@ type UserAdminService struct { answerCommonRepo answercommon.AnswerRepo commentCommonRepo comment_common.CommentCommonRepo userExternalLoginRepo user_external_login.UserExternalLoginRepo + notificationRepo notificationcommon.NotificationRepo + pluginUserConfigRepo plugin_common.PluginUserConfigRepo + badgeRepo badge.BadgeRepo } // NewUserAdminService new user admin service @@ -94,6 +100,9 @@ func NewUserAdminService( answerCommonRepo answercommon.AnswerRepo, commentCommonRepo comment_common.CommentCommonRepo, userExternalLoginRepo user_external_login.UserExternalLoginRepo, + notificationRepo notificationcommon.NotificationRepo, + pluginUserConfigRepo plugin_common.PluginUserConfigRepo, + badgeRepo badge.BadgeRepo, ) *UserAdminService { return &UserAdminService{ userRepo: userRepo, @@ -107,6 +116,9 @@ func NewUserAdminService( answerCommonRepo: answerCommonRepo, commentCommonRepo: commentCommonRepo, userExternalLoginRepo: userExternalLoginRepo, + notificationRepo: notificationRepo, + pluginUserConfigRepo: pluginUserConfigRepo, + badgeRepo: badgeRepo, } } @@ -154,10 +166,7 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up } if req.IsDeleted() { - err := us.userExternalLoginRepo.DeleteUserExternalLoginByUserID(ctx, userInfo.ID) - if err != nil { - log.Errorf("remove all user external login error: %v", err) - } + us.removeAllUserConfiguration(ctx, userInfo.ID) } // if user reputation is zero means this user is inactive, so try to activate this user. @@ -167,6 +176,30 @@ func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.Up return nil } +// removeAllUserConfiguration remove all user configuration +func (us *UserAdminService) removeAllUserConfiguration(ctx context.Context, userID string) { + err := us.userExternalLoginRepo.DeleteUserExternalLoginByUserID(ctx, userID) + if err != nil { + log.Errorf("remove all user external login error: %v", err) + } + err = us.notificationRepo.DeleteNotification(ctx, userID) + if err != nil { + log.Errorf("remove all user notification error: %v", err) + } + err = us.notificationRepo.DeleteUserNotificationConfig(ctx, userID) + if err != nil { + log.Errorf("remove all user notification config error: %v", err) + } + err = us.pluginUserConfigRepo.DeleteUserPluginConfig(ctx, userID) + if err != nil { + log.Errorf("remove all user plugin config error: %v", err) + } + err = us.badgeRepo.DeleteUserBadge(ctx, userID) + if err != nil { + log.Errorf("remove all user badge error: %v", err) + } +} + // removeAllUserCreatedContent remove all user created content func (us *UserAdminService) removeAllUserCreatedContent(ctx context.Context, userID string) { if err := us.questionCommonRepo.RemoveAllUserQuestion(ctx, userID); err != nil { From fb7d09b8b380e535af79cad2ad7232d6977566cd Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 2 Jan 2025 15:59:07 +0800 Subject: [PATCH 2/2] fix: rename DeleteUserBadge to DeleteUserBadgeAward --- cmd/wire_gen.go | 6 +++--- internal/repo/badge/badge_repo.go | 9 --------- internal/repo/badge_award/badge_award_repo.go | 9 +++++++++ internal/service/badge/badge_award_service.go | 2 ++ internal/service/badge/badge_service.go | 2 -- internal/service/user_admin/user_backyard.go | 10 +++++----- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index 4363f08bb..c45d2f396 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -227,11 +227,11 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, contentRevisionService := content.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService, notificationQueueService, activityQueueService, reportRepo, reviewService, reviewActivityRepo) revisionController := controller.NewRevisionController(contentRevisionService, rankService) rankController := controller.NewRankController(rankService) - badgeRepo := badge.NewBadgeRepo(dataData, uniqueIDRepo) + badgeAwardRepo := badge_award.NewBadgeAwardRepo(dataData, uniqueIDRepo) notificationRepo := notification2.NewNotificationRepo(dataData) pluginUserConfigRepo := plugin_config.NewPluginUserConfigRepo(dataData) userAdminRepo := user.NewUserAdminRepo(dataData, authRepo) - userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo, notificationRepo, pluginUserConfigRepo, badgeRepo) + userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo, siteInfoCommonService, emailService, questionRepo, answerRepo, commentCommonRepo, userExternalLoginRepo, notificationRepo, pluginUserConfigRepo, badgeAwardRepo) userAdminController := controller_admin.NewUserAdminController(userAdminService) reasonRepo := reason.NewReasonRepo(configService) reasonService := reason2.NewReasonService(reasonRepo) @@ -241,6 +241,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, siteInfoController := controller_admin.NewSiteInfoController(siteInfoService) controllerSiteInfoController := controller.NewSiteInfoController(siteInfoCommonService) notificationCommon := notificationcommon.NewNotificationCommon(dataData, notificationRepo, userCommon, activityRepo, followRepo, objService, notificationQueueService, userExternalLoginRepo, siteInfoCommonService) + badgeRepo := badge.NewBadgeRepo(dataData, uniqueIDRepo) notificationService := notification.NewNotificationService(dataData, notificationRepo, notificationCommon, revisionService, userRepo, reportRepo, reviewService, badgeRepo) notificationController := controller.NewNotificationController(notificationService, rankService) dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configService, siteInfoCommonService, serviceConf, reviewService, revisionRepo, dataData) @@ -263,7 +264,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database, metaService := meta2.NewMetaService(metaCommonService, userCommon, answerRepo, questionRepo, eventQueueService) metaController := controller.NewMetaController(metaService) badgeGroupRepo := badge_group.NewBadgeGroupRepo(dataData, uniqueIDRepo) - badgeAwardRepo := badge_award.NewBadgeAwardRepo(dataData, uniqueIDRepo) eventRuleRepo := badge.NewEventRuleRepo(dataData) badgeAwardService := badge2.NewBadgeAwardService(badgeAwardRepo, badgeRepo, userCommon, objService, notificationQueueService) badgeEventService := badge2.NewBadgeEventService(dataData, eventQueueService, badgeRepo, eventRuleRepo, badgeAwardService) diff --git a/internal/repo/badge/badge_repo.go b/internal/repo/badge/badge_repo.go index 4c5e51ae1..d52fc1229 100644 --- a/internal/repo/badge/badge_repo.go +++ b/internal/repo/badge/badge_repo.go @@ -149,12 +149,3 @@ func (r *badgeRepo) UpdateAwardCount(ctx context.Context, badgeID string, awardC } return } - -// DeleteUserBadge delete user badge -func (r *badgeRepo) DeleteUserBadge(ctx context.Context, userID string) (err error) { - _, err = r.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.BadgeAward{}) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - return -} diff --git a/internal/repo/badge_award/badge_award_repo.go b/internal/repo/badge_award/badge_award_repo.go index c1ce1e584..8c8dd9850 100644 --- a/internal/repo/badge_award/badge_award_repo.go +++ b/internal/repo/badge_award/badge_award_repo.go @@ -184,3 +184,12 @@ func (r *badgeAwardRepo) GetByUserIdAndBadgeIdAndAwardKey(ctx context.Context, u } return } + +// DeleteUserBadgeAward delete user badge award +func (r *badgeAwardRepo) DeleteUserBadgeAward(ctx context.Context, userID string) (err error) { + _, err = r.data.DB.Context(ctx).Where("user_id = ?", userID).Delete(&entity.BadgeAward{}) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} diff --git a/internal/service/badge/badge_award_service.go b/internal/service/badge/badge_award_service.go index feb95a657..5b0df2897 100644 --- a/internal/service/badge/badge_award_service.go +++ b/internal/service/badge/badge_award_service.go @@ -52,6 +52,8 @@ type BadgeAwardRepo interface { GetByUserIdAndBadgeId(ctx context.Context, userID string, badgeID string) (badgeAward *entity.BadgeAward, exists bool, err error) GetByUserIdAndBadgeIdAndAwardKey(ctx context.Context, userID string, badgeID string, awardKey string) (badgeAward *entity.BadgeAward, exists bool, err error) + + DeleteUserBadgeAward(ctx context.Context, userID string) (err error) } type BadgeAwardService struct { diff --git a/internal/service/badge/badge_service.go b/internal/service/badge/badge_service.go index 5ed4c1573..0ab538a3b 100644 --- a/internal/service/badge/badge_service.go +++ b/internal/service/badge/badge_service.go @@ -45,8 +45,6 @@ type BadgeRepo interface { UpdateStatus(ctx context.Context, id string, status int8) (err error) UpdateAwardCount(ctx context.Context, badgeID string, awardCount int) (err error) - - DeleteUserBadge(ctx context.Context, userID string) (err error) } type BadgeService struct { diff --git a/internal/service/user_admin/user_backyard.go b/internal/service/user_admin/user_backyard.go index 6f436b0c7..6c1d50c13 100644 --- a/internal/service/user_admin/user_backyard.go +++ b/internal/service/user_admin/user_backyard.go @@ -84,7 +84,7 @@ type UserAdminService struct { userExternalLoginRepo user_external_login.UserExternalLoginRepo notificationRepo notificationcommon.NotificationRepo pluginUserConfigRepo plugin_common.PluginUserConfigRepo - badgeRepo badge.BadgeRepo + badgeAwardRepo badge.BadgeAwardRepo } // NewUserAdminService new user admin service @@ -102,7 +102,7 @@ func NewUserAdminService( userExternalLoginRepo user_external_login.UserExternalLoginRepo, notificationRepo notificationcommon.NotificationRepo, pluginUserConfigRepo plugin_common.PluginUserConfigRepo, - badgeRepo badge.BadgeRepo, + badgeAwardRepo badge.BadgeAwardRepo, ) *UserAdminService { return &UserAdminService{ userRepo: userRepo, @@ -118,7 +118,7 @@ func NewUserAdminService( userExternalLoginRepo: userExternalLoginRepo, notificationRepo: notificationRepo, pluginUserConfigRepo: pluginUserConfigRepo, - badgeRepo: badgeRepo, + badgeAwardRepo: badgeAwardRepo, } } @@ -194,9 +194,9 @@ func (us *UserAdminService) removeAllUserConfiguration(ctx context.Context, user if err != nil { log.Errorf("remove all user plugin config error: %v", err) } - err = us.badgeRepo.DeleteUserBadge(ctx, userID) + err = us.badgeAwardRepo.DeleteUserBadgeAward(ctx, userID) if err != nil { - log.Errorf("remove all user badge error: %v", err) + log.Errorf("remove all user badge award error: %v", err) } }