diff options
author | Joe Chen <jc@unknwon.io> | 2022-10-09 22:55:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-09 22:55:32 +0800 |
commit | 8982a42d38c537de87fabcc59f062841f1c437c2 (patch) | |
tree | bae626255a1582feb14be4b73f5aa0a581c141ab /internal/db | |
parent | fb00e3e56fd777df96e71df1b497e50ff3fbb069 (diff) |
refactor(db): move `User.HasForkedRepository` to `users.HasForkedRepository` (#7176)
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/user.go | 16 | ||||
-rw-r--r-- | internal/db/user_cache.go | 8 | ||||
-rw-r--r-- | internal/db/users.go | 13 | ||||
-rw-r--r-- | internal/db/users_test.go | 23 |
4 files changed, 41 insertions, 19 deletions
diff --git a/internal/db/user.go b/internal/db/user.go index 97e2800f..08b7e09e 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -124,11 +124,6 @@ func (u *User) AfterSet(colName string, _ xorm.Cell) { } } -// IDStr returns string representation of user's ID. -func (u *User) IDStr() string { - return com.ToStr(u.ID) -} - func (u *User) APIFormat() *api.User { return &api.User{ ID: u.ID, @@ -140,17 +135,6 @@ func (u *User) APIFormat() *api.User { } } -// returns true if user login type is LoginPlain. -func (u *User) IsLocal() bool { - return u.LoginSource <= 0 -} - -// HasForkedRepo checks if user has already forked a repository with given ID. -func (u *User) HasForkedRepo(repoID int64) bool { - _, has, _ := HasForkedRepo(u.ID, repoID) - return has -} - func (u *User) RepoCreationNum() int { if u.MaxRepoCreation <= -1 { return conf.Repository.MaxCreationLimit diff --git a/internal/db/user_cache.go b/internal/db/user_cache.go index 314ea3b1..f4fa8172 100644 --- a/internal/db/user_cache.go +++ b/internal/db/user_cache.go @@ -4,13 +4,17 @@ package db +import ( + "fmt" +) + // MailResendCacheKey returns key used for cache mail resend. func (u *User) MailResendCacheKey() string { - return "MailResend_" + u.IDStr() + return fmt.Sprintf("MailResend_%d", u.ID) } // TwoFactorCacheKey returns key used for cache two factor passcode. // e.g. TwoFactor_1_012664 func (u *User) TwoFactorCacheKey(passcode string) string { - return "TwoFactor_" + u.IDStr() + "_" + passcode + return fmt.Sprintf("TwoFactor_%d_%s", u.ID, passcode) } diff --git a/internal/db/users.go b/internal/db/users.go index 8cebd814..d52cfc4c 100644 --- a/internal/db/users.go +++ b/internal/db/users.go @@ -48,6 +48,8 @@ type UsersStore interface { // GetByUsername returns the user with given username. It returns // ErrUserNotExist when not found. GetByUsername(ctx context.Context, username string) (*User, error) + // HasForkedRepository returns true if the user has forked given repository. + HasForkedRepository(ctx context.Context, userID, repoID int64) bool } var Users UsersStore @@ -68,6 +70,11 @@ func (u *User) AfterFind(_ *gorm.DB) error { return nil } +// IsLocal returns true if user is created as local account. +func (u *User) IsLocal() bool { + return u.LoginSource <= 0 +} + var _ UsersStore = (*users)(nil) type users struct { @@ -344,3 +351,9 @@ func (db *users) GetByUsername(ctx context.Context, username string) (*User, err } return user, nil } + +func (db *users) HasForkedRepository(ctx context.Context, userID, repoID int64) bool { + var count int64 + db.WithContext(ctx).Model(new(Repository)).Where("owner_id = ? AND fork_id = ?", userID, repoID).Count(&count) + return count > 0 +} diff --git a/internal/db/users_test.go b/internal/db/users_test.go index 94922a18..68c1bfdc 100644 --- a/internal/db/users_test.go +++ b/internal/db/users_test.go @@ -24,7 +24,7 @@ func TestUsers(t *testing.T) { } t.Parallel() - tables := []interface{}{new(User), new(EmailAddress)} + tables := []interface{}{new(User), new(EmailAddress), new(Repository)} db := &users{ DB: dbtest.NewDB(t, "users", tables...), } @@ -38,6 +38,7 @@ func TestUsers(t *testing.T) { {"GetByEmail", usersGetByEmail}, {"GetByID", usersGetByID}, {"GetByUsername", usersGetByUsername}, + {"HasForkedRepository", usersHasForkedRepository}, } { t.Run(tc.name, func(t *testing.T) { t.Cleanup(func() { @@ -275,3 +276,23 @@ func usersGetByUsername(t *testing.T, db *users) { wantErr := ErrUserNotExist{args: errutil.Args{"name": "bad_username"}} assert.Equal(t, wantErr, err) } + +func usersHasForkedRepository(t *testing.T, db *users) { + ctx := context.Background() + + has := db.HasForkedRepository(ctx, 1, 1) + assert.False(t, has) + + _, err := NewReposStore(db.DB).Create( + ctx, + 1, + CreateRepoOptions{ + Name: "repo1", + ForkID: 1, + }, + ) + require.NoError(t, err) + + has = db.HasForkedRepository(ctx, 1, 1) + assert.True(t, has) +} |