aboutsummaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2023-02-04 13:36:00 +0800
committerGitHub <noreply@github.com>2023-02-04 13:36:00 +0800
commit6d220540c15e68196404951e8b67cac8002d1a6f (patch)
treef6dacfde8af9f17424bccb2833016c98bfc97c69 /internal/db
parented51686240ff84e3cc12be8bc4311c529d44faee (diff)
refactor(db): migrate methods off `user.go` (#7334)
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/user.go30
-rw-r--r--internal/db/users.go34
-rw-r--r--internal/db/users_test.go2
3 files changed, 22 insertions, 44 deletions
diff --git a/internal/db/user.go b/internal/db/user.go
index 17e0f66b..5be64668 100644
--- a/internal/db/user.go
+++ b/internal/db/user.go
@@ -5,7 +5,6 @@
package db
import (
- "context"
"fmt"
_ "image/jpeg"
"os"
@@ -14,8 +13,6 @@ import (
log "unknwon.dev/clog/v2"
"xorm.io/xorm"
- "github.com/gogs/git-module"
-
"gogs.io/gogs/internal/repoutil"
"gogs.io/gogs/internal/userutil"
)
@@ -200,33 +197,6 @@ func DeleteInactivateUsers() (err error) {
return err
}
-// UserCommit represents a commit with validation of user.
-type UserCommit struct {
- User *User
- *git.Commit
-}
-
-// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
-func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit {
- emails := make(map[string]*User)
- newCommits := make([]*UserCommit, len(oldCommits))
- for i := range oldCommits {
- var u *User
- if v, ok := emails[oldCommits[i].Author.Email]; !ok {
- u, _ = Users.GetByEmail(context.TODO(), oldCommits[i].Author.Email)
- emails[oldCommits[i].Author.Email] = u
- } else {
- u = v
- }
-
- newCommits[i] = &UserCommit{
- User: u,
- Commit: oldCommits[i],
- }
- }
- return newCommits
-}
-
// GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
accesses := make([]*Access, 0, 10)
diff --git a/internal/db/users.go b/internal/db/users.go
index 7e986a47..7b2b550e 100644
--- a/internal/db/users.go
+++ b/internal/db/users.go
@@ -450,27 +450,35 @@ func (db *users) GetByEmail(ctx context.Context, email string) (*User, error) {
}
email = strings.ToLower(email)
- // First try to find the user by primary email
+ /*
+ Equivalent SQL for PostgreSQL:
+
+ SELECT * FROM "user"
+ LEFT JOIN email_address ON email_address.uid = "user".id
+ WHERE
+ "user".type = @userType
+ AND (
+ "user".email = @email AND "user".is_active = TRUE
+ OR email_address.email = @email AND email_address.is_activated = TRUE
+ )
+ */
user := new(User)
err := db.WithContext(ctx).
- Where("email = ? AND type = ? AND is_active = ?", email, UserTypeIndividual, true).
- First(user).
+ Joins(dbutil.Quote("LEFT JOIN email_address ON email_address.uid = %s.id", "user"), true).
+ Where(dbutil.Quote("%s.type = ?", "user"), UserTypeIndividual).
+ Where(db.
+ Where(dbutil.Quote("%[1]s.email = ? AND %[1]s.is_active = ?", "user"), email, true).
+ Or("email_address.email = ? AND email_address.is_activated = ?", email, true),
+ ).
+ First(&user).
Error
- if err == nil {
- return user, nil
- } else if err != gorm.ErrRecordNotFound {
- return nil, err
- }
-
- // Otherwise, check activated email addresses
- emailAddress, err := NewEmailAddressesStore(db.DB).GetByEmail(ctx, email, true)
if err != nil {
- if IsErrEmailAddressNotExist(err) {
+ if err == gorm.ErrRecordNotFound {
return nil, ErrUserNotExist{args: errutil.Args{"email": email}}
}
return nil, err
}
- return db.GetByID(ctx, emailAddress.UserID)
+ return user, nil
}
func (db *users) GetByID(ctx context.Context, id int64) (*User, error) {
diff --git a/internal/db/users_test.go b/internal/db/users_test.go
index 10f30b81..171b3a88 100644
--- a/internal/db/users_test.go
+++ b/internal/db/users_test.go
@@ -593,7 +593,7 @@ func usersGetMailableEmailsByUsernames(t *testing.T, db *users) {
_, err = db.Create(ctx, "cindy", "cindy@exmaple.com", CreateUserOptions{Activated: true})
require.NoError(t, err)
- got, err := db.GetMailableEmailsByUsernames(ctx, []string{alice.Name, bob.Name, "404"})
+ got, err := db.GetMailableEmailsByUsernames(ctx, []string{alice.Name, bob.Name, "ignore-non-exist"})
require.NoError(t, err)
want := []string{bob.Email}
assert.Equal(t, want, got)