diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-10-17 07:29:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-17 07:29:26 +0800 |
commit | f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad (patch) | |
tree | 93abb2f354576e50c87d70b0b4bb46369fb3a1f1 /models/avatars | |
parent | 5d3dbffa150d832d2f9aedd9f90ca91178a95f9c (diff) | |
download | gitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.tar.gz gitea-f860a6d2e4177ed4f4c2a58a07882bd00a1a52ad.zip |
Add system setting table with cache and also add cache supports for user setting (#18058)
Diffstat (limited to 'models/avatars')
-rw-r--r-- | models/avatars/avatar.go | 15 | ||||
-rw-r--r-- | models/avatars/avatar_test.go | 32 | ||||
-rw-r--r-- | models/avatars/main_test.go | 22 |
3 files changed, 51 insertions, 18 deletions
diff --git a/models/avatars/avatar.go b/models/avatars/avatar.go index 9f7b0c474f..418e9b9ccc 100644 --- a/models/avatars/avatar.go +++ b/models/avatars/avatar.go @@ -13,6 +13,7 @@ import ( "sync" "code.gitea.io/gitea/models/db" + system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/log" @@ -72,7 +73,7 @@ func GetEmailForHash(md5Sum string) (string, error) { // 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) { - urlStr, err := setting.LibravatarService.FromEmail(email) + urlStr, err := system_model.LibravatarService.FromEmail(email) if err != nil { log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err) return nil, err @@ -149,8 +150,10 @@ func generateEmailAvatarLink(email string, size int, final bool) string { return DefaultAvatarLink() } + enableFederatedAvatar, _ := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar) + var err error - if setting.EnableFederatedAvatar && setting.LibravatarService != nil { + if enableFederatedAvatar != nil && enableFederatedAvatar.GetValueBool() && system_model.LibravatarService != nil { emailHash := saveEmailHash(email) if final { // for final link, we can spend more time on slow external query @@ -166,12 +169,16 @@ func generateEmailAvatarLink(email string, size int, final bool) string { urlStr += "?size=" + strconv.Itoa(size) } return urlStr - } else if !setting.DisableGravatar { + } + + disableGravatar, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar) + if disableGravatar != nil && !disableGravatar.GetValueBool() { // copy GravatarSourceURL, because we will modify its Path. - avatarURLCopy := *setting.GravatarSourceURL + avatarURLCopy := *system_model.GravatarSourceURL avatarURLCopy.Path = path.Join(avatarURLCopy.Path, HashEmail(email)) return generateRecognizedAvatarURL(avatarURLCopy, size) } + return DefaultAvatarLink() } diff --git a/models/avatars/avatar_test.go b/models/avatars/avatar_test.go index 4d6255ca5f..ace5445fc0 100644 --- a/models/avatars/avatar_test.go +++ b/models/avatars/avatar_test.go @@ -2,12 +2,13 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package avatars +package avatars_test import ( - "net/url" "testing" + avatars_model "code.gitea.io/gitea/models/avatars" + system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" @@ -15,40 +16,43 @@ import ( const gravatarSource = "https://secure.gravatar.com/avatar/" -func disableGravatar() { - setting.EnableFederatedAvatar = false - setting.LibravatarService = nil - setting.DisableGravatar = true +func disableGravatar(t *testing.T) { + err := system_model.SetSettingNoVersion(system_model.KeyPictureEnableFederatedAvatar, "false") + assert.NoError(t, err) + err = system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "true") + assert.NoError(t, err) + system_model.LibravatarService = nil } func enableGravatar(t *testing.T) { - setting.DisableGravatar = false - var err error - setting.GravatarSourceURL, err = url.Parse(gravatarSource) + err := system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "false") + assert.NoError(t, err) + setting.GravatarSource = gravatarSource + err = system_model.Init() assert.NoError(t, err) } func TestHashEmail(t *testing.T) { assert.Equal(t, "d41d8cd98f00b204e9800998ecf8427e", - HashEmail(""), + avatars_model.HashEmail(""), ) assert.Equal(t, "353cbad9b58e69c96154ad99f92bedc7", - HashEmail("gitea@example.com"), + avatars_model.HashEmail("gitea@example.com"), ) } func TestSizedAvatarLink(t *testing.T) { setting.AppSubURL = "/testsuburl" - disableGravatar() + disableGravatar(t) assert.Equal(t, "/testsuburl/assets/img/avatar_default.png", - GenerateEmailAvatarFastLink("gitea@example.com", 100)) + avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100)) enableGravatar(t) assert.Equal(t, "https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100", - GenerateEmailAvatarFastLink("gitea@example.com", 100), + avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100), ) } diff --git a/models/avatars/main_test.go b/models/avatars/main_test.go new file mode 100644 index 0000000000..0e98d8f64d --- /dev/null +++ b/models/avatars/main_test.go @@ -0,0 +1,22 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package avatars_test + +import ( + "path/filepath" + "testing" + + "code.gitea.io/gitea/models/unittest" + + _ "code.gitea.io/gitea/models" + _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/perm/access" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: filepath.Join("..", ".."), + }) +} |