mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
91 lines
2.5 KiB
91 lines
2.5 KiB
8 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package repo
|
||
8 years ago
|
|
||
4 years ago
|
import (
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
|
)
|
||
|
|
||
8 years ago
|
// Star represents a starred repo by an user.
|
||
|
type Star struct {
|
||
4 years ago
|
ID int64 `xorm:"pk autoincr"`
|
||
|
UID int64 `xorm:"UNIQUE(s)"`
|
||
|
RepoID int64 `xorm:"UNIQUE(s)"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||
8 years ago
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(Star))
|
||
|
}
|
||
|
|
||
8 years ago
|
// StarRepo or unstar repository.
|
||
|
func StarRepo(userID, repoID int64, star bool) error {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
|
staring := isStaring(db.GetEngine(ctx), userID, repoID)
|
||
8 years ago
|
|
||
|
if star {
|
||
3 years ago
|
if staring {
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
if err := db.Insert(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
} else {
|
||
3 years ago
|
if !staring {
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
|
||
8 years ago
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return committer.Commit()
|
||
8 years ago
|
}
|
||
|
|
||
|
// IsStaring checks if user has starred given repository.
|
||
|
func IsStaring(userID, repoID int64) bool {
|
||
3 years ago
|
return isStaring(db.GetEngine(db.DefaultContext), userID, repoID)
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
func isStaring(e db.Engine, userID, repoID int64) bool {
|
||
4 years ago
|
has, _ := e.Get(&Star{UID: userID, RepoID: repoID})
|
||
8 years ago
|
return has
|
||
|
}
|
||
|
|
||
|
// GetStargazers returns the users that starred the repo.
|
||
3 years ago
|
func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
|
||
3 years ago
|
sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID).
|
||
8 years ago
|
Join("LEFT", "star", "`user`.id = star.uid")
|
||
5 years ago
|
if opts.Page > 0 {
|
||
3 years ago
|
sess = db.SetSessionPagination(sess, &opts)
|
||
5 years ago
|
|
||
3 years ago
|
users := make([]*user_model.User, 0, opts.PageSize)
|
||
5 years ago
|
return users, sess.Find(&users)
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
3 years ago
|
users := make([]*user_model.User, 0, 8)
|
||
8 years ago
|
return users, sess.Find(&users)
|
||
8 years ago
|
}
|