Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
|
|
|
|
package avatars
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-01-05 09:00:32 +03:00
|
|
|
"sync"
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-10-17 02:29:26 +03:00
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2022-11-24 00:57:37 +03:00
|
|
|
const (
|
|
|
|
// DefaultAvatarClass is the default class of a rendered avatar
|
2023-02-13 20:59:59 +03:00
|
|
|
DefaultAvatarClass = "ui avatar gt-vm"
|
2022-11-24 00:57:37 +03:00
|
|
|
// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar
|
|
|
|
DefaultAvatarPixelSize = 28
|
|
|
|
)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
|
|
|
|
// EmailHash represents a pre-generated hash map (mainly used by LibravatarURL, it queries email server's DNS records)
|
|
|
|
type EmailHash struct {
|
|
|
|
Hash string `xorm:"pk varchar(32)"`
|
|
|
|
Email string `xorm:"UNIQUE NOT NULL"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(EmailHash))
|
|
|
|
}
|
|
|
|
|
2022-01-05 09:00:32 +03:00
|
|
|
var (
|
|
|
|
defaultAvatarLink string
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
// DefaultAvatarLink the default avatar link
|
|
|
|
func DefaultAvatarLink() string {
|
2022-01-05 09:00:32 +03:00
|
|
|
once.Do(func() {
|
|
|
|
u, err := url.Parse(setting.AppSubURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Can not parse AppSubURL: %v", err)
|
|
|
|
return
|
|
|
|
}
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
|
2022-01-05 09:00:32 +03:00
|
|
|
u.Path = path.Join(u.Path, "/assets/img/avatar_default.png")
|
|
|
|
defaultAvatarLink = u.String()
|
|
|
|
})
|
|
|
|
return defaultAvatarLink
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HashEmail hashes email address to MD5 string. https://en.gravatar.com/site/implement/hash/
|
|
|
|
func HashEmail(email string) string {
|
|
|
|
return base.EncodeMD5(strings.ToLower(strings.TrimSpace(email)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmailForHash converts a provided md5sum to the email
|
|
|
|
func GetEmailForHash(md5Sum string) (string, error) {
|
|
|
|
return cache.GetString("Avatar:"+md5Sum, func() (string, error) {
|
|
|
|
emailHash := EmailHash{
|
|
|
|
Hash: strings.ToLower(strings.TrimSpace(md5Sum)),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := db.GetEngine(db.DefaultContext).Get(&emailHash)
|
|
|
|
return emailHash.Email, err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LibravatarURL returns the URL for the given email. Slow due to the DNS lookup.
|
|
|
|
// This function should only be called if a federated avatar service is enabled.
|
|
|
|
func LibravatarURL(email string) (*url.URL, error) {
|
2022-10-17 02:29:26 +03:00
|
|
|
urlStr, err := system_model.LibravatarService.FromEmail(email)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u, err := url.Parse(urlStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to parse libravatar url(%s): error %v", urlStr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// saveEmailHash returns an avatar link for a provided email,
|
|
|
|
// the email and hash are saved into database, which will be used by GetEmailForHash later
|
|
|
|
func saveEmailHash(email string) string {
|
|
|
|
lowerEmail := strings.ToLower(strings.TrimSpace(email))
|
|
|
|
emailHash := HashEmail(lowerEmail)
|
|
|
|
_, _ = cache.GetString("Avatar:"+emailHash, func() (string, error) {
|
|
|
|
emailHash := &EmailHash{
|
|
|
|
Email: lowerEmail,
|
|
|
|
Hash: emailHash,
|
|
|
|
}
|
|
|
|
// OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
|
2022-11-12 23:18:50 +03:00
|
|
|
if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
has, err := db.GetEngine(ctx).Where("email = ? AND hash = ?", emailHash.Email, emailHash.Hash).Get(new(EmailHash))
|
|
|
|
if has || err != nil {
|
|
|
|
// Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, _ = db.GetEngine(ctx).Insert(emailHash)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
// Seriously we don't care about any DB problems just return the lowerEmail - we expect the transaction to fail most of the time
|
|
|
|
return lowerEmail, nil
|
|
|
|
}
|
|
|
|
return lowerEmail, nil
|
|
|
|
})
|
|
|
|
return emailHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateUserAvatarFastLink returns a fast link (302) to the user's avatar: "/user/avatar/${User.Name}/${size}"
|
|
|
|
func GenerateUserAvatarFastLink(userName string, size int) string {
|
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
|
|
|
}
|
2021-11-16 21:18:25 +03:00
|
|
|
return setting.AppSubURL + "/user/avatar/" + url.PathEscape(userName) + "/" + strconv.Itoa(size)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateUserAvatarImageLink returns a link for `User.Avatar` image file: "/avatars/${User.Avatar}"
|
|
|
|
func GenerateUserAvatarImageLink(userAvatar string, size int) string {
|
|
|
|
if size > 0 {
|
2021-11-16 21:18:25 +03:00
|
|
|
return setting.AppSubURL + "/avatars/" + url.PathEscape(userAvatar) + "?size=" + strconv.Itoa(size)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
}
|
2021-11-16 21:18:25 +03:00
|
|
|
return setting.AppSubURL + "/avatars/" + url.PathEscape(userAvatar)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateRecognizedAvatarURL generate a recognized avatar (Gravatar/Libravatar) URL, it modifies the URL so the parameter is passed by a copy
|
|
|
|
func generateRecognizedAvatarURL(u url.URL, size int) string {
|
|
|
|
urlQuery := u.Query()
|
|
|
|
urlQuery.Set("d", "identicon")
|
|
|
|
if size > 0 {
|
|
|
|
urlQuery.Set("s", strconv.Itoa(size))
|
|
|
|
}
|
|
|
|
u.RawQuery = urlQuery.Encode()
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateEmailAvatarLink returns a email avatar link.
|
|
|
|
// if final is true, it may use a slow path (eg: query DNS).
|
|
|
|
// if final is false, it always uses a fast path.
|
|
|
|
func generateEmailAvatarLink(email string, size int, final bool) string {
|
|
|
|
email = strings.TrimSpace(email)
|
|
|
|
if email == "" {
|
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:06:52 +03:00
|
|
|
enableFederatedAvatar := system_model.GetSettingBool(system_model.KeyPictureEnableFederatedAvatar)
|
2022-10-17 02:29:26 +03:00
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
var err error
|
2022-11-10 09:43:53 +03:00
|
|
|
if enableFederatedAvatar && system_model.LibravatarService != nil {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
emailHash := saveEmailHash(email)
|
|
|
|
if final {
|
|
|
|
// for final link, we can spend more time on slow external query
|
|
|
|
var avatarURL *url.URL
|
|
|
|
if avatarURL, err = LibravatarURL(email); err != nil {
|
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
|
|
|
return generateRecognizedAvatarURL(*avatarURL, size)
|
|
|
|
}
|
|
|
|
// for non-final link, we should return fast (use a 302 redirection link)
|
2021-11-16 21:18:25 +03:00
|
|
|
urlStr := setting.AppSubURL + "/avatar/" + url.PathEscape(emailHash)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
if size > 0 {
|
|
|
|
urlStr += "?size=" + strconv.Itoa(size)
|
|
|
|
}
|
|
|
|
return urlStr
|
2022-10-17 02:29:26 +03:00
|
|
|
}
|
|
|
|
|
2023-01-01 19:06:52 +03:00
|
|
|
disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar)
|
2022-11-10 09:43:53 +03:00
|
|
|
if !disableGravatar {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
// copy GravatarSourceURL, because we will modify its Path.
|
2022-10-17 02:29:26 +03:00
|
|
|
avatarURLCopy := *system_model.GravatarSourceURL
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
avatarURLCopy.Path = path.Join(avatarURLCopy.Path, HashEmail(email))
|
|
|
|
return generateRecognizedAvatarURL(avatarURLCopy, size)
|
|
|
|
}
|
2022-10-17 02:29:26 +03:00
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:46:10 +03:00
|
|
|
// GenerateEmailAvatarFastLink returns a avatar link (fast, the link may be a delegated one: "/avatar/${hash}")
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
func GenerateEmailAvatarFastLink(email string, size int) string {
|
|
|
|
return generateEmailAvatarLink(email, size, false)
|
|
|
|
}
|
|
|
|
|
2022-01-20 20:46:10 +03:00
|
|
|
// GenerateEmailAvatarFinalLink returns a avatar final link (maybe slow)
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-06 02:25:46 +03:00
|
|
|
func GenerateEmailAvatarFinalLink(email string, size int) string {
|
|
|
|
return generateEmailAvatarLink(email, size, true)
|
|
|
|
}
|