mirror of https://gitee.com/answerdev/answer.git
fix: status updates are performed when the user status is inconsistent
This commit is contained in:
parent
c8da6a3087
commit
2672230e31
|
@ -91,7 +91,8 @@ func (uc *UserController) GetOtherUserInfoByUsername(ctx *gin.Context) {
|
||||||
// @Router /answer/api/v1/user/status [get]
|
// @Router /answer/api/v1/user/status [get]
|
||||||
func (uc *UserController) GetUserStatus(ctx *gin.Context) {
|
func (uc *UserController) GetUserStatus(ctx *gin.Context) {
|
||||||
userID := middleware.GetLoginUserIDFromContext(ctx)
|
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)
|
handler.HandleResponse(ctx, err, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,14 @@ func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo
|
||||||
return userInfo, nil
|
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) {
|
func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) (err error) {
|
||||||
userInfoCache, err := json.Marshal(userInfo)
|
userInfoCache, err := json.Marshal(userInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -14,6 +14,7 @@ type AuthRepo interface {
|
||||||
SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
|
SetUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
|
||||||
RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error)
|
RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error)
|
||||||
GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, 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)
|
GetCmsUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)
|
||||||
SetCmsUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
|
SetCmsUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) error
|
||||||
RemoveCmsUserCacheInfo(ctx context.Context, accessToken string) (err 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
|
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) {
|
func (as *AuthService) RemoveUserCacheInfo(ctx context.Context, accessToken string) (err error) {
|
||||||
return as.authRepo.RemoveUserCacheInfo(ctx, accessToken)
|
return as.authRepo.RemoveUserCacheInfo(ctx, accessToken)
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ func (us *UserService) GetUserInfoByUserID(ctx context.Context, token, userID st
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserStatus get user info by user id
|
// 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{}
|
resp = &schema.GetUserStatusResp{}
|
||||||
if len(userID) == 0 {
|
if len(userID) == 0 {
|
||||||
return resp, nil
|
return resp, nil
|
||||||
|
@ -77,6 +77,16 @@ func (us *UserService) GetUserStatus(ctx context.Context, userID string) (resp *
|
||||||
if !exist {
|
if !exist {
|
||||||
return nil, errors.BadRequest(reason.UserNotFound)
|
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{
|
resp = &schema.GetUserStatusResp{
|
||||||
Status: schema.UserStatusShow[userInfo.Status],
|
Status: schema.UserStatusShow[userInfo.Status],
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue