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 /modules | |
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 'modules')
-rw-r--r-- | modules/repository/commits_test.go | 12 | ||||
-rw-r--r-- | modules/setting/picture.go | 60 | ||||
-rw-r--r-- | modules/setting/setting.go | 7 | ||||
-rw-r--r-- | modules/system/appstate.go (renamed from modules/appstate/appstate.go) | 2 | ||||
-rw-r--r-- | modules/system/appstate_test.go (renamed from modules/appstate/appstate_test.go) | 2 | ||||
-rw-r--r-- | modules/system/db.go (renamed from modules/appstate/db.go) | 8 | ||||
-rw-r--r-- | modules/system/item_runtime.go (renamed from modules/appstate/item_runtime.go) | 2 | ||||
-rw-r--r-- | modules/system/setting.go | 46 | ||||
-rw-r--r-- | modules/system/user_setting.go | 34 | ||||
-rw-r--r-- | modules/templates/helper.go | 4 | ||||
-rw-r--r-- | modules/updatechecker/update_checker.go | 6 |
11 files changed, 133 insertions, 50 deletions
diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index c62e324b66..7bd741d0c8 100644 --- a/modules/repository/commits_test.go +++ b/modules/repository/commits_test.go @@ -11,8 +11,10 @@ import ( "time" repo_model "code.gitea.io/gitea/models/repo" + system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) @@ -100,6 +102,14 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) { assert.EqualValues(t, []string{"readme.md"}, headCommit.Modified) } +func enableGravatar(t *testing.T) { + err := system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "false") + assert.NoError(t, err) + setting.GravatarSource = "https://secure.gravatar.com/avatar" + err = system_model.Init() + assert.NoError(t, err) +} + func TestPushCommits_AvatarLink(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) @@ -123,6 +133,8 @@ func TestPushCommits_AvatarLink(t *testing.T) { }, } + enableGravatar(t) + assert.Equal(t, "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=84", pushCommits.AvatarLink("user2@example.com")) diff --git a/modules/setting/picture.go b/modules/setting/picture.go index a6d3447dcc..af9041ade3 100644 --- a/modules/setting/picture.go +++ b/modules/setting/picture.go @@ -4,14 +4,6 @@ package setting -import ( - "net/url" - - "code.gitea.io/gitea/modules/log" - - "strk.kbt.io/projects/go/libravatar" -) - // settings var ( // Picture settings @@ -30,10 +22,8 @@ var ( } GravatarSource string - GravatarSourceURL *url.URL - DisableGravatar bool - EnableFederatedAvatar bool - LibravatarService *libravatar.Libravatar + DisableGravatar bool // Depreciated: migrated to database + EnableFederatedAvatar bool // Depreciated: migrated to database RepoAvatar = struct { Storage @@ -69,38 +59,30 @@ func newPictureService() { default: GravatarSource = source } - DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool() - EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(!InstallLock) - if OfflineMode { - DisableGravatar = true - EnableFederatedAvatar = false - } - if DisableGravatar { - EnableFederatedAvatar = false - } - if EnableFederatedAvatar || !DisableGravatar { - var err error - GravatarSourceURL, err = url.Parse(GravatarSource) - if err != nil { - log.Fatal("Failed to parse Gravatar URL(%s): %v", - GravatarSource, err) - } - } - if EnableFederatedAvatar { - LibravatarService = libravatar.New() - if GravatarSourceURL.Scheme == "https" { - LibravatarService.SetUseHTTPS(true) - LibravatarService.SetSecureFallbackHost(GravatarSourceURL.Host) - } else { - LibravatarService.SetUseHTTPS(false) - LibravatarService.SetFallbackHost(GravatarSourceURL.Host) - } - } + DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool(GetDefaultDisableGravatar()) + deprecatedSettingDB("", "DISABLE_GRAVATAR") + EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(GetDefaultEnableFederatedAvatar(DisableGravatar)) + deprecatedSettingDB("", "ENABLE_FEDERATED_AVATAR") newRepoAvatarService() } +func GetDefaultDisableGravatar() bool { + return !OfflineMode +} + +func GetDefaultEnableFederatedAvatar(disableGravatar bool) bool { + v := !InstallLock + if OfflineMode { + v = false + } + if disableGravatar { + v = false + } + return v +} + func newRepoAvatarService() { sec := Cfg.Section("picture") diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 007e3ef61f..f93be2fbd1 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -606,6 +606,13 @@ func deprecatedSetting(oldSection, oldKey, newSection, newKey string) { } } +// deprecatedSettingDB add a hint that the configuration has been moved to database but still kept in app.ini +func deprecatedSettingDB(oldSection, oldKey string) { + if Cfg.Section(oldSection).HasKey(oldKey) { + log.Error("Deprecated `[%s]` `%s` present which has been copied to database table sys_setting", oldSection, oldKey) + } +} + // loadFromConf initializes configuration context. // NOTE: do not print any log except error. func loadFromConf(allowEmpty bool, extraConfig string) { diff --git a/modules/appstate/appstate.go b/modules/system/appstate.go index f65f5367e2..deee8cd029 100644 --- a/modules/appstate/appstate.go +++ b/modules/system/appstate.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package appstate +package system // StateStore is the interface to get/set app state items type StateStore interface { diff --git a/modules/appstate/appstate_test.go b/modules/system/appstate_test.go index e4a0d72850..fb0c2aaf9f 100644 --- a/modules/appstate/appstate_test.go +++ b/modules/system/appstate_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package appstate +package system import ( "path/filepath" diff --git a/modules/appstate/db.go b/modules/system/db.go index 2538d1b5c8..b1c283c488 100644 --- a/modules/appstate/db.go +++ b/modules/system/db.go @@ -2,10 +2,10 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package appstate +package system import ( - "code.gitea.io/gitea/models/appstate" + "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/json" "github.com/yuin/goldmark/util" @@ -16,7 +16,7 @@ type DBStore struct{} // Get reads the state item func (f *DBStore) Get(item StateItem) error { - content, err := appstate.GetAppStateContent(item.Name()) + content, err := system.GetAppStateContent(item.Name()) if err != nil { return err } @@ -32,5 +32,5 @@ func (f *DBStore) Set(item StateItem) error { if err != nil { return err } - return appstate.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b)) + return system.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b)) } diff --git a/modules/appstate/item_runtime.go b/modules/system/item_runtime.go index 7fdc53f642..ef758a5675 100644 --- a/modules/appstate/item_runtime.go +++ b/modules/system/item_runtime.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. -package appstate +package system // RuntimeState contains app state for runtime, and we can save remote version for update checker here in future type RuntimeState struct { diff --git a/modules/system/setting.go b/modules/system/setting.go new file mode 100644 index 0000000000..aebf24a501 --- /dev/null +++ b/modules/system/setting.go @@ -0,0 +1,46 @@ +// Copyright 2021 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 system + +import ( + "strconv" + + "code.gitea.io/gitea/models/system" + "code.gitea.io/gitea/modules/cache" +) + +func genKey(key string) string { + return "system.setting." + key +} + +// GetSetting returns the setting value via the key +func GetSetting(key string) (string, error) { + return cache.GetString(genKey(key), func() (string, error) { + res, err := system.GetSetting(key) + if err != nil { + return "", err + } + return res.SettingValue, nil + }) +} + +// GetSettingBool return bool value of setting, +// none existing keys and errors are ignored and result in false +func GetSettingBool(key string) bool { + s, _ := GetSetting(key) + b, _ := strconv.ParseBool(s) + return b +} + +// SetSetting sets the setting value +func SetSetting(key, value string, version int) error { + cache.Remove(genKey(key)) + + return system.SetSetting(&system.Setting{ + SettingKey: key, + SettingValue: value, + Version: version, + }) +} diff --git a/modules/system/user_setting.go b/modules/system/user_setting.go new file mode 100644 index 0000000000..eaf146c08d --- /dev/null +++ b/modules/system/user_setting.go @@ -0,0 +1,34 @@ +// Copyright 2021 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 system + +import ( + "fmt" + + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/cache" +) + +func genUserKey(userID int64, key string) string { + return fmt.Sprintf("user_%d.setting.%s", userID, key) +} + +// GetUserSetting returns the user setting value via the key +func GetUserSetting(userID int64, key string) (string, error) { + return cache.GetString(genUserKey(userID, key), func() (string, error) { + res, err := user.GetSetting(userID, key) + if err != nil { + return "", err + } + return res.SettingValue, nil + }) +} + +// SetUserSetting sets the user setting value +func SetUserSetting(userID int64, key, value string) error { + cache.Remove(genUserKey(userID, key)) + + return user.SetUserSetting(userID, key, value) +} diff --git a/modules/templates/helper.go b/modules/templates/helper.go index e10beae1d9..7bd2bc0a1c 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -29,6 +29,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/organization" repo_model "code.gitea.io/gitea/models/repo" + system_model "code.gitea.io/gitea/models/system" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" @@ -41,6 +42,7 @@ import ( "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/svg" + system_module "code.gitea.io/gitea/modules/system" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/gitdiff" @@ -85,7 +87,7 @@ func NewFuncMap() []template.FuncMap { return setting.AssetVersion }, "DisableGravatar": func() bool { - return setting.DisableGravatar + return system_module.GetSettingBool(system_model.KeyPictureDisableGravatar) }, "DefaultShowFullName": func() bool { return setting.UI.DefaultShowFullName diff --git a/modules/updatechecker/update_checker.go b/modules/updatechecker/update_checker.go index 9c1569b15e..816fb3764c 100644 --- a/modules/updatechecker/update_checker.go +++ b/modules/updatechecker/update_checker.go @@ -8,10 +8,10 @@ import ( "io" "net/http" - "code.gitea.io/gitea/modules/appstate" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/proxy" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/system" "github.com/hashicorp/go-version" ) @@ -64,13 +64,13 @@ func GiteaUpdateChecker(httpEndpoint string) error { // UpdateRemoteVersion updates the latest available version of Gitea func UpdateRemoteVersion(version string) (err error) { - return appstate.AppState.Set(&CheckerState{LatestVersion: version}) + return system.AppState.Set(&CheckerState{LatestVersion: version}) } // GetRemoteVersion returns the current remote version (or currently installed version if fail to fetch from DB) func GetRemoteVersion() string { item := new(CheckerState) - if err := appstate.AppState.Get(item); err != nil { + if err := system.AppState.Get(item); err != nil { return "" } return item.LatestVersion |