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.

116 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 RepositoryList, err error) {
  64. if len(orderBy) == 0 {
  65. orderBy = "updated_unix DESC"
  66. }
  67. sess := x.
  68. Join("INNER", "star", "star.repo_id = repository.id").
  69. Where("star.uid = ?", u.ID).
  70. OrderBy(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. if err = repos.loadAttributes(x); err != nil {
  83. return
  84. }
  85. return
  86. }
  87. // GetStarredRepoCount returns the numbers of repo the user starred.
  88. func (u *User) GetStarredRepoCount(private bool) (int64, error) {
  89. sess := x.
  90. Join("INNER", "star", "star.repo_id = repository.id").
  91. Where("star.uid = ?", u.ID)
  92. if !private {
  93. sess = sess.And("is_private = ?", false)
  94. }
  95. return sess.Count(&Repository{})
  96. }