aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-17 17:58:31 +0800
committerGitHub <noreply@github.com>2021-11-17 17:58:31 +0800
commit95d3266bee797cbeb7228d361fe32531737906d2 (patch)
tree68d90e7ef3e890c1ac311cf79f08cbe12a0dafca /models
parentadda27668b9764f97822e56e0591a6eb22165a83 (diff)
downloadgitea-95d3266bee797cbeb7228d361fe32531737906d2.tar.gz
gitea-95d3266bee797cbeb7228d361fe32531737906d2.zip
Move user follow and openid into models/user/ (#17613)
* Move UserRedirect into models/user/ * Fix lint & test * Fix lint * Fix lint * remove nolint comment * Fix lint * Move user follow and openid into models/user * Ignore the lint * Ignore the lint * Fix test * ignore stutters lint on UserOpenID
Diffstat (limited to 'models')
-rw-r--r--models/error.go15
-rw-r--r--models/statistic.go3
-rw-r--r--models/user.go37
-rw-r--r--models/user/follow.go (renamed from models/user_follow.go)2
-rw-r--r--models/user/follow_test.go22
-rw-r--r--models/user/main_test.go2
-rw-r--r--models/user/openid.go (renamed from models/user_openid.go)49
-rw-r--r--models/user/openid_test.go (renamed from models/user_openid_test.go)21
-rw-r--r--models/user_follow_test.go50
-rw-r--r--models/user_test.go48
10 files changed, 127 insertions, 122 deletions
diff --git a/models/error.go b/models/error.go
index db0fce0ce1..7d9b2ae65b 100644
--- a/models/error.go
+++ b/models/error.go
@@ -155,21 +155,6 @@ func (err ErrUserInactive) Error() string {
return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name)
}
-// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
-type ErrOpenIDAlreadyUsed struct {
- OpenID string
-}
-
-// IsErrOpenIDAlreadyUsed checks if an error is a ErrOpenIDAlreadyUsed.
-func IsErrOpenIDAlreadyUsed(err error) bool {
- _, ok := err.(ErrOpenIDAlreadyUsed)
- return ok
-}
-
-func (err ErrOpenIDAlreadyUsed) Error() string {
- return fmt.Sprintf("OpenID already in use [oid: %s]", err.OpenID)
-}
-
// ErrUserOwnRepos represents a "UserOwnRepos" kind of error.
type ErrUserOwnRepos struct {
UID int64
diff --git a/models/statistic.go b/models/statistic.go
index fab35e62dc..1849497cd9 100644
--- a/models/statistic.go
+++ b/models/statistic.go
@@ -7,6 +7,7 @@ package models
import (
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/login"
+ user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/setting"
)
@@ -92,7 +93,7 @@ func GetStatistic() (stats Statistic) {
stats.Counter.Comment, _ = e.Count(new(Comment))
stats.Counter.Oauth = 0
- stats.Counter.Follow, _ = e.Count(new(Follow))
+ stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
stats.Counter.Mirror, _ = e.Count(new(Mirror))
stats.Counter.Release, _ = e.Count(new(Release))
stats.Counter.LoginSource = login.CountSources()
diff --git a/models/user.go b/models/user.go
index 8146c184e7..e3cf94efe0 100644
--- a/models/user.go
+++ b/models/user.go
@@ -25,6 +25,7 @@ import (
"code.gitea.io/gitea/models/login"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/auth/openid"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
@@ -359,7 +360,7 @@ func (u *User) GetFollowers(listOptions db.ListOptions) ([]*User, error) {
// IsFollowing returns true if user is following followID.
func (u *User) IsFollowing(followID int64) bool {
- return IsFollowing(u.ID, followID)
+ return user_model.IsFollowing(u.ID, followID)
}
// GetFollowing returns range of user's following.
@@ -469,7 +470,7 @@ func (u *User) isVisibleToUser(e db.Engine, viewer *User) bool {
}
// If they follow - they see each over
- follower := IsFollowing(u.ID, viewer.ID)
+ follower := user_model.IsFollowing(u.ID, viewer.ID)
if follower {
return true
}
@@ -1212,12 +1213,12 @@ func deleteUser(e db.Engine, u *User) error {
&Access{UserID: u.ID},
&Watch{UserID: u.ID},
&Star{UID: u.ID},
- &Follow{UserID: u.ID},
- &Follow{FollowID: u.ID},
+ &user_model.Follow{UserID: u.ID},
+ &user_model.Follow{FollowID: u.ID},
&Action{UserID: u.ID},
&IssueUser{UID: u.ID},
&user_model.EmailAddress{UID: u.ID},
- &UserOpenID{UID: u.ID},
+ &user_model.UserOpenID{UID: u.ID},
&Reaction{UserID: u.ID},
&TeamUser{UID: u.ID},
&Collaboration{UserID: u.ID},
@@ -1798,3 +1799,29 @@ func IterateUser(f func(user *User) error) error {
}
}
}
+
+// GetUserByOpenID returns the user object by given OpenID if exists.
+func GetUserByOpenID(uri string) (*User, error) {
+ if len(uri) == 0 {
+ return nil, ErrUserNotExist{0, uri, 0}
+ }
+
+ uri, err := openid.Normalize(uri)
+ if err != nil {
+ return nil, err
+ }
+
+ log.Trace("Normalized OpenID URI: " + uri)
+
+ // Otherwise, check in openid table
+ oid := &user_model.UserOpenID{}
+ has, err := db.GetEngine(db.DefaultContext).Where("uri=?", uri).Get(oid)
+ if err != nil {
+ return nil, err
+ }
+ if has {
+ return GetUserByID(oid.UID)
+ }
+
+ return nil, ErrUserNotExist{0, uri, 0}
+}
diff --git a/models/user_follow.go b/models/user/follow.go
index 8832aa2f18..89675b5078 100644
--- a/models/user_follow.go
+++ b/models/user/follow.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
-package models
+package user
import (
"code.gitea.io/gitea/models/db"
diff --git a/models/user/follow_test.go b/models/user/follow_test.go
new file mode 100644
index 0000000000..538c7b18a2
--- /dev/null
+++ b/models/user/follow_test.go
@@ -0,0 +1,22 @@
+// Copyright 2020 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 user
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/unittest"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestIsFollowing(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ assert.True(t, IsFollowing(4, 2))
+ assert.False(t, IsFollowing(2, 4))
+ assert.False(t, IsFollowing(5, unittest.NonexistentID))
+ assert.False(t, IsFollowing(unittest.NonexistentID, 5))
+ assert.False(t, IsFollowing(unittest.NonexistentID, unittest.NonexistentID))
+}
diff --git a/models/user/main_test.go b/models/user/main_test.go
index 1dd9fb2781..7862556a03 100644
--- a/models/user/main_test.go
+++ b/models/user/main_test.go
@@ -15,5 +15,7 @@ func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."),
"email_address.yml",
"user_redirect.yml",
+ "follow.yml",
+ "user_open_id.yml",
)
}
diff --git a/models/user_openid.go b/models/user/openid.go
index 17a58536a2..8ca3c7f2c8 100644
--- a/models/user_openid.go
+++ b/models/user/openid.go
@@ -2,21 +2,21 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
-package models
+package user
import (
"errors"
+ "fmt"
"code.gitea.io/gitea/models/db"
- "code.gitea.io/gitea/modules/auth/openid"
- "code.gitea.io/gitea/modules/log"
)
// ErrOpenIDNotExist openid is not known
var ErrOpenIDNotExist = errors.New("OpenID is unknown")
// UserOpenID is the list of all OpenID identities of a user.
-type UserOpenID struct {
+// Since this is a middle table, name it OpenID is not suitable, so we ignore the lint here
+type UserOpenID struct { //revive:disable-line:exported
ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"INDEX NOT NULL"`
URI string `xorm:"UNIQUE NOT NULL"`
@@ -49,6 +49,21 @@ func isOpenIDUsed(e db.Engine, uri string) (bool, error) {
return e.Get(&UserOpenID{URI: uri})
}
+// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
+type ErrOpenIDAlreadyUsed struct {
+ OpenID string
+}
+
+// IsErrOpenIDAlreadyUsed checks if an error is a ErrOpenIDAlreadyUsed.
+func IsErrOpenIDAlreadyUsed(err error) bool {
+ _, ok := err.(ErrOpenIDAlreadyUsed)
+ return ok
+}
+
+func (err ErrOpenIDAlreadyUsed) Error() string {
+ return fmt.Sprintf("OpenID already in use [oid: %s]", err.OpenID)
+}
+
// NOTE: make sure openid.URI is normalized already
func addUserOpenID(e db.Engine, openid *UserOpenID) error {
used, err := isOpenIDUsed(e, openid.URI)
@@ -95,29 +110,3 @@ func ToggleUserOpenIDVisibility(id int64) (err error) {
_, err = db.GetEngine(db.DefaultContext).Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id)
return err
}
-
-// GetUserByOpenID returns the user object by given OpenID if exists.
-func GetUserByOpenID(uri string) (*User, error) {
- if len(uri) == 0 {
- return nil, ErrUserNotExist{0, uri, 0}
- }
-
- uri, err := openid.Normalize(uri)
- if err != nil {
- return nil, err
- }
-
- log.Trace("Normalized OpenID URI: " + uri)
-
- // Otherwise, check in openid table
- oid := &UserOpenID{}
- has, err := db.GetEngine(db.DefaultContext).Where("uri=?", uri).Get(oid)
- if err != nil {
- return nil, err
- }
- if has {
- return GetUserByID(oid.UID)
- }
-
- return nil, ErrUserNotExist{0, uri, 0}
-}
diff --git a/models/user_openid_test.go b/models/user/openid_test.go
index d0d801ad18..ba678ef864 100644
--- a/models/user_openid_test.go
+++ b/models/user/openid_test.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
-package models
+package user
import (
"testing"
@@ -30,25 +30,6 @@ func TestGetUserOpenIDs(t *testing.T) {
}
}
-func TestGetUserByOpenID(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- _, err := GetUserByOpenID("https://unknown")
- if assert.Error(t, err) {
- assert.True(t, IsErrUserNotExist(err))
- }
-
- user, err := GetUserByOpenID("https://user1.domain1.tld")
- if assert.NoError(t, err) {
- assert.Equal(t, int64(1), user.ID)
- }
-
- user, err = GetUserByOpenID("https://domain1.tld/user2/")
- if assert.NoError(t, err) {
- assert.Equal(t, int64(2), user.ID)
- }
-}
-
func TestToggleUserOpenIDVisibility(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
oids, err := GetUserOpenIDs(int64(2))
diff --git a/models/user_follow_test.go b/models/user_follow_test.go
deleted file mode 100644
index 5ba922728a..0000000000
--- a/models/user_follow_test.go
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright 2020 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 models
-
-import (
- "testing"
-
- "code.gitea.io/gitea/models/unittest"
- "github.com/stretchr/testify/assert"
-)
-
-func TestIsFollowing(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
- assert.True(t, IsFollowing(4, 2))
- assert.False(t, IsFollowing(2, 4))
- assert.False(t, IsFollowing(5, unittest.NonexistentID))
- assert.False(t, IsFollowing(unittest.NonexistentID, 5))
- assert.False(t, IsFollowing(unittest.NonexistentID, unittest.NonexistentID))
-}
-
-func TestFollowUser(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- testSuccess := func(followerID, followedID int64) {
- assert.NoError(t, FollowUser(followerID, followedID))
- unittest.AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID})
- }
- testSuccess(4, 2)
- testSuccess(5, 2)
-
- assert.NoError(t, FollowUser(2, 2))
-
- unittest.CheckConsistencyFor(t, &User{})
-}
-
-func TestUnfollowUser(t *testing.T) {
- assert.NoError(t, unittest.PrepareTestDatabase())
-
- testSuccess := func(followerID, followedID int64) {
- assert.NoError(t, UnfollowUser(followerID, followedID))
- unittest.AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID})
- }
- testSuccess(4, 2)
- testSuccess(5, 2)
- testSuccess(2, 2)
-
- unittest.CheckConsistencyFor(t, &User{})
-}
diff --git a/models/user_test.go b/models/user_test.go
index dc273ce1bc..4e2e521767 100644
--- a/models/user_test.go
+++ b/models/user_test.go
@@ -560,3 +560,51 @@ func TestNewUserRedirect3(t *testing.T) {
RedirectUserID: user.ID,
})
}
+
+func TestFollowUser(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ testSuccess := func(followerID, followedID int64) {
+ assert.NoError(t, user_model.FollowUser(followerID, followedID))
+ unittest.AssertExistsAndLoadBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
+ }
+ testSuccess(4, 2)
+ testSuccess(5, 2)
+
+ assert.NoError(t, user_model.FollowUser(2, 2))
+
+ unittest.CheckConsistencyFor(t, &User{})
+}
+
+func TestUnfollowUser(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ testSuccess := func(followerID, followedID int64) {
+ assert.NoError(t, user_model.UnfollowUser(followerID, followedID))
+ unittest.AssertNotExistsBean(t, &user_model.Follow{UserID: followerID, FollowID: followedID})
+ }
+ testSuccess(4, 2)
+ testSuccess(5, 2)
+ testSuccess(2, 2)
+
+ unittest.CheckConsistencyFor(t, &User{})
+}
+
+func TestGetUserByOpenID(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ _, err := GetUserByOpenID("https://unknown")
+ if assert.Error(t, err) {
+ assert.True(t, IsErrUserNotExist(err))
+ }
+
+ user, err := GetUserByOpenID("https://user1.domain1.tld")
+ if assert.NoError(t, err) {
+ assert.Equal(t, int64(1), user.ID)
+ }
+
+ user, err = GetUserByOpenID("https://domain1.tld/user2/")
+ if assert.NoError(t, err) {
+ assert.Equal(t, int64(2), user.ID)
+ }
+}