2017-03-11 08:46:53 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
2020-10-13 00:01:57 +00:00
|
|
|
import (
|
2021-09-19 11:49:59 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2020-10-13 00:01:57 +00:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
2017-03-11 08:46:53 +00:00
|
|
|
// Follow represents relations of user and his/her followers.
|
|
|
|
type Follow struct {
|
2020-10-13 00:01:57 +00:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UserID int64 `xorm:"UNIQUE(follow)"`
|
|
|
|
FollowID int64 `xorm:"UNIQUE(follow)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2017-03-11 08:46:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Follow))
|
|
|
|
}
|
|
|
|
|
2017-03-11 08:46:53 +00:00
|
|
|
// IsFollowing returns true if user is following followID.
|
|
|
|
func IsFollowing(userID, followID int64) bool {
|
2021-09-19 11:49:59 +00:00
|
|
|
has, _ := db.DefaultContext().Engine().Get(&Follow{UserID: userID, FollowID: followID})
|
2017-03-11 08:46:53 +00:00
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// FollowUser marks someone be another's follower.
|
|
|
|
func FollowUser(userID, followID int64) (err error) {
|
|
|
|
if userID == followID || IsFollowing(userID, followID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
sess := db.DefaultContext().NewSession()
|
2017-06-21 00:57:05 +00:00
|
|
|
defer sess.Close()
|
2017-03-11 08:46:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:49:59 +00:00
|
|
|
sess := db.DefaultContext().NewSession()
|
2017-06-21 00:57:05 +00:00
|
|
|
defer sess.Close()
|
2017-03-11 08:46:53 +00:00
|
|
|
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()
|
|
|
|
}
|