-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add system setting table with cache and also add cache supports for u…
…ser setting (#18058)
- Loading branch information
Showing
59 changed files
with
1,117 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,53 +2,57 @@ | |
// 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" | ||
) | ||
|
||
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("[email protected]"), | ||
avatars_model.HashEmail("[email protected]"), | ||
) | ||
} | ||
|
||
func TestSizedAvatarLink(t *testing.T) { | ||
setting.AppSubURL = "/testsuburl" | ||
|
||
disableGravatar() | ||
disableGravatar(t) | ||
assert.Equal(t, "/testsuburl/assets/img/avatar_default.png", | ||
GenerateEmailAvatarFastLink("[email protected]", 100)) | ||
avatars_model.GenerateEmailAvatarFastLink("[email protected]", 100)) | ||
|
||
enableGravatar(t) | ||
assert.Equal(t, | ||
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100", | ||
GenerateEmailAvatarFastLink("[email protected]", 100), | ||
avatars_model.GenerateEmailAvatarFastLink("[email protected]", 100), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
- | ||
id: 1 | ||
setting_key: 'disable_gravatar' | ||
setting_value: 'false' | ||
version: 1 | ||
created: 1653533198 | ||
updated: 1653533198 | ||
|
||
- | ||
id: 2 | ||
setting_key: 'enable_federated_avatar' | ||
setting_value: 'false' | ||
version: 1 | ||
created: 1653533198 | ||
updated: 1653533198 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2022 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 migrations | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
type SystemSetting struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
SettingKey string `xorm:"varchar(255) unique"` // ensure key is always lowercase | ||
SettingValue string `xorm:"text"` | ||
Version int `xorm:"version"` // prevent to override | ||
Created timeutil.TimeStamp `xorm:"created"` | ||
Updated timeutil.TimeStamp `xorm:"updated"` | ||
} | ||
|
||
func insertSettingsIfNotExist(x *xorm.Engine, sysSettings []*SystemSetting) error { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
for _, setting := range sysSettings { | ||
exist, err := sess.Table("system_setting").Where("setting_key=?", setting.SettingKey).Exist() | ||
if err != nil { | ||
return err | ||
} | ||
if !exist { | ||
if _, err := sess.Insert(setting); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return sess.Commit() | ||
} | ||
|
||
func createSystemSettingsTable(x *xorm.Engine) error { | ||
if err := x.Sync2(new(SystemSetting)); err != nil { | ||
return fmt.Errorf("sync2: %v", err) | ||
} | ||
|
||
// migrate xx to database | ||
sysSettings := []*SystemSetting{ | ||
{ | ||
SettingKey: "picture.disable_gravatar", | ||
SettingValue: strconv.FormatBool(setting.DisableGravatar), | ||
}, | ||
{ | ||
SettingKey: "picture.enable_federated_avatar", | ||
SettingValue: strconv.FormatBool(setting.EnableFederatedAvatar), | ||
}, | ||
} | ||
|
||
return insertSettingsIfNotExist(x, sysSettings) | ||
} |
Oops, something went wrong.