[MM-44954] Regenerate default avatar (#22871)

* Regenerate default profile picture if username has changed

- Only actions is profile picture has not been changed
- Adjusts ResetLastPictureUpdate store function to store
-curTime instead of 0
    - This is to support updating the default picture while still
    retaining the ability to discern a default image from a set one.
- Changes SetDefaultProfileImage to leverage UpdateDefaultProfileImage
- Test updates around updating user default profile pictures

* App interface updates

* Only display picture update date if non-negative

- Ensures we don't display negative timestamps (default images)
- Change ported for mono-repo changes

* Remove duplicate test assertion

---------

Co-authored-by: Nathan Geist <ngeist@spiria.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Nathan
2023-05-04 08:14:26 -06:00
коммит произвёл GitHub
родитель 349e5d4573
Коммит 670b0e4c9f
8 изменённых файлов: 105 добавлений и 18 удалений

Просмотреть файл

@@ -792,7 +792,7 @@ func (a *App) GetDefaultProfileImage(user *model.User) ([]byte, *model.AppError)
return a.ch.srv.GetDefaultProfileImage(user)
}
func (a *App) SetDefaultProfileImage(c request.CTX, user *model.User) *model.AppError {
func (a *App) UpdateDefaultProfileImage(c request.CTX, user *model.User) *model.AppError {
img, appErr := a.GetDefaultProfileImage(user)
if appErr != nil {
return appErr
@@ -809,6 +809,16 @@ func (a *App) SetDefaultProfileImage(c request.CTX, user *model.User) *model.App
a.InvalidateCacheForUser(user.Id)
return nil
}
func (a *App) SetDefaultProfileImage(c request.CTX, user *model.User) *model.AppError {
if err := a.UpdateDefaultProfileImage(c, user); err != nil {
c.Logger().Error("Failed to update default profile image for user", mlog.String("user_id", user.Id), mlog.Err(err))
return err
}
updatedUser, appErr := a.GetUser(user.Id)
if appErr != nil {
c.Logger().Warn("Error in getting users profile forcing logout", mlog.String("user_id", user.Id), mlog.Err(appErr))
@@ -1228,37 +1238,53 @@ func (a *App) UpdateUser(c request.CTX, user *model.User, sendNotifications bool
}
}
newUser := userUpdate.New
if (newUser.Username != userUpdate.Old.Username) && (newUser.LastPictureUpdate <= 0) {
// When a username is updated and the profile is still using a default profile picture, generate a new one based on their username
if err := a.UpdateDefaultProfileImage(c, newUser); err != nil {
c.Logger().Warn("Error with updating default profile image", mlog.Err(err))
}
tempUser, getUserErr := a.GetUser(user.Id)
if getUserErr != nil {
c.Logger().Warn("Error when retrieving user after profile picture update, avatar may fail to update automatically on client applications.", mlog.Err(getUserErr))
} else {
newUser = tempUser
}
}
if sendNotifications {
if userUpdate.New.Email != userUpdate.Old.Email || newEmail != "" {
if newUser.Email != userUpdate.Old.Email || newEmail != "" {
if *a.Config().EmailSettings.RequireEmailVerification {
a.Srv().Go(func() {
if err := a.SendEmailVerification(userUpdate.New, newEmail, ""); err != nil {
if err := a.SendEmailVerification(newUser, newEmail, ""); err != nil {
c.Logger().Error("Failed to send email verification", mlog.Err(err))
}
})
} else {
a.Srv().Go(func() {
if err := a.Srv().EmailService.SendEmailChangeEmail(userUpdate.Old.Email, userUpdate.New.Email, userUpdate.New.Locale, a.GetSiteURL()); err != nil {
if err := a.Srv().EmailService.SendEmailChangeEmail(userUpdate.Old.Email, newUser.Email, newUser.Locale, a.GetSiteURL()); err != nil {
c.Logger().Error("Failed to send email change email", mlog.Err(err))
}
})
}
}
if userUpdate.New.Username != userUpdate.Old.Username {
if newUser.Username != userUpdate.Old.Username {
a.Srv().Go(func() {
if err := a.Srv().EmailService.SendChangeUsernameEmail(userUpdate.New.Username, userUpdate.New.Email, userUpdate.New.Locale, a.GetSiteURL()); err != nil {
if err := a.Srv().EmailService.SendChangeUsernameEmail(newUser.Username, newUser.Email, newUser.Locale, a.GetSiteURL()); err != nil {
c.Logger().Error("Failed to send change username email", mlog.Err(err))
}
})
}
a.sendUpdatedUserEvent(*userUpdate.New)
a.sendUpdatedUserEvent(*newUser)
}
a.InvalidateCacheForUser(user.Id)
a.onUserProfileChange(user.Id)
return userUpdate.New, nil
return newUser, nil
}
func (a *App) UpdateUserActive(c request.CTX, userID string, active bool) *model.AppError {