From 2672230e310d58189cb45b88d503ebf73d727607 Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Thu, 20 Oct 2022 18:21:38 +0800 Subject: [PATCH] fix: status updates are performed when the user status is inconsistent --- internal/controller/user_controller.go | 3 ++- internal/repo/auth/auth.go | 8 ++++++++ internal/service/auth/auth.go | 12 ++++++++++++ internal/service/user_service.go | 12 +++++++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.go index 7f37801b..b7e8cb4b 100644 --- a/internal/controller/user_controller.go +++ b/internal/controller/user_controller.go @@ -91,7 +91,8 @@ func (uc *UserController) GetOtherUserInfoByUsername(ctx *gin.Context) { // @Router /answer/api/v1/user/status [get] func (uc *UserController) GetUserStatus(ctx *gin.Context) { userID := middleware.GetLoginUserIDFromContext(ctx) - resp, err := uc.userService.GetUserStatus(ctx, userID) + token := middleware.ExtractToken(ctx) + resp, err := uc.userService.GetUserStatus(ctx, userID, token) handler.HandleResponse(ctx, err, resp) } diff --git a/internal/repo/auth/auth.go b/internal/repo/auth/auth.go index c71d4d00..784b1e30 100644 --- a/internal/repo/auth/auth.go +++ b/internal/repo/auth/auth.go @@ -43,6 +43,14 @@ func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo return userInfo, nil } +func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err error) { + err = ar.data.Cache.Del(ctx, constant.UserStatusChangedCacheKey+userID) + if err != nil { + return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return nil +} + func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) { userInfoCache, err := json.Marshal(userInfo) if err != nil { diff --git a/internal/service/auth/auth.go b/internal/service/auth/auth.go index 6f4ff43a..5c930433 100644 --- a/internal/service/auth/auth.go +++ b/internal/service/auth/auth.go @@ -14,6 +14,7 @@ type AuthRepo interface { SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) + RemoveUserStatus(ctx context.Context, userID string) (err error) GetCmsUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) SetCmsUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error RemoveCmsUserCacheInfo(ctx context.Context, accessToken string) (err error) @@ -56,6 +57,17 @@ func (as *AuthService) SetUserCacheInfo(ctx context.Context, userInfo *entity.Us return accessToken, err } +func (as *AuthService) UpdateUserCacheInfo(ctx context.Context, token string, userInfo *entity.UserCacheInfo) (err error) { + err = as.authRepo.SetUserCacheInfo(ctx, token, userInfo) + if err != nil { + return err + } + if err := as.authRepo.RemoveUserStatus(ctx, userInfo.UserID); err != nil { + log.Error(err) + } + return +} + func (as *AuthService) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) { return as.authRepo.RemoveUserCacheInfo(ctx, accessToken) } diff --git a/internal/service/user_service.go b/internal/service/user_service.go index 85101562..20ab8346 100644 --- a/internal/service/user_service.go +++ b/internal/service/user_service.go @@ -65,7 +65,7 @@ func (us *UserService) GetUserInfoByUserID(ctx context.Context, token, userID st } // GetUserStatus get user info by user id -func (us *UserService) GetUserStatus(ctx context.Context, userID string) (resp *schema.GetUserStatusResp, err error) { +func (us *UserService) GetUserStatus(ctx context.Context, userID, token string) (resp *schema.GetUserStatusResp, err error) { resp = &schema.GetUserStatusResp{} if len(userID) == 0 { return resp, nil @@ -77,6 +77,16 @@ func (us *UserService) GetUserStatus(ctx context.Context, userID string) (resp * if !exist { return nil, errors.BadRequest(reason.UserNotFound) } + + userCacheInfo := &entity.UserCacheInfo{ + UserID: userID, + UserStatus: userInfo.Status, + EmailStatus: userInfo.MailStatus, + } + err = us.authService.UpdateUserCacheInfo(ctx, token, userCacheInfo) + if err != nil { + return nil, err + } resp = &schema.GetUserStatusResp{ Status: schema.UserStatusShow[userInfo.Status], }