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.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
2020-10-13 05:31:57 +05:30
|
|
|
import (
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
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-09-19 17:19:59 +05:30
|
|
|
sess := db.DefaultContext().NewSession()
|
2016-12-29 20:28:24 +05:30
|
|
|
defer sess.Close()
|
|
|
|
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if star {
|
2019-01-14 07:59:58 +05:30
|
|
|
if isStaring(sess, userID, repoID) {
|
2016-12-29 20:28:24 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-14 07:59:58 +05:30
|
|
|
if !isStaring(sess, userID, repoID) {
|
2016-12-29 20:28:24 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-13 05:31:57 +05:30
|
|
|
if _, err := sess.Delete(&Star{UID: userID, RepoID: repoID}); err != nil {
|
2016-12-29 20:28:24 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsStaring checks if user has starred given repository.
|
|
|
|
func IsStaring(userID, repoID int64) bool {
|
2021-09-19 17:19:59 +05:30
|
|
|
return isStaring(db.DefaultContext().Engine(), userID, repoID)
|
2019-01-14 07:59:58 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func isStaring(e db.Engine, userID, repoID int64) bool {
|
2020-10-13 05:31:57 +05:30
|
|
|
has, _ := e.Get(&Star{UID: userID, RepoID: repoID})
|
2016-12-29 20:28:24 +05:30
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStargazers returns the users that starred the repo.
|
2020-01-25 00:30:29 +05:30
|
|
|
func (repo *Repository) GetStargazers(opts ListOptions) ([]*User, error) {
|
2021-09-19 17:19:59 +05:30
|
|
|
sess := db.DefaultContext().Engine().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-14 23:18:27 +05:30
|
|
|
sess = setSessionPagination(sess, &opts)
|
2020-01-25 00:30:29 +05:30
|
|
|
|
|
|
|
users := make([]*User, 0, opts.PageSize)
|
|
|
|
return users, sess.Find(&users)
|
2017-01-06 12:35:09 +05:30
|
|
|
}
|
2020-01-25 00:30:29 +05:30
|
|
|
|
|
|
|
users := make([]*User, 0, 8)
|
2017-01-06 12:35:09 +05:30
|
|
|
return users, sess.Find(&users)
|
2016-12-29 20:28:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetStarredRepos returns the repos the user starred.
|
2017-02-15 11:31:50 +05:30
|
|
|
func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
|
2017-02-07 17:24:16 +05:30
|
|
|
if len(orderBy) == 0 {
|
2017-02-14 12:58:22 +05:30
|
|
|
orderBy = "updated_unix DESC"
|
2017-02-07 17:24:16 +05:30
|
|
|
}
|
2021-09-19 17:19:59 +05:30
|
|
|
sess := db.DefaultContext().Engine().
|
2016-12-29 20:28:24 +05:30
|
|
|
Join("INNER", "star", "star.repo_id = repository.id").
|
2017-02-03 09:33:49 +05:30
|
|
|
Where("star.uid = ?", u.ID).
|
2017-02-14 12:58:22 +05:30
|
|
|
OrderBy(orderBy)
|
2016-12-29 20:28:24 +05:30
|
|
|
|
|
|
|
if !private {
|
|
|
|
sess = sess.And("is_private = ?", false)
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:24:16 +05:30
|
|
|
if page <= 0 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
sess.Limit(pageSize, (page-1)*pageSize)
|
|
|
|
|
|
|
|
repos = make([]*Repository, 0, pageSize)
|
|
|
|
|
2017-01-24 08:09:58 +05:30
|
|
|
if err = sess.Find(&repos); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
if err = repos.loadAttributes(db.DefaultContext().Engine()); err != nil {
|
2017-02-15 11:31:50 +05:30
|
|
|
return
|
2017-01-24 08:09:58 +05:30
|
|
|
}
|
|
|
|
|
2016-12-29 20:28:24 +05:30
|
|
|
return
|
|
|
|
}
|
2017-02-07 17:24:16 +05:30
|
|
|
|
|
|
|
// GetStarredRepoCount returns the numbers of repo the user starred.
|
|
|
|
func (u *User) GetStarredRepoCount(private bool) (int64, error) {
|
2021-09-19 17:19:59 +05:30
|
|
|
sess := db.DefaultContext().Engine().
|
2017-02-07 17:24:16 +05:30
|
|
|
Join("INNER", "star", "star.repo_id = repository.id").
|
|
|
|
Where("star.uid = ?", u.ID)
|
|
|
|
|
|
|
|
if !private {
|
|
|
|
sess = sess.And("is_private = ?", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Count(&Repository{})
|
|
|
|
}
|