2017-03-11 02:46:53 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 12:20:29 -06:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-03-11 02:46:53 -06:00
|
|
|
|
2021-11-17 03:58:31 -06:00
|
|
|
package user
|
2017-03-11 02:46:53 -06:00
|
|
|
|
2020-10-12 19:01:57 -05:00
|
|
|
import (
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2020-10-12 19:01:57 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
2022-06-25 17:50:12 -05:00
|
|
|
// Follow represents relations of user and their followers.
|
2017-03-11 02:46:53 -06:00
|
|
|
type Follow struct {
|
2020-10-12 19:01:57 -05: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 02:46:53 -06:00
|
|
|
}
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Follow))
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:46:53 -06:00
|
|
|
// IsFollowing returns true if user is following followID.
|
|
|
|
func IsFollowing(userID, followID int64) bool {
|
2021-09-23 10:45:36 -05:00
|
|
|
has, _ := db.GetEngine(db.DefaultContext).Get(&Follow{UserID: userID, FollowID: followID})
|
2017-03-11 02:46:53 -06: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
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:18:50 -06:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2021-11-21 09:41:00 -06:00
|
|
|
if err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 09:41:00 -06:00
|
|
|
defer committer.Close()
|
2017-03-11 02:46:53 -06:00
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if err = db.Insert(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 09:41:00 -06:00
|
|
|
return committer.Commit()
|
2017-03-11 02:46:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnfollowUser unmarks someone as another's follower.
|
|
|
|
func UnfollowUser(userID, followID int64) (err error) {
|
|
|
|
if userID == followID || !IsFollowing(userID, followID) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-12 14:18:50 -06:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2021-11-21 09:41:00 -06:00
|
|
|
if err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 09:41:00 -06:00
|
|
|
defer committer.Close()
|
2017-03-11 02:46:53 -06:00
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 09:41:00 -06:00
|
|
|
if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil {
|
2017-03-11 02:46:53 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 09:41:00 -06:00
|
|
|
return committer.Commit()
|
2017-03-11 02:46:53 -06:00
|
|
|
}
|