MM-12067: Add SetDefaultProfileImage to reset the user profile image to a generated one (#9449)
* MM-12067: Add SetDefaultProfileImage to reset the user profile image to a generated one * Allow to get the default profile image for my user * Allowing to reset the last update image date to 0 * PR reviews
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3e462713de
Коммит
c82a84ed76
88
app/user.go
88
app/user.go
@@ -745,36 +745,72 @@ func getFont(initialFont string) (*truetype.Font, error) {
|
||||
}
|
||||
|
||||
func (a *App) GetProfileImage(user *model.User) ([]byte, bool, *model.AppError) {
|
||||
var img []byte
|
||||
readFailed := false
|
||||
|
||||
if len(*a.Config().FileSettings.DriverName) == 0 {
|
||||
var err *model.AppError
|
||||
if img, err = CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
} else {
|
||||
path := "users/" + user.Id + "/profile.png"
|
||||
|
||||
if data, err := a.ReadFile(path); err != nil {
|
||||
readFailed = true
|
||||
|
||||
if img, err = CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if user.LastPictureUpdate == 0 {
|
||||
if _, err := a.WriteFile(bytes.NewReader(img), path); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
img = data
|
||||
img, appErr := CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont)
|
||||
if appErr != nil {
|
||||
return nil, false, appErr
|
||||
}
|
||||
return img, false, nil
|
||||
}
|
||||
|
||||
return img, readFailed, nil
|
||||
path := "users/" + user.Id + "/profile.png"
|
||||
|
||||
data, err := a.ReadFile(path)
|
||||
if err != nil {
|
||||
img, appErr := CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont)
|
||||
if appErr != nil {
|
||||
return nil, false, appErr
|
||||
}
|
||||
|
||||
if user.LastPictureUpdate == 0 {
|
||||
if _, err := a.WriteFile(bytes.NewReader(img), path); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
return img, true, nil
|
||||
}
|
||||
|
||||
return data, false, nil
|
||||
}
|
||||
|
||||
func (a *App) GetDefaultProfileImage(user *model.User) ([]byte, *model.AppError) {
|
||||
img, appErr := CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont)
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func (a *App) SetDefaultProfileImage(user *model.User) *model.AppError {
|
||||
img, appErr := CreateProfileImage(user.Username, user.Id, a.Config().FileSettings.InitialFont)
|
||||
if appErr != nil {
|
||||
return appErr
|
||||
}
|
||||
|
||||
path := "users/" + user.Id + "/profile.png"
|
||||
|
||||
if _, err := a.WriteFile(bytes.NewReader(img), path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
<-a.Srv.Store.User().ResetLastPictureUpdate(user.Id)
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
updatedUser, appErr := a.GetUser(user.Id)
|
||||
if appErr != nil {
|
||||
mlog.Error(fmt.Sprintf("Error in getting users profile for id=%v forcing logout", user.Id), mlog.String("user_id", user.Id))
|
||||
return nil
|
||||
}
|
||||
|
||||
options := a.Config().GetSanitizeOptions()
|
||||
updatedUser.SanitizeProfile(options)
|
||||
|
||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_USER_UPDATED, "", "", "", nil)
|
||||
message.Add("user", updatedUser)
|
||||
a.Publish(message)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) SetProfileImage(userId string, imageData *multipart.FileHeader) *model.AppError {
|
||||
|
||||
Ссылка в новой задаче
Block a user