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

140 lines
3.9KB

  1. // Copyright 2017 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. import "fmt"
  6. // Watch is connection request for receiving repository notification.
  7. type Watch struct {
  8. ID int64 `xorm:"pk autoincr"`
  9. UserID int64 `xorm:"UNIQUE(watch)"`
  10. RepoID int64 `xorm:"UNIQUE(watch)"`
  11. }
  12. func isWatching(e Engine, userID, repoID int64) bool {
  13. has, _ := e.Get(&Watch{UserID: userID, RepoID: repoID})
  14. return has
  15. }
  16. // IsWatching checks if user has watched given repository.
  17. func IsWatching(userID, repoID int64) bool {
  18. return isWatching(x, userID, repoID)
  19. }
  20. func watchRepo(e Engine, userID, repoID int64, watch bool) (err error) {
  21. if watch {
  22. if isWatching(e, userID, repoID) {
  23. return nil
  24. }
  25. if _, err = e.Insert(&Watch{RepoID: repoID, UserID: userID}); err != nil {
  26. return err
  27. }
  28. _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoID)
  29. } else {
  30. if !isWatching(e, userID, repoID) {
  31. return nil
  32. }
  33. if _, err = e.Delete(&Watch{0, userID, repoID}); err != nil {
  34. return err
  35. }
  36. _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repoID)
  37. }
  38. return err
  39. }
  40. // WatchRepo watch or unwatch repository.
  41. func WatchRepo(userID, repoID int64, watch bool) (err error) {
  42. return watchRepo(x, userID, repoID, watch)
  43. }
  44. func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
  45. watches := make([]*Watch, 0, 10)
  46. return watches, e.Where("`watch`.repo_id=?", repoID).
  47. And("`user`.is_active=?", true).
  48. And("`user`.prohibit_login=?", false).
  49. Join("INNER", "`user`", "`user`.id = `watch`.user_id").
  50. Find(&watches)
  51. }
  52. // GetWatchers returns all watchers of given repository.
  53. func GetWatchers(repoID int64) ([]*Watch, error) {
  54. return getWatchers(x, repoID)
  55. }
  56. // GetWatchers returns range of users watching given repository.
  57. func (repo *Repository) GetWatchers(page int) ([]*User, error) {
  58. users := make([]*User, 0, ItemsPerPage)
  59. sess := x.Where("watch.repo_id=?", repo.ID).
  60. Join("LEFT", "watch", "`user`.id=`watch`.user_id")
  61. if page > 0 {
  62. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  63. }
  64. return users, sess.Find(&users)
  65. }
  66. func notifyWatchers(e Engine, act *Action) error {
  67. // Add feeds for user self and all watchers.
  68. watches, err := getWatchers(e, act.RepoID)
  69. if err != nil {
  70. return fmt.Errorf("get watchers: %v", err)
  71. }
  72. // Add feed for actioner.
  73. act.UserID = act.ActUserID
  74. if _, err = e.InsertOne(act); err != nil {
  75. return fmt.Errorf("insert new actioner: %v", err)
  76. }
  77. act.loadRepo()
  78. // check repo owner exist.
  79. if err := act.Repo.getOwner(e); err != nil {
  80. return fmt.Errorf("can't get repo owner: %v", err)
  81. }
  82. // Add feed for organization
  83. if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
  84. act.ID = 0
  85. act.UserID = act.Repo.Owner.ID
  86. if _, err = e.InsertOne(act); err != nil {
  87. return fmt.Errorf("insert new actioner: %v", err)
  88. }
  89. }
  90. for i := range watches {
  91. if act.ActUserID == watches[i].UserID {
  92. continue
  93. }
  94. act.ID = 0
  95. act.UserID = watches[i].UserID
  96. act.Repo.Units = nil
  97. switch act.OpType {
  98. case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
  99. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
  100. continue
  101. }
  102. case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
  103. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
  104. continue
  105. }
  106. case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
  107. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
  108. continue
  109. }
  110. }
  111. if _, err = e.InsertOne(act); err != nil {
  112. return fmt.Errorf("insert new action: %v", err)
  113. }
  114. }
  115. return nil
  116. }
  117. // NotifyWatchers creates batch of actions for every watcher.
  118. func NotifyWatchers(act *Action) error {
  119. return notifyWatchers(x, act)
  120. }
上海开阖软件有限公司 沪ICP备12045867号-1