diff options
author | Unknwon <u@gogs.io> | 2015-12-21 04:24:11 -0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-12-21 04:24:11 -0800 |
commit | a49af93fafe824cdd375318d15de420b3ed61e79 (patch) | |
tree | 1508c0bbd9f9caf2c3af8163e4e9abe326093558 /models | |
parent | c62a6b7a1238524225ec9c214dc5eac7da017663 (diff) | |
download | gitea-a49af93fafe824cdd375318d15de420b3ed61e79.tar.gz gitea-a49af93fafe824cdd375318d15de420b3ed61e79.zip |
#1692 APIs: Users Followers
- User profile un/follow
- List user's followers/following
Diffstat (limited to 'models')
-rw-r--r-- | models/issue.go | 41 | ||||
-rw-r--r-- | models/user.go | 145 |
2 files changed, 114 insertions, 72 deletions
diff --git a/models/issue.go b/models/issue.go index 2b2e8bd618..22ea72512c 100644 --- a/models/issue.go +++ b/models/issue.go @@ -665,6 +665,47 @@ func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int return ius, err } +func UpdateMentions(userNames []string, issueId int64) error { + for i := range userNames { + userNames[i] = strings.ToLower(userNames[i]) + } + users := make([]*User, 0, len(userNames)) + + if err := x.Where("lower_name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("lower_name ASC").Find(&users); err != nil { + return err + } + + ids := make([]int64, 0, len(userNames)) + for _, user := range users { + ids = append(ids, user.Id) + if !user.IsOrganization() { + continue + } + + if user.NumMembers == 0 { + continue + } + + tempIds := make([]int64, 0, user.NumMembers) + orgUsers, err := GetOrgUsersByOrgId(user.Id) + if err != nil { + return err + } + + for _, orgUser := range orgUsers { + tempIds = append(tempIds, orgUser.ID) + } + + ids = append(ids, tempIds...) + } + + if err := UpdateIssueUsersByMentions(ids, issueId); err != nil { + return err + } + + return nil +} + // IssueStats represents issue statistic information. type IssueStats struct { OpenCount, ClosedCount int64 diff --git a/models/user.go b/models/user.go index ebdc3c47fe..1a8500ac76 100644 --- a/models/user.go +++ b/models/user.go @@ -56,7 +56,7 @@ type User struct { LowerName string `xorm:"UNIQUE NOT NULL"` Name string `xorm:"UNIQUE NOT NULL"` FullName string - // Email is the primary email address (to be used for communication). + // Email is the primary email address (to be used for communication) Email string `xorm:"NOT NULL"` Passwd string `xorm:"NOT NULL"` LoginType LoginType @@ -78,24 +78,24 @@ type User struct { // Maximum repository creation limit, -1 means use gloabl default MaxRepoCreation int `xorm:"NOT NULL DEFAULT -1"` - // Permissions. + // Permissions IsActive bool IsAdmin bool AllowGitHook bool AllowImportLocal bool // Allow migrate repository by local path - // Avatar. + // Avatar Avatar string `xorm:"VARCHAR(2048) NOT NULL"` AvatarEmail string `xorm:"NOT NULL"` UseCustomAvatar bool - // Counters. - NumFollowers int - NumFollowings int - NumStars int - NumRepos int + // Counters + NumFollowers int + NumFollowing int `xorm:"NOT NULL"` + NumStars int + NumRepos int - // For organization. + // For organization Description string NumTeams int NumMembers int @@ -263,6 +263,34 @@ func (u *User) AvatarLink() string { return link } +// User.GetFollwoers returns range of user's followers. +func (u *User) GetFollowers(page int) ([]*User, error) { + users := make([]*User, 0, ItemsPerPage) + sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.follow_id=?", u.Id) + if setting.UsePostgreSQL { + sess = sess.Join("LEFT", "follow", `"user".id=follow.user_id`) + } else { + sess = sess.Join("LEFT", "follow", "user.id=follow.user_id") + } + return users, sess.Find(&users) +} + +func (u *User) IsFollowing(followID int64) bool { + return IsFollowing(u.Id, followID) +} + +// GetFollowing returns range of user's following. +func (u *User) GetFollowing(page int) ([]*User, error) { + users := make([]*User, 0, ItemsPerPage) + sess := x.Limit(ItemsPerPage, (page-1)*ItemsPerPage).Where("follow.user_id=?", u.Id) + if setting.UsePostgreSQL { + sess = sess.Join("LEFT", "follow", `"user".id=follow.follow_id`) + } else { + sess = sess.Join("LEFT", "follow", "user.id=follow.follow_id") + } + return users, sess.Find(&users) +} + // NewGitSig generates and returns the signature of given user. func (u *User) NewGitSig() *git.Signature { return &git.Signature{ @@ -1077,100 +1105,73 @@ func SearchUserByName(opt SearchOption) (us []*User, err error) { return us, err } -// Follow is connection request for receiving user notification. +// ___________ .__ .__ +// \_ _____/___ | | | | ______ _ __ +// | __)/ _ \| | | | / _ \ \/ \/ / +// | \( <_> ) |_| |_( <_> ) / +// \___ / \____/|____/____/\____/ \/\_/ +// \/ + +// Follow represents relations of user and his/her followers. type Follow struct { ID int64 `xorm:"pk autoincr"` UserID int64 `xorm:"UNIQUE(follow)"` FollowID int64 `xorm:"UNIQUE(follow)"` } +func IsFollowing(userID, followID int64) bool { + has, _ := x.Get(&Follow{UserID: userID, FollowID: followID}) + return has +} + // FollowUser marks someone be another's follower. -func FollowUser(userId int64, followId int64) (err error) { +func FollowUser(userID, followID int64) (err error) { + if userID == followID || IsFollowing(userID, followID) { + return nil + } + sess := x.NewSession() - defer sess.Close() - sess.Begin() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } - if _, err = sess.Insert(&Follow{UserID: userId, FollowID: followId}); err != nil { - sess.Rollback() + if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil { return err } - rawSql := "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, followId); err != nil { - sess.Rollback() + if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil { return err } - rawSql = "UPDATE `user` SET num_followings = num_followings + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, userId); err != nil { - sess.Rollback() + if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil { return err } return sess.Commit() } -// UnFollowUser unmarks someone be another's follower. -func UnFollowUser(userId int64, unFollowId int64) (err error) { - session := x.NewSession() - defer session.Close() - session.Begin() - - if _, err = session.Delete(&Follow{UserID: userId, FollowID: unFollowId}); err != nil { - session.Rollback() - return err +// UnfollowUser unmarks someone be another's follower. +func UnfollowUser(userID, followID int64) (err error) { + if userID == followID || !IsFollowing(userID, followID) { + return nil } - rawSql := "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?" - if _, err = session.Exec(rawSql, unFollowId); err != nil { - session.Rollback() + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { return err } - rawSql = "UPDATE `user` SET num_followings = num_followings - 1 WHERE id = ?" - if _, err = session.Exec(rawSql, userId); err != nil { - session.Rollback() + if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil { return err } - return session.Commit() -} -func UpdateMentions(userNames []string, issueId int64) error { - for i := range userNames { - userNames[i] = strings.ToLower(userNames[i]) - } - users := make([]*User, 0, len(userNames)) - - if err := x.Where("lower_name IN (?)", strings.Join(userNames, "\",\"")).OrderBy("lower_name ASC").Find(&users); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil { return err } - ids := make([]int64, 0, len(userNames)) - for _, user := range users { - ids = append(ids, user.Id) - if !user.IsOrganization() { - continue - } - - if user.NumMembers == 0 { - continue - } - - tempIds := make([]int64, 0, user.NumMembers) - orgUsers, err := GetOrgUsersByOrgId(user.Id) - if err != nil { - return err - } - - for _, orgUser := range orgUsers { - tempIds = append(tempIds, orgUser.ID) - } - - ids = append(ids, tempIds...) - } - - if err := UpdateIssueUsersByMentions(ids, issueId); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil { return err } - - return nil + return sess.Commit() } |