summaryrefslogtreecommitdiffstats
path: root/models/repo/star.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-12 23:48:20 +0800
committerGitHub <noreply@github.com>2021-12-12 23:48:20 +0800
commit572324049008ac803d3d7c17a7b3a81ef00386fc (patch)
treea9c3709643a23165d27aa67e76f10f26a89936d5 /models/repo/star.go
parent0a7e8327a017c5dd43e552bbcd0d0f056bc1671b (diff)
downloadgitea-572324049008ac803d3d7c17a7b3a81ef00386fc.tar.gz
gitea-572324049008ac803d3d7c17a7b3a81ef00386fc.zip
Some repository refactors (#17950)
* some repository refactors * remove unnecessary code * Fix test * Remove unnecessary banner
Diffstat (limited to 'models/repo/star.go')
-rw-r--r--models/repo/star.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/models/repo/star.go b/models/repo/star.go
new file mode 100644
index 0000000000..8db297e3b4
--- /dev/null
+++ b/models/repo/star.go
@@ -0,0 +1,90 @@
+// 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 repo
+
+import (
+ "code.gitea.io/gitea/models/db"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/timeutil"
+)
+
+// Star represents a starred repo by an user.
+type Star struct {
+ ID int64 `xorm:"pk autoincr"`
+ UID int64 `xorm:"UNIQUE(s)"`
+ RepoID int64 `xorm:"UNIQUE(s)"`
+ CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
+}
+
+func init() {
+ db.RegisterModel(new(Star))
+}
+
+// StarRepo or unstar repository.
+func StarRepo(userID, repoID int64, star bool) error {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
+ return err
+ }
+ defer committer.Close()
+ staring := isStaring(db.GetEngine(ctx), userID, repoID)
+
+ if star {
+ if staring {
+ return nil
+ }
+
+ if err := db.Insert(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
+ return err
+ }
+ if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
+ return err
+ }
+ if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
+ return err
+ }
+ } else {
+ if !staring {
+ return nil
+ }
+
+ if _, err := db.DeleteByBean(ctx, &Star{UID: userID, RepoID: repoID}); err != nil {
+ return err
+ }
+ if _, err := db.Exec(ctx, "UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
+ return err
+ }
+ if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
+ return err
+ }
+ }
+
+ return committer.Commit()
+}
+
+// IsStaring checks if user has starred given repository.
+func IsStaring(userID, repoID int64) bool {
+ return isStaring(db.GetEngine(db.DefaultContext), userID, repoID)
+}
+
+func isStaring(e db.Engine, userID, repoID int64) bool {
+ has, _ := e.Get(&Star{UID: userID, RepoID: repoID})
+ return has
+}
+
+// GetStargazers returns the users that starred the repo.
+func GetStargazers(repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
+ sess := db.GetEngine(db.DefaultContext).Where("star.repo_id = ?", repo.ID).
+ Join("LEFT", "star", "`user`.id = star.uid")
+ if opts.Page > 0 {
+ sess = db.SetSessionPagination(sess, &opts)
+
+ users := make([]*user_model.User, 0, opts.PageSize)
+ return users, sess.Find(&users)
+ }
+
+ users := make([]*user_model.User, 0, 8)
+ return users, sess.Find(&users)
+}