2016-12-29 20:28:24 +05:30
|
|
|
// Copyright 2016 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.
|
|
|
|
|
2021-12-12 21:18:20 +05:30
|
|
|
package repo
|
2016-12-29 20:28:24 +05:30
|
|
|
|
2020-10-13 05:31:57 +05:30
|
|
|
import (
|
2022-05-20 19:38:52 +05:30
|
|
|
"context"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-10-13 05:31:57 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
)
|
|
|
|
|
2016-12-29 20:28:24 +05:30
|
|
|
// Star represents a starred repo by an user.
|
|
|
|
type Star struct {
|
2020-10-13 05:31:57 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2016-12-29 20:28:24 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Star))
|
|
|
|
}
|
|
|
|
|
2016-12-29 20:28:24 +05:30
|
|
|
// StarRepo or unstar repository.
|
|
|
|
func StarRepo(userID, repoID int64, star bool) error {
|
2021-11-21 21:11:00 +05:30
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
defer committer.Close()
|
2022-05-20 19:38:52 +05:30
|
|
|
staring := IsStaring(ctx, userID, repoID)
|
2016-12-29 20:28:24 +05:30
|
|
|
|
|
|
|
if star {
|
2021-11-21 21:11:00 +05:30
|
|
|
if staring {
|
2016-12-29 20:28:24 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
if err := db.Insert(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-21 21:11:00 +05:30
|
|
|
if !staring {
|
2016-12-29 20:28:24 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
return committer.Commit()
|
2016-12-29 20:28:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsStaring checks if user has starred given repository.
|
2022-05-20 19:38:52 +05:30
|
|
|
func IsStaring(ctx context.Context, userID, repoID int64) bool {
|
|
|
|
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
|
2016-12-29 20:28:24 +05:30
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStargazers returns the users that starred the repo.
|
2021-12-12 21:18:20 +05:30
|
|
|
func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID).
|
2017-01-06 12:35:09 +05:30
|
|
|
Join("LEFT", "star", "`user`.id = star.uid")
|
2020-01-25 00:30:29 +05:30
|
|
|
if opts.Page > 0 {
|
2021-09-24 17:02:56 +05:30
|
|
|
sess = db.SetSessionPagination(sess, &opts)
|
2020-01-25 00:30:29 +05:30
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
users := make([]*user_model.User, 0, opts.PageSize)
|
2020-01-25 00:30:29 +05:30
|
|
|
return users, sess.Find(&users)
|
2017-01-06 12:35:09 +05:30
|
|
|
}
|
2020-01-25 00:30:29 +05:30
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
users := make([]*user_model.User, 0, 8)
|
2017-01-06 12:35:09 +05:30
|
|
|
return users, sess.Find(&users)
|
2016-12-29 20:28:24 +05:30
|
|
|
}
|