summaryrefslogtreecommitdiffstats
path: root/models/avatar.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-09-19 19:49:59 +0800
committerGitHub <noreply@github.com>2021-09-19 19:49:59 +0800
commita4bfef265d9e512830350635a0489c2cdcd6508f (patch)
tree1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/avatar.go
parent462306e263db5a809dbe2cdf62e99307aeff28de (diff)
downloadgitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz
gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/avatar.go')
-rw-r--r--models/avatar.go29
1 files changed, 15 insertions, 14 deletions
diff --git a/models/avatar.go b/models/avatar.go
index b4c078f8cf..e81b876667 100644
--- a/models/avatar.go
+++ b/models/avatar.go
@@ -12,6 +12,7 @@ import (
"strconv"
"strings"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log"
@@ -24,6 +25,10 @@ type EmailHash struct {
Email string `xorm:"UNIQUE NOT NULL"`
}
+func init() {
+ db.RegisterModel(new(EmailHash))
+}
+
// DefaultAvatarLink the default avatar link
func DefaultAvatarLink() string {
u, err := url.Parse(setting.AppSubURL)
@@ -59,7 +64,7 @@ func GetEmailForHash(md5Sum string) (string, error) {
Hash: strings.ToLower(strings.TrimSpace(md5Sum)),
}
- _, err := x.Get(&emailHash)
+ _, err := db.DefaultContext().Engine().Get(&emailHash)
return emailHash.Email, err
})
}
@@ -90,19 +95,15 @@ func HashedAvatarLink(email string, size int) string {
Hash: sum,
}
// OK we're going to open a session just because I think that that might hide away any problems with postgres reporting errors
- sess := x.NewSession()
- defer sess.Close()
- if err := sess.Begin(); err != nil {
- // we don't care about any DB problem just return the lowerEmail
- return lowerEmail, nil
- }
- has, err := sess.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 lowerEmail, nil
- }
- _, _ = sess.Insert(emailHash)
- if err := sess.Commit(); err != nil {
+ if err := db.WithTx(func(ctx *db.Context) error {
+ has, err := ctx.Engine().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
+ }
+ _, _ = ctx.Engine().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
}