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 04:55:46 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// 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 04:55:46 +05:30
|
|
|
|
|
|
|
package avatars
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-28 15:08:59 +05:30
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2023-10-05 06:38:19 +05:30
|
|
|
"fmt"
|
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 04:55:46 +05:30
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-10-05 06:38:19 +05:30
|
|
|
"sync/atomic"
|
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 04:55:46 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-10-05 06:38:19 +05:30
|
|
|
|
|
|
|
"strk.kbt.io/projects/go/libravatar"
|
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 04:55:46 +05:30
|
|
|
)
|
|
|
|
|
2022-11-24 03:27:37 +05:30
|
|
|
const (
|
|
|
|
// DefaultAvatarClass is the default class of a rendered avatar
|
2024-03-22 19:15:10 +05:30
|
|
|
DefaultAvatarClass = "ui avatar tw-align-middle"
|
2022-11-24 03:27:37 +05:30
|
|
|
// 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 04:55:46 +05:30
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:38:19 +05:30
|
|
|
type avatarSettingStruct struct {
|
2022-01-05 11:30:32 +05:30
|
|
|
defaultAvatarLink string
|
2023-10-05 06:38:19 +05:30
|
|
|
gravatarSource string
|
|
|
|
gravatarSourceURL *url.URL
|
|
|
|
libravatar *libravatar.Libravatar
|
|
|
|
}
|
2022-01-05 11:30:32 +05:30
|
|
|
|
2023-10-05 06:38:19 +05:30
|
|
|
var avatarSettingAtomic atomic.Pointer[avatarSettingStruct]
|
|
|
|
|
|
|
|
func loadAvatarSetting() (*avatarSettingStruct, error) {
|
|
|
|
s := avatarSettingAtomic.Load()
|
|
|
|
if s == nil || s.gravatarSource != setting.GravatarSource {
|
|
|
|
s = &avatarSettingStruct{}
|
2022-01-05 11:30:32 +05:30
|
|
|
u, err := url.Parse(setting.AppSubURL)
|
|
|
|
if err != nil {
|
2023-10-05 06:38:19 +05:30
|
|
|
return nil, fmt.Errorf("unable to parse AppSubURL: %w", err)
|
2022-01-05 11:30:32 +05:30
|
|
|
}
|
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 04:55:46 +05:30
|
|
|
|
2022-01-05 11:30:32 +05:30
|
|
|
u.Path = path.Join(u.Path, "/assets/img/avatar_default.png")
|
2023-10-05 06:38:19 +05:30
|
|
|
s.defaultAvatarLink = u.String()
|
|
|
|
|
|
|
|
s.gravatarSourceURL, err = url.Parse(setting.GravatarSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse GravatarSource %q: %w", setting.GravatarSource, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.libravatar = libravatar.New()
|
|
|
|
if s.gravatarSourceURL.Scheme == "https" {
|
|
|
|
s.libravatar.SetUseHTTPS(true)
|
|
|
|
s.libravatar.SetSecureFallbackHost(s.gravatarSourceURL.Host)
|
|
|
|
} else {
|
|
|
|
s.libravatar.SetUseHTTPS(false)
|
|
|
|
s.libravatar.SetFallbackHost(s.gravatarSourceURL.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
avatarSettingAtomic.Store(s)
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAvatarLink the default avatar link
|
|
|
|
func DefaultAvatarLink() string {
|
|
|
|
a, err := loadAvatarSetting()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to loadAvatarSetting: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return a.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 04:55:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// HashEmail hashes email address to MD5 string. https://en.gravatar.com/site/implement/hash/
|
|
|
|
func HashEmail(email string) string {
|
2023-12-28 15:08:59 +05:30
|
|
|
m := md5.New()
|
|
|
|
_, _ = m.Write([]byte(strings.ToLower(strings.TrimSpace(email))))
|
|
|
|
return hex.EncodeToString(m.Sum(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 04:55:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetEmailForHash converts a provided md5sum to the email
|
2023-10-14 14:07:24 +05:30
|
|
|
func GetEmailForHash(ctx context.Context, md5Sum string) (string, 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 04:55:46 +05:30
|
|
|
return cache.GetString("Avatar:"+md5Sum, func() (string, error) {
|
|
|
|
emailHash := EmailHash{
|
|
|
|
Hash: strings.ToLower(strings.TrimSpace(md5Sum)),
|
|
|
|
}
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
_, err := db.GetEngine(ctx).Get(&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 04:55:46 +05:30
|
|
|
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) {
|
2023-10-05 06:38:19 +05:30
|
|
|
a, err := loadAvatarSetting()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
urlStr, err := a.libravatar.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 04:55:46 +05:30
|
|
|
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
|
2023-10-14 14:07:24 +05:30
|
|
|
func saveEmailHash(ctx context.Context, email string) string {
|
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 04:55:46 +05:30
|
|
|
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
|
2023-10-14 14:07:24 +05:30
|
|
|
if err := db.WithTx(ctx, 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 04:55:46 +05:30
|
|
|
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 23:48:25 +05:30
|
|
|
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 04:55:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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 23:48:25 +05:30
|
|
|
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 04:55:46 +05:30
|
|
|
}
|
2021-11-16 23:48:25 +05:30
|
|
|
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 04:55:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
func generateEmailAvatarLink(ctx context.Context, email string, size int, final bool) string {
|
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 04:55:46 +05:30
|
|
|
email = strings.TrimSpace(email)
|
|
|
|
if email == "" {
|
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:38:19 +05:30
|
|
|
avatarSetting, err := loadAvatarSetting()
|
|
|
|
if err != nil {
|
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
2022-10-17 04:59:26 +05:30
|
|
|
|
2023-10-05 06:38:19 +05:30
|
|
|
enableFederatedAvatar := setting.Config().Picture.EnableFederatedAvatar.Value(ctx)
|
|
|
|
if enableFederatedAvatar {
|
2023-10-14 14:07:24 +05:30
|
|
|
emailHash := saveEmailHash(ctx, 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 04:55:46 +05:30
|
|
|
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 23:48:25 +05:30
|
|
|
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 04:55:46 +05:30
|
|
|
if size > 0 {
|
|
|
|
urlStr += "?size=" + strconv.Itoa(size)
|
|
|
|
}
|
|
|
|
return urlStr
|
2022-10-17 04:59:26 +05:30
|
|
|
}
|
|
|
|
|
2023-10-05 06:38:19 +05:30
|
|
|
disableGravatar := setting.Config().Picture.DisableGravatar.Value(ctx)
|
2022-11-10 12:13:53 +05:30
|
|
|
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 04:55:46 +05:30
|
|
|
// copy GravatarSourceURL, because we will modify its Path.
|
2023-10-05 06:38:19 +05:30
|
|
|
avatarURLCopy := *avatarSetting.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 04:55:46 +05:30
|
|
|
avatarURLCopy.Path = path.Join(avatarURLCopy.Path, HashEmail(email))
|
|
|
|
return generateRecognizedAvatarURL(avatarURLCopy, size)
|
|
|
|
}
|
2022-10-17 04:59:26 +05:30
|
|
|
|
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 04:55:46 +05:30
|
|
|
return DefaultAvatarLink()
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// GenerateEmailAvatarFastLink returns a avatar link (fast, the link may be a delegated one: "/avatar/${hash}")
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
func GenerateEmailAvatarFastLink(ctx context.Context, email string, size int) string {
|
|
|
|
return generateEmailAvatarLink(ctx, email, size, false)
|
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 04:55:46 +05:30
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
// GenerateEmailAvatarFinalLink returns a avatar final link (maybe slow)
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 19:07:34 +05:30
|
|
|
func GenerateEmailAvatarFinalLink(ctx context.Context, email string, size int) string {
|
|
|
|
return generateEmailAvatarLink(ctx, email, size, true)
|
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 04:55:46 +05:30
|
|
|
}
|