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.

repo_watch.go 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/setting"
  8. )
  9. // RepoWatchMode specifies what kind of watch the user has on a repository
  10. type RepoWatchMode int8
  11. const (
  12. // RepoWatchModeNone don't watch
  13. RepoWatchModeNone RepoWatchMode = iota // 0
  14. // RepoWatchModeNormal watch repository (from other sources)
  15. RepoWatchModeNormal // 1
  16. // RepoWatchModeDont explicit don't auto-watch
  17. RepoWatchModeDont // 2
  18. // RepoWatchModeAuto watch repository (from AutoWatchOnChanges)
  19. RepoWatchModeAuto // 3
  20. )
  21. // Watch is connection request for receiving repository notification.
  22. type Watch struct {
  23. ID int64 `xorm:"pk autoincr"`
  24. UserID int64 `xorm:"UNIQUE(watch)"`
  25. RepoID int64 `xorm:"UNIQUE(watch)"`
  26. Mode RepoWatchMode `xorm:"SMALLINT NOT NULL DEFAULT 1"`
  27. }
  28. // getWatch gets what kind of subscription a user has on a given repository; returns dummy record if none found
  29. func getWatch(e Engine, userID, repoID int64) (Watch, error) {
  30. watch := Watch{UserID: userID, RepoID: repoID}
  31. has, err := e.Get(&watch)
  32. if err != nil {
  33. return watch, err
  34. }
  35. if !has {
  36. watch.Mode = RepoWatchModeNone
  37. }
  38. return watch, nil
  39. }
  40. // Decodes watchability of RepoWatchMode
  41. func isWatchMode(mode RepoWatchMode) bool {
  42. return mode != RepoWatchModeNone && mode != RepoWatchModeDont
  43. }
  44. // IsWatching checks if user has watched given repository.
  45. func IsWatching(userID, repoID int64) bool {
  46. watch, err := getWatch(x, userID, repoID)
  47. return err == nil && isWatchMode(watch.Mode)
  48. }
  49. func watchRepoMode(e Engine, watch Watch, mode RepoWatchMode) (err error) {
  50. if watch.Mode == mode {
  51. return nil
  52. }
  53. if mode == RepoWatchModeAuto && (watch.Mode == RepoWatchModeDont || isWatchMode(watch.Mode)) {
  54. // Don't auto watch if already watching or deliberately not watching
  55. return nil
  56. }
  57. hadrec := watch.Mode != RepoWatchModeNone
  58. needsrec := mode != RepoWatchModeNone
  59. repodiff := 0
  60. if isWatchMode(mode) && !isWatchMode(watch.Mode) {
  61. repodiff = 1
  62. } else if !isWatchMode(mode) && isWatchMode(watch.Mode) {
  63. repodiff = -1
  64. }
  65. watch.Mode = mode
  66. if !hadrec && needsrec {
  67. watch.Mode = mode
  68. if _, err = e.Insert(watch); err != nil {
  69. return err
  70. }
  71. } else if needsrec {
  72. watch.Mode = mode
  73. if _, err := e.ID(watch.ID).AllCols().Update(watch); err != nil {
  74. return err
  75. }
  76. } else if _, err = e.Delete(Watch{ID: watch.ID}); err != nil {
  77. return err
  78. }
  79. if repodiff != 0 {
  80. _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
  81. }
  82. return err
  83. }
  84. // WatchRepoMode watch repository in specific mode.
  85. func WatchRepoMode(userID, repoID int64, mode RepoWatchMode) (err error) {
  86. var watch Watch
  87. if watch, err = getWatch(x, userID, repoID); err != nil {
  88. return err
  89. }
  90. return watchRepoMode(x, watch, mode)
  91. }
  92. func watchRepo(e Engine, userID, repoID int64, doWatch bool) (err error) {
  93. var watch Watch
  94. if watch, err = getWatch(e, userID, repoID); err != nil {
  95. return err
  96. }
  97. if !doWatch && watch.Mode == RepoWatchModeAuto {
  98. err = watchRepoMode(e, watch, RepoWatchModeDont)
  99. } else if !doWatch {
  100. err = watchRepoMode(e, watch, RepoWatchModeNone)
  101. } else {
  102. err = watchRepoMode(e, watch, RepoWatchModeNormal)
  103. }
  104. return err
  105. }
  106. // WatchRepo watch or unwatch repository.
  107. func WatchRepo(userID, repoID int64, watch bool) (err error) {
  108. return watchRepo(x, userID, repoID, watch)
  109. }
  110. func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
  111. watches := make([]*Watch, 0, 10)
  112. return watches, e.Where("`watch`.repo_id=?", repoID).
  113. And("`watch`.mode<>?", RepoWatchModeDont).
  114. And("`user`.is_active=?", true).
  115. And("`user`.prohibit_login=?", false).
  116. Join("INNER", "`user`", "`user`.id = `watch`.user_id").
  117. Find(&watches)
  118. }
  119. // GetWatchers returns all watchers of given repository.
  120. func GetWatchers(repoID int64) ([]*Watch, error) {
  121. return getWatchers(x, repoID)
  122. }
  123. // GetWatchers returns range of users watching given repository.
  124. func (repo *Repository) GetWatchers(page int) ([]*User, error) {
  125. users := make([]*User, 0, ItemsPerPage)
  126. sess := x.Where("watch.repo_id=?", repo.ID).
  127. Join("LEFT", "watch", "`user`.id=`watch`.user_id").
  128. And("`watch`.mode<>?", RepoWatchModeDont)
  129. if page > 0 {
  130. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  131. }
  132. return users, sess.Find(&users)
  133. }
  134. func notifyWatchers(e Engine, act *Action) error {
  135. // Add feeds for user self and all watchers.
  136. watches, err := getWatchers(e, act.RepoID)
  137. if err != nil {
  138. return fmt.Errorf("get watchers: %v", err)
  139. }
  140. // Add feed for actioner.
  141. act.UserID = act.ActUserID
  142. if _, err = e.InsertOne(act); err != nil {
  143. return fmt.Errorf("insert new actioner: %v", err)
  144. }
  145. act.loadRepo()
  146. // check repo owner exist.
  147. if err := act.Repo.getOwner(e); err != nil {
  148. return fmt.Errorf("can't get repo owner: %v", err)
  149. }
  150. // Add feed for organization
  151. if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
  152. act.ID = 0
  153. act.UserID = act.Repo.Owner.ID
  154. if _, err = e.InsertOne(act); err != nil {
  155. return fmt.Errorf("insert new actioner: %v", err)
  156. }
  157. }
  158. for i := range watches {
  159. if act.ActUserID == watches[i].UserID {
  160. continue
  161. }
  162. act.ID = 0
  163. act.UserID = watches[i].UserID
  164. act.Repo.Units = nil
  165. switch act.OpType {
  166. case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
  167. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
  168. continue
  169. }
  170. case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
  171. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
  172. continue
  173. }
  174. case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
  175. if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
  176. continue
  177. }
  178. }
  179. if _, err = e.InsertOne(act); err != nil {
  180. return fmt.Errorf("insert new action: %v", err)
  181. }
  182. }
  183. return nil
  184. }
  185. // NotifyWatchers creates batch of actions for every watcher.
  186. func NotifyWatchers(act *Action) error {
  187. return notifyWatchers(x, act)
  188. }
  189. // NotifyWatchersActions creates batch of actions for every watcher.
  190. func NotifyWatchersActions(acts []*Action) error {
  191. sess := x.NewSession()
  192. defer sess.Close()
  193. if err := sess.Begin(); err != nil {
  194. return err
  195. }
  196. for _, act := range acts {
  197. if err := notifyWatchers(sess, act); err != nil {
  198. return err
  199. }
  200. }
  201. return sess.Commit()
  202. }
  203. func watchIfAuto(e Engine, userID, repoID int64, isWrite bool) error {
  204. if !isWrite || !setting.Service.AutoWatchOnChanges {
  205. return nil
  206. }
  207. watch, err := getWatch(e, userID, repoID)
  208. if err != nil {
  209. return err
  210. }
  211. if watch.Mode != RepoWatchModeNone {
  212. return nil
  213. }
  214. return watchRepoMode(e, watch, RepoWatchModeAuto)
  215. }
  216. // WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
  217. func WatchIfAuto(userID int64, repoID int64, isWrite bool) error {
  218. return watchIfAuto(x, userID, repoID, isWrite)
  219. }