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.

118 lines
2.8 KiB

  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(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(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. has, _ := x.Get(&Star{0, userID, repoID})
  50. return has
  51. }
  52. // GetStargazers returns the users that starred the repo.
  53. func (repo *Repository) GetStargazers(page int) ([]*User, error) {
  54. users := make([]*User, 0, ItemsPerPage)
  55. sess := x.Where("star.repo_id = ?", repo.ID).
  56. Join("LEFT", "star", "`user`.id = star.uid")
  57. if page > 0 {
  58. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  59. }
  60. return users, sess.Find(&users)
  61. }
  62. // GetStarredRepos returns the repos the user starred.
  63. func (u *User) GetStarredRepos(private bool, page, pageSize int, orderBy string) (repos []*Repository, err error) {
  64. if len(orderBy) == 0 {
  65. orderBy = "star.id"
  66. }
  67. sess := x.
  68. Join("INNER", "star", "star.repo_id = repository.id").
  69. Where("star.uid = ?", u.ID).
  70. Desc(orderBy)
  71. if !private {
  72. sess = sess.And("is_private = ?", false)
  73. }
  74. if page <= 0 {
  75. page = 1
  76. }
  77. sess.Limit(pageSize, (page-1)*pageSize)
  78. repos = make([]*Repository, 0, pageSize)
  79. if err = sess.Find(&repos); err != nil {
  80. return
  81. }
  82. for _, repo := range repos {
  83. if err = repo.GetOwner(); err != nil {
  84. return
  85. }
  86. }
  87. return
  88. }
  89. // GetStarredRepoCount returns the numbers of repo the user starred.
  90. func (u *User) GetStarredRepoCount(private bool) (int64, error) {
  91. sess := x.
  92. Join("INNER", "star", "star.repo_id = repository.id").
  93. Where("star.uid = ?", u.ID)
  94. if !private {
  95. sess = sess.And("is_private = ?", false)
  96. }
  97. return sess.Count(&Repository{})
  98. }