本站源代码
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.

121 lines
2.9KB

  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. // Star represents a starred repo by an user.
  6. type Star struct {
  7. ID int64 `xorm:"pk autoincr"`
  8. UID int64 `xorm:"UNIQUE(s)"`
  9. RepoID int64 `xorm:"UNIQUE(s)"`
  10. }
  11. // StarRepo or unstar repository.
  12. func StarRepo(userID, repoID int64, star bool) error {
  13. sess := x.NewSession()
  14. defer sess.Close()
  15. if err := sess.Begin(); err != nil {
  16. return err
  17. }
  18. if star {
  19. if isStaring(sess, userID, repoID) {
  20. return nil
  21. }
  22. if _, err := sess.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
  23. return err
  24. }
  25. if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
  26. return err
  27. }
  28. if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
  29. return err
  30. }
  31. } else {
  32. if !isStaring(sess, userID, repoID) {
  33. return nil
  34. }
  35. if _, err := sess.Delete(&Star{0, userID, repoID}); err != nil {
  36. return err
  37. }
  38. if _, err := sess.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
  39. return err
  40. }
  41. if _, err := sess.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID); err != nil {
  42. return err
  43. }
  44. }
  45. return sess.Commit()
  46. }
  47. // IsStaring checks if user has starred given repository.
  48. func IsStaring(userID, repoID int64) bool {
  49. return isStaring(x, userID, repoID)
  50. }
  51. func isStaring(e Engine, userID, repoID int64) bool {
  52. has, _ := e.Get(&Star{0, userID, repoID})
  53. return has
  54. }
  55. // GetStargazers returns the users that starred the repo.
  56. func (repo *Repository) GetStargazers(page int) ([]*User, error) {
  57. users := make([]*User, 0, ItemsPerPage)
  58. sess := x.Where("star.repo_id = ?", repo.ID).
  59. Join("LEFT", "star", "`user`.id = star.uid")
  60. if page > 0 {
  61. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  62. }
  63. return users, sess.Find(&users)
  64. }
  65. // GetStarredRepos returns the repos the user starred.
  66. func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos RepositoryList, err error) {
  67. if len(orderBy) == 0 {
  68. orderBy = "updated_unix DESC"
  69. }
  70. sess := x.
  71. Join("INNER", "star", "star.repo_id = repository.id").
  72. Where("star.uid = ?", u.ID).
  73. OrderBy(orderBy)
  74. if !private {
  75. sess = sess.And("is_private = ?", false)
  76. }
  77. if page <= 0 {
  78. page = 1
  79. }
  80. sess.Limit(pageSize, (page-1)*pageSize)
  81. repos = make([]*Repository, 0, pageSize)
  82. if err = sess.Find(&repos); err != nil {
  83. return
  84. }
  85. if err = repos.loadAttributes(x); err != nil {
  86. return
  87. }
  88. return
  89. }
  90. // GetStarredRepoCount returns the numbers of repo the user starred.
  91. func (u *User) GetStarredRepoCount(private bool) (int64, error) {
  92. sess := x.
  93. Join("INNER", "star", "star.repo_id = repository.id").
  94. Where("star.uid = ?", u.ID)
  95. if !private {
  96. sess = sess.And("is_private = ?", false)
  97. }
  98. return sess.Count(&Repository{})
  99. }
上海开阖软件有限公司 沪ICP备12045867号-1