diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/activities/repo_activity.go | 6 | ||||
-rw-r--r-- | models/asymkey/gpg_key_commit_verification.go | 11 | ||||
-rw-r--r-- | models/avatars/avatar.go | 14 | ||||
-rw-r--r-- | models/avatars/avatar_test.go | 11 | ||||
-rw-r--r-- | models/git/commit_status.go | 3 | ||||
-rw-r--r-- | models/organization/org.go | 4 | ||||
-rw-r--r-- | models/repo/avatar.go | 7 | ||||
-rw-r--r-- | models/system/setting.go | 59 | ||||
-rw-r--r-- | models/system/setting_test.go | 13 | ||||
-rw-r--r-- | models/user/avatar.go | 12 | ||||
-rw-r--r-- | models/user/user.go | 15 |
11 files changed, 78 insertions, 77 deletions
diff --git a/models/activities/repo_activity.go b/models/activities/repo_activity.go index 9018276c3a..72b6be3122 100644 --- a/models/activities/repo_activity.go +++ b/models/activities/repo_activity.go @@ -97,12 +97,12 @@ func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository } users := make(map[int64]*ActivityAuthorData) var unknownUserID int64 - unknownUserAvatarLink := user_model.NewGhostUser().AvatarLink() + unknownUserAvatarLink := user_model.NewGhostUser().AvatarLink(ctx) for _, v := range code.Authors { if len(v.Email) == 0 { continue } - u, err := user_model.GetUserByEmail(v.Email) + u, err := user_model.GetUserByEmail(ctx, v.Email) if u == nil || user_model.IsErrUserNotExist(err) { unknownUserID-- users[unknownUserID] = &ActivityAuthorData{ @@ -119,7 +119,7 @@ func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository users[u.ID] = &ActivityAuthorData{ Name: u.DisplayName(), Login: u.LowerName, - AvatarLink: u.AvatarLink(), + AvatarLink: u.AvatarLink(ctx), HomeLink: u.HomeLink(), Commits: v.Commits, } diff --git a/models/asymkey/gpg_key_commit_verification.go b/models/asymkey/gpg_key_commit_verification.go index 1b88fb8b13..db6e78cad5 100644 --- a/models/asymkey/gpg_key_commit_verification.go +++ b/models/asymkey/gpg_key_commit_verification.go @@ -4,6 +4,7 @@ package asymkey import ( + "context" "fmt" "hash" "strings" @@ -70,14 +71,14 @@ const ( ) // ParseCommitsWithSignature checks if signaute of commits are corresponding to users gpg keys. -func ParseCommitsWithSignature(oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error)) []*SignCommit { +func ParseCommitsWithSignature(ctx context.Context, oldCommits []*user_model.UserCommit, repoTrustModel repo_model.TrustModelType, isOwnerMemberCollaborator func(*user_model.User) (bool, error)) []*SignCommit { newCommits := make([]*SignCommit, 0, len(oldCommits)) keyMap := map[string]bool{} for _, c := range oldCommits { signCommit := &SignCommit{ UserCommit: c, - Verification: ParseCommitWithSignature(c.Commit), + Verification: ParseCommitWithSignature(ctx, c.Commit), } _ = CalculateTrustStatus(signCommit.Verification, repoTrustModel, isOwnerMemberCollaborator, &keyMap) @@ -88,13 +89,13 @@ func ParseCommitsWithSignature(oldCommits []*user_model.UserCommit, repoTrustMod } // ParseCommitWithSignature check if signature is good against keystore. -func ParseCommitWithSignature(c *git.Commit) *CommitVerification { +func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerification { var committer *user_model.User if c.Committer != nil { var err error // Find Committer account - committer, err = user_model.GetUserByEmail(c.Committer.Email) // This finds the user by primary email or activated email so commit will not be valid if email is not - if err != nil { // Skipping not user for committer + committer, err = user_model.GetUserByEmail(ctx, c.Committer.Email) // This finds the user by primary email or activated email so commit will not be valid if email is not + if err != nil { // Skipping not user for committer committer = &user_model.User{ Name: c.Committer.Name, Email: c.Committer.Email, diff --git a/models/avatars/avatar.go b/models/avatars/avatar.go index 67f693e82f..6cf05dd284 100644 --- a/models/avatars/avatar.go +++ b/models/avatars/avatar.go @@ -147,13 +147,13 @@ func generateRecognizedAvatarURL(u url.URL, size int) 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 { +func generateEmailAvatarLink(ctx context.Context, email string, size int, final bool) string { email = strings.TrimSpace(email) if email == "" { return DefaultAvatarLink() } - enableFederatedAvatar := system_model.GetSettingBool(system_model.KeyPictureEnableFederatedAvatar) + enableFederatedAvatar := system_model.GetSettingBool(ctx, system_model.KeyPictureEnableFederatedAvatar) var err error if enableFederatedAvatar && system_model.LibravatarService != nil { @@ -174,7 +174,7 @@ func generateEmailAvatarLink(email string, size int, final bool) string { return urlStr } - disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar) + disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar) if !disableGravatar { // copy GravatarSourceURL, because we will modify its Path. avatarURLCopy := *system_model.GravatarSourceURL @@ -186,11 +186,11 @@ func generateEmailAvatarLink(email string, size int, final bool) string { } // GenerateEmailAvatarFastLink returns a avatar link (fast, the link may be a delegated one: "/avatar/${hash}") -func GenerateEmailAvatarFastLink(email string, size int) string { - return generateEmailAvatarLink(email, size, false) +func GenerateEmailAvatarFastLink(ctx context.Context, email string, size int) string { + return generateEmailAvatarLink(ctx, email, size, false) } // GenerateEmailAvatarFinalLink returns a avatar final link (maybe slow) -func GenerateEmailAvatarFinalLink(email string, size int) string { - return generateEmailAvatarLink(email, size, true) +func GenerateEmailAvatarFinalLink(ctx context.Context, email string, size int) string { + return generateEmailAvatarLink(ctx, email, size, true) } diff --git a/models/avatars/avatar_test.go b/models/avatars/avatar_test.go index 29be2ea346..a3cb36d0e1 100644 --- a/models/avatars/avatar_test.go +++ b/models/avatars/avatar_test.go @@ -7,6 +7,7 @@ import ( "testing" avatars_model "code.gitea.io/gitea/models/avatars" + "code.gitea.io/gitea/models/db" system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/setting" @@ -16,15 +17,15 @@ import ( const gravatarSource = "https://secure.gravatar.com/avatar/" func disableGravatar(t *testing.T) { - err := system_model.SetSettingNoVersion(system_model.KeyPictureEnableFederatedAvatar, "false") + err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureEnableFederatedAvatar, "false") assert.NoError(t, err) - err = system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "true") + err = system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "true") assert.NoError(t, err) system_model.LibravatarService = nil } func enableGravatar(t *testing.T) { - err := system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "false") + err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false") assert.NoError(t, err) setting.GravatarSource = gravatarSource err = system_model.Init() @@ -47,11 +48,11 @@ func TestSizedAvatarLink(t *testing.T) { disableGravatar(t) assert.Equal(t, "/testsuburl/assets/img/avatar_default.png", - avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100)) + avatars_model.GenerateEmailAvatarFastLink(db.DefaultContext, "gitea@example.com", 100)) enableGravatar(t) assert.Equal(t, "https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100", - avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100), + avatars_model.GenerateEmailAvatarFastLink(db.DefaultContext, "gitea@example.com", 100), ) } diff --git a/models/git/commit_status.go b/models/git/commit_status.go index 7c40b6d214..489507f710 100644 --- a/models/git/commit_status.go +++ b/models/git/commit_status.go @@ -351,7 +351,8 @@ func hashCommitStatusContext(context string) string { func ConvertFromGitCommit(ctx context.Context, commits []*git.Commit, repo *repo_model.Repository) []*SignCommitWithStatuses { return ParseCommitsWithStatus(ctx, asymkey_model.ParseCommitsWithSignature( - user_model.ValidateCommitsWithEmails(commits), + ctx, + user_model.ValidateCommitsWithEmails(ctx, commits), repo.GetTrustModel(), func(user *user_model.User) (bool, error) { return repo_model.IsOwnerMemberCollaborator(repo, user.ID) diff --git a/models/organization/org.go b/models/organization/org.go index 852facf704..f05027be72 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -156,8 +156,8 @@ func (org *Organization) hasMemberWithUserID(ctx context.Context, userID int64) } // AvatarLink returns the full avatar link with http host -func (org *Organization) AvatarLink() string { - return org.AsUser().AvatarLink() +func (org *Organization) AvatarLink(ctx context.Context) string { + return org.AsUser().AvatarLink(ctx) } // HTMLURL returns the organization's full link. diff --git a/models/repo/avatar.go b/models/repo/avatar.go index 9ec01bc04b..a76a949267 100644 --- a/models/repo/avatar.go +++ b/models/repo/avatar.go @@ -85,12 +85,7 @@ func (repo *Repository) relAvatarLink(ctx context.Context) string { } // AvatarLink returns a link to the repository's avatar. -func (repo *Repository) AvatarLink() string { - return repo.avatarLink(db.DefaultContext) -} - -// avatarLink returns user avatar absolute link. -func (repo *Repository) avatarLink(ctx context.Context) string { +func (repo *Repository) AvatarLink(ctx context.Context) string { link := repo.relAvatarLink(ctx) // we only prepend our AppURL to our known (relative, internal) avatar link to get an absolute URL if strings.HasPrefix(link, "/") && !strings.HasPrefix(link, "//") { diff --git a/models/system/setting.go b/models/system/setting.go index 50fe17498e..098d9a1832 100644 --- a/models/system/setting.go +++ b/models/system/setting.go @@ -80,8 +80,8 @@ func IsErrDataExpired(err error) bool { } // GetSettingNoCache returns specific setting without using the cache -func GetSettingNoCache(key string) (*Setting, error) { - v, err := GetSettings([]string{key}) +func GetSettingNoCache(ctx context.Context, key string) (*Setting, error) { + v, err := GetSettings(ctx, []string{key}) if err != nil { return nil, err } @@ -91,27 +91,31 @@ func GetSettingNoCache(key string) (*Setting, error) { return v[strings.ToLower(key)], nil } +const contextCacheKey = "system_setting" + // GetSetting returns the setting value via the key -func GetSetting(key string) (string, error) { - return cache.GetString(genSettingCacheKey(key), func() (string, error) { - res, err := GetSettingNoCache(key) - if err != nil { - return "", err - } - return res.SettingValue, nil +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 + }) }) } // 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) +func GetSettingBool(ctx context.Context, key string) bool { + s, _ := GetSetting(ctx, key) v, _ := strconv.ParseBool(s) return v } // GetSettings returns specific settings -func GetSettings(keys []string) (map[string]*Setting, error) { +func GetSettings(ctx context.Context, keys []string) (map[string]*Setting, error) { for i := 0; i < len(keys); i++ { keys[i] = strings.ToLower(keys[i]) } @@ -161,16 +165,17 @@ func GetAllSettings() (AllSettings, error) { } // DeleteSetting deletes a specific setting for a user -func DeleteSetting(setting *Setting) error { +func DeleteSetting(ctx context.Context, setting *Setting) error { + cache.RemoveContextData(ctx, contextCacheKey, setting.SettingKey) cache.Remove(genSettingCacheKey(setting.SettingKey)) _, err := db.GetEngine(db.DefaultContext).Delete(setting) return err } -func SetSettingNoVersion(key, value string) error { - s, err := GetSettingNoCache(key) +func SetSettingNoVersion(ctx context.Context, key, value string) error { + s, err := GetSettingNoCache(ctx, key) if IsErrSettingIsNotExist(err) { - return SetSetting(&Setting{ + return SetSetting(ctx, &Setting{ SettingKey: key, SettingValue: value, }) @@ -179,11 +184,11 @@ func SetSettingNoVersion(key, value string) error { return err } s.SettingValue = value - return SetSetting(s) + return SetSetting(ctx, s) } // SetSetting updates a users' setting for a specific key -func SetSetting(setting *Setting) error { +func SetSetting(ctx context.Context, setting *Setting) error { if err := upsertSettingValue(strings.ToLower(setting.SettingKey), setting.SettingValue, setting.Version); err != nil { return err } @@ -192,9 +197,11 @@ func SetSetting(setting *Setting) error { cc := cache.GetCache() if cc != nil { - return cc.Put(genSettingCacheKey(setting.SettingKey), setting.SettingValue, setting_module.CacheService.TTLSeconds()) + if err := cc.Put(genSettingCacheKey(setting.SettingKey), setting.SettingValue, setting_module.CacheService.TTLSeconds()); err != nil { + return err + } } - + cache.SetContextData(ctx, contextCacheKey, setting.SettingKey, setting.SettingValue) return nil } @@ -244,7 +251,7 @@ var ( func Init() error { var disableGravatar bool - disableGravatarSetting, err := GetSettingNoCache(KeyPictureDisableGravatar) + disableGravatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureDisableGravatar) if IsErrSettingIsNotExist(err) { disableGravatar = setting_module.GetDefaultDisableGravatar() disableGravatarSetting = &Setting{SettingValue: strconv.FormatBool(disableGravatar)} @@ -255,7 +262,7 @@ func Init() error { } var enableFederatedAvatar bool - enableFederatedAvatarSetting, err := GetSettingNoCache(KeyPictureEnableFederatedAvatar) + enableFederatedAvatarSetting, err := GetSettingNoCache(db.DefaultContext, KeyPictureEnableFederatedAvatar) if IsErrSettingIsNotExist(err) { enableFederatedAvatar = setting_module.GetDefaultEnableFederatedAvatar(disableGravatar) enableFederatedAvatarSetting = &Setting{SettingValue: strconv.FormatBool(enableFederatedAvatar)} @@ -268,13 +275,13 @@ func Init() error { if setting_module.OfflineMode { disableGravatar = true enableFederatedAvatar = false - if !GetSettingBool(KeyPictureDisableGravatar) { - if err := SetSettingNoVersion(KeyPictureDisableGravatar, "true"); err != nil { + if !GetSettingBool(db.DefaultContext, KeyPictureDisableGravatar) { + if err := SetSettingNoVersion(db.DefaultContext, KeyPictureDisableGravatar, "true"); err != nil { return fmt.Errorf("Failed to set setting %q: %w", KeyPictureDisableGravatar, err) } } - if GetSettingBool(KeyPictureEnableFederatedAvatar) { - if err := SetSettingNoVersion(KeyPictureEnableFederatedAvatar, "false"); err != nil { + if GetSettingBool(db.DefaultContext, KeyPictureEnableFederatedAvatar) { + if err := SetSettingNoVersion(db.DefaultContext, KeyPictureEnableFederatedAvatar, "false"); err != nil { return fmt.Errorf("Failed to set setting %q: %w", KeyPictureEnableFederatedAvatar, err) } } diff --git a/models/system/setting_test.go b/models/system/setting_test.go index c43d2e3084..fbd04088e6 100644 --- a/models/system/setting_test.go +++ b/models/system/setting_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/models/unittest" @@ -20,24 +21,24 @@ func TestSettings(t *testing.T) { newSetting := &system.Setting{SettingKey: keyName, SettingValue: "50"} // create setting - err := system.SetSetting(newSetting) + err := system.SetSetting(db.DefaultContext, newSetting) assert.NoError(t, err) // test about saving unchanged values - err = system.SetSetting(newSetting) + err = system.SetSetting(db.DefaultContext, newSetting) assert.NoError(t, err) // get specific setting - settings, err := system.GetSettings([]string{keyName}) + settings, err := system.GetSettings(db.DefaultContext, []string{keyName}) assert.NoError(t, err) assert.Len(t, settings, 1) assert.EqualValues(t, newSetting.SettingValue, settings[strings.ToLower(keyName)].SettingValue) // updated setting updatedSetting := &system.Setting{SettingKey: keyName, SettingValue: "100", Version: settings[strings.ToLower(keyName)].Version} - err = system.SetSetting(updatedSetting) + err = system.SetSetting(db.DefaultContext, updatedSetting) assert.NoError(t, err) - value, err := system.GetSetting(keyName) + value, err := system.GetSetting(db.DefaultContext, keyName) assert.NoError(t, err) assert.EqualValues(t, updatedSetting.SettingValue, value) @@ -48,7 +49,7 @@ func TestSettings(t *testing.T) { assert.EqualValues(t, updatedSetting.SettingValue, settings[strings.ToLower(updatedSetting.SettingKey)].SettingValue) // delete setting - err = system.DeleteSetting(&system.Setting{SettingKey: strings.ToLower(keyName)}) + err = system.DeleteSetting(db.DefaultContext, &system.Setting{SettingKey: strings.ToLower(keyName)}) assert.NoError(t, err) settings, err = system.GetAllSettings() assert.NoError(t, err) diff --git a/models/user/avatar.go b/models/user/avatar.go index e6ca49efd0..9af2a9acc2 100644 --- a/models/user/avatar.go +++ b/models/user/avatar.go @@ -58,7 +58,7 @@ func GenerateRandomAvatar(ctx context.Context, u *User) error { } // AvatarLinkWithSize returns a link to the user's avatar with size. size <= 0 means default size -func (u *User) AvatarLinkWithSize(size int) string { +func (u *User) AvatarLinkWithSize(ctx context.Context, size int) string { if u.ID == -1 { // ghost user return avatars.DefaultAvatarLink() @@ -67,7 +67,7 @@ func (u *User) AvatarLinkWithSize(size int) string { useLocalAvatar := false autoGenerateAvatar := false - disableGravatar := system_model.GetSettingBool(system_model.KeyPictureDisableGravatar) + disableGravatar := system_model.GetSettingBool(ctx, system_model.KeyPictureDisableGravatar) switch { case u.UseCustomAvatar: @@ -79,7 +79,7 @@ func (u *User) AvatarLinkWithSize(size int) string { if useLocalAvatar { if u.Avatar == "" && autoGenerateAvatar { - if err := GenerateRandomAvatar(db.DefaultContext, u); err != nil { + if err := GenerateRandomAvatar(ctx, u); err != nil { log.Error("GenerateRandomAvatar: %v", err) } } @@ -88,12 +88,12 @@ func (u *User) AvatarLinkWithSize(size int) string { } return avatars.GenerateUserAvatarImageLink(u.Avatar, size) } - return avatars.GenerateEmailAvatarFastLink(u.AvatarEmail, size) + return avatars.GenerateEmailAvatarFastLink(ctx, u.AvatarEmail, size) } // AvatarLink returns the full avatar link with http host -func (u *User) AvatarLink() string { - link := u.AvatarLinkWithSize(0) +func (u *User) AvatarLink(ctx context.Context) string { + link := u.AvatarLinkWithSize(ctx, 0) if !strings.HasPrefix(link, "//") && !strings.Contains(link, "://") { return setting.AppURL + strings.TrimPrefix(link, setting.AppSubURL+"/") } diff --git a/models/user/user.go b/models/user/user.go index 0917bea754..0a43de7435 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -1114,11 +1114,11 @@ type UserCommit struct { //revive:disable-line:exported } // ValidateCommitWithEmail check if author's e-mail of commit is corresponding to a user. -func ValidateCommitWithEmail(c *git.Commit) *User { +func ValidateCommitWithEmail(ctx context.Context, c *git.Commit) *User { if c.Author == nil { return nil } - u, err := GetUserByEmail(c.Author.Email) + u, err := GetUserByEmail(ctx, c.Author.Email) if err != nil { return nil } @@ -1126,7 +1126,7 @@ func ValidateCommitWithEmail(c *git.Commit) *User { } // ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users. -func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit { +func ValidateCommitsWithEmails(ctx context.Context, oldCommits []*git.Commit) []*UserCommit { var ( emails = make(map[string]*User) newCommits = make([]*UserCommit, 0, len(oldCommits)) @@ -1135,7 +1135,7 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit { var u *User if c.Author != nil { if v, ok := emails[c.Author.Email]; !ok { - u, _ = GetUserByEmail(c.Author.Email) + u, _ = GetUserByEmail(ctx, c.Author.Email) emails[c.Author.Email] = u } else { u = v @@ -1151,12 +1151,7 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit { } // GetUserByEmail returns the user object by given e-mail if exists. -func GetUserByEmail(email string) (*User, error) { - return GetUserByEmailContext(db.DefaultContext, email) -} - -// GetUserByEmailContext returns the user object by given e-mail if exists with db context -func GetUserByEmailContext(ctx context.Context, email string) (*User, error) { +func GetUserByEmail(ctx context.Context, email string) (*User, error) { if len(email) == 0 { return nil, ErrUserNotExist{0, email, 0} } |