You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.2 KiB
81 lines
2.2 KiB
8 years ago
|
// Copyright 2017 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.
|
||
|
|
||
3 years ago
|
package user
|
||
8 years ago
|
|
||
4 years ago
|
import (
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
|
)
|
||
|
|
||
8 years ago
|
// Follow represents relations of user and his/her followers.
|
||
|
type Follow struct {
|
||
4 years ago
|
ID int64 `xorm:"pk autoincr"`
|
||
|
UserID int64 `xorm:"UNIQUE(follow)"`
|
||
|
FollowID int64 `xorm:"UNIQUE(follow)"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(Follow))
|
||
|
}
|
||
|
|
||
8 years ago
|
// IsFollowing returns true if user is following followID.
|
||
|
func IsFollowing(userID, followID int64) bool {
|
||
3 years ago
|
has, _ := db.GetEngine(db.DefaultContext).Get(&Follow{UserID: userID, FollowID: followID})
|
||
8 years ago
|
return has
|
||
|
}
|
||
|
|
||
|
// FollowUser marks someone be another's follower.
|
||
|
func FollowUser(userID, followID int64) (err error) {
|
||
|
if userID == followID || IsFollowing(userID, followID) {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
sess := db.NewSession(db.DefaultContext)
|
||
8 years ago
|
defer sess.Close()
|
||
8 years ago
|
if err = sess.Begin(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
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 as another's follower.
|
||
|
func UnfollowUser(userID, followID int64) (err error) {
|
||
|
if userID == followID || !IsFollowing(userID, followID) {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
sess := db.NewSession(db.DefaultContext)
|
||
8 years ago
|
defer sess.Close()
|
||
8 years ago
|
if err = sess.Begin(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return sess.Commit()
|
||
|
}
|