Skip to content

Commit 3803f25

Browse files
ethantkoeniglunny
authored andcommitted
Move user_follow to separate file (#1210)
Also add unit tests
1 parent 1e3548b commit 3803f25

File tree

5 files changed

+123
-72
lines changed

5 files changed

+123
-72
lines changed

models/fixtures/follow.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-
2+
id: 1
3+
user_id: 4
4+
follow_id: 2

models/fixtures/user.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
avatar_email: [email protected]
2727
num_repos: 2
2828
num_stars: 2
29+
num_followers: 1
2930

3031
-
3132
id: 3
@@ -56,6 +57,7 @@
5657
avatar: avatar4
5758
avatar_email: [email protected]
5859
num_repos: 0
60+
num_following: 1
5961

6062
-
6163
id: 5
@@ -72,6 +74,7 @@
7274
num_repos: 1
7375
allow_create_organization: false
7476
is_active: true
77+
num_following: 0
7578

7679
-
7780
id: 6

models/user.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,78 +1292,6 @@ func SearchUserByName(opts *SearchUserOptions) (users []*User, _ int64, _ error)
12921292
return users, count, sess.Find(&users)
12931293
}
12941294

1295-
// ___________ .__ .__
1296-
// \_ _____/___ | | | | ______ _ __
1297-
// | __)/ _ \| | | | / _ \ \/ \/ /
1298-
// | \( <_> ) |_| |_( <_> ) /
1299-
// \___ / \____/|____/____/\____/ \/\_/
1300-
// \/
1301-
1302-
// Follow represents relations of user and his/her followers.
1303-
type Follow struct {
1304-
ID int64 `xorm:"pk autoincr"`
1305-
UserID int64 `xorm:"UNIQUE(follow)"`
1306-
FollowID int64 `xorm:"UNIQUE(follow)"`
1307-
}
1308-
1309-
// IsFollowing returns true if user is following followID.
1310-
func IsFollowing(userID, followID int64) bool {
1311-
has, _ := x.Get(&Follow{UserID: userID, FollowID: followID})
1312-
return has
1313-
}
1314-
1315-
// FollowUser marks someone be another's follower.
1316-
func FollowUser(userID, followID int64) (err error) {
1317-
if userID == followID || IsFollowing(userID, followID) {
1318-
return nil
1319-
}
1320-
1321-
sess := x.NewSession()
1322-
defer sessionRelease(sess)
1323-
if err = sess.Begin(); err != nil {
1324-
return err
1325-
}
1326-
1327-
if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil {
1328-
return err
1329-
}
1330-
1331-
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
1332-
return err
1333-
}
1334-
1335-
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
1336-
return err
1337-
}
1338-
return sess.Commit()
1339-
}
1340-
1341-
// UnfollowUser unmarks someone as another's follower.
1342-
func UnfollowUser(userID, followID int64) (err error) {
1343-
if userID == followID || !IsFollowing(userID, followID) {
1344-
return nil
1345-
}
1346-
1347-
sess := x.NewSession()
1348-
defer sessionRelease(sess)
1349-
if err = sess.Begin(); err != nil {
1350-
return err
1351-
}
1352-
1353-
if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil {
1354-
return err
1355-
}
1356-
1357-
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
1358-
return err
1359-
}
1360-
1361-
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
1362-
return err
1363-
}
1364-
return sess.Commit()
1365-
}
1366-
13671295
// GetStarredRepos returns the repos starred by a particular user
13681296
func GetStarredRepos(userID int64, private bool) ([]*Repository, error) {
13691297
sess := x.Where("star.uid=?", userID).

models/user_follow.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
// Follow represents relations of user and his/her followers.
8+
type Follow struct {
9+
ID int64 `xorm:"pk autoincr"`
10+
UserID int64 `xorm:"UNIQUE(follow)"`
11+
FollowID int64 `xorm:"UNIQUE(follow)"`
12+
}
13+
14+
// IsFollowing returns true if user is following followID.
15+
func IsFollowing(userID, followID int64) bool {
16+
has, _ := x.Get(&Follow{UserID: userID, FollowID: followID})
17+
return has
18+
}
19+
20+
// FollowUser marks someone be another's follower.
21+
func FollowUser(userID, followID int64) (err error) {
22+
if userID == followID || IsFollowing(userID, followID) {
23+
return nil
24+
}
25+
26+
sess := x.NewSession()
27+
defer sessionRelease(sess)
28+
if err = sess.Begin(); err != nil {
29+
return err
30+
}
31+
32+
if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil {
33+
return err
34+
}
35+
36+
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
37+
return err
38+
}
39+
40+
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
41+
return err
42+
}
43+
return sess.Commit()
44+
}
45+
46+
// UnfollowUser unmarks someone as another's follower.
47+
func UnfollowUser(userID, followID int64) (err error) {
48+
if userID == followID || !IsFollowing(userID, followID) {
49+
return nil
50+
}
51+
52+
sess := x.NewSession()
53+
defer sessionRelease(sess)
54+
if err = sess.Begin(); err != nil {
55+
return err
56+
}
57+
58+
if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil {
59+
return err
60+
}
61+
62+
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
63+
return err
64+
}
65+
66+
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
67+
return err
68+
}
69+
return sess.Commit()
70+
}
71+

models/user_follow_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package models
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsFollowing(t *testing.T) {
10+
assert.NoError(t, PrepareTestDatabase())
11+
assert.True(t, IsFollowing(4, 2))
12+
assert.False(t, IsFollowing(2, 4))
13+
assert.False(t, IsFollowing(5, NonexistentID))
14+
assert.False(t, IsFollowing(NonexistentID, 5))
15+
assert.False(t, IsFollowing(NonexistentID, NonexistentID))
16+
}
17+
18+
func TestFollowUser(t *testing.T) {
19+
assert.NoError(t, PrepareTestDatabase())
20+
21+
testSuccess := func(followerID, followedID int64) {
22+
assert.NoError(t, FollowUser(followerID, followedID))
23+
AssertExistsAndLoadBean(t, &Follow{UserID: followerID, FollowID: followedID})
24+
}
25+
testSuccess(4, 2)
26+
testSuccess(5, 2)
27+
28+
assert.NoError(t, FollowUser(2, 2))
29+
30+
CheckConsistencyFor(t, &User{})
31+
}
32+
33+
func TestUnfollowUser(t *testing.T) {
34+
assert.NoError(t, PrepareTestDatabase())
35+
36+
testSuccess := func(followerID, followedID int64) {
37+
assert.NoError(t, UnfollowUser(followerID, followedID))
38+
AssertNotExistsBean(t, &Follow{UserID: followerID, FollowID: followedID})
39+
}
40+
testSuccess(4, 2)
41+
testSuccess(5, 2)
42+
testSuccess(2, 2)
43+
44+
CheckConsistencyFor(t, &User{})
45+
}

0 commit comments

Comments
 (0)