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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // GetRepoWatchersIDs returns IDs of watchers for a given repo ID
  124. // but avoids joining with `user` for performance reasons
  125. // User permissions must be verified elsewhere if required
  126. func GetRepoWatchersIDs(repoID int64) ([]int64, error) {
  127. ids := make([]int64, 0, 64)
  128. return ids, x.Table("watch").
  129. Where("watch.repo_id=?", repoID).
  130. And("watch.mode<>?", RepoWatchModeDont).
  131. Select("user_id").
  132. Find(&ids)
  133. }
  134. // GetWatchers returns range of users watching given repository.
  135. func (repo *Repository) GetWatchers(page int) ([]*User, error) {
  136. users := make([]*User, 0, ItemsPerPage)
  137. sess := x.Where("watch.repo_id=?", repo.ID).
  138. Join("LEFT", "watch", "`user`.id=`watch`.user_id").
  139. And("`watch`.mode<>?", RepoWatchModeDont)
  140. if page > 0 {
  141. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  142. }
  143. return users, sess.Find(&users)
  144. }
  145. func notifyWatchers(e Engine, actions ...*Action) error {
  146. var watchers []*Watch
  147. var repo *Repository
  148. var err error
  149. var permCode []bool
  150. var permIssue []bool
  151. var permPR []bool
  152. for _, act := range actions {
  153. repoChanged := repo == nil || repo.ID != act.RepoID
  154. if repoChanged {
  155. // Add feeds for user self and all watchers.
  156. watchers, err = getWatchers(e, act.RepoID)
  157. if err != nil {
  158. return fmt.Errorf("get watchers: %v", err)
  159. }
  160. }
  161. // Add feed for actioner.
  162. act.UserID = act.ActUserID
  163. if _, err = e.InsertOne(act); err != nil {
  164. return fmt.Errorf("insert new actioner: %v", err)
  165. }
  166. if repoChanged {
  167. act.loadRepo()
  168. repo = act.Repo
  169. // check repo owner exist.
  170. if err := act.Repo.getOwner(e); err != nil {
  171. return fmt.Errorf("can't get repo owner: %v", err)
  172. }
  173. } else if act.Repo == nil {
  174. act.Repo = repo
  175. }
  176. // Add feed for organization
  177. if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
  178. act.ID = 0
  179. act.UserID = act.Repo.Owner.ID
  180. if _, err = e.InsertOne(act); err != nil {
  181. return fmt.Errorf("insert new actioner: %v", err)
  182. }
  183. }
  184. if repoChanged {
  185. permCode = make([]bool, len(watchers))
  186. permIssue = make([]bool, len(watchers))
  187. permPR = make([]bool, len(watchers))
  188. for i, watcher := range watchers {
  189. user, err := getUserByID(e, watcher.UserID)
  190. if err != nil {
  191. permCode[i] = false
  192. permIssue[i] = false
  193. permPR[i] = false
  194. continue
  195. }
  196. perm, err := getUserRepoPermission(e, repo, user)
  197. if err != nil {
  198. permCode[i] = false
  199. permIssue[i] = false
  200. permPR[i] = false
  201. continue
  202. }
  203. permCode[i] = perm.CanRead(UnitTypeCode)
  204. permIssue[i] = perm.CanRead(UnitTypeIssues)
  205. permPR[i] = perm.CanRead(UnitTypePullRequests)
  206. }
  207. }
  208. for i, watcher := range watchers {
  209. if act.ActUserID == watcher.UserID {
  210. continue
  211. }
  212. act.ID = 0
  213. act.UserID = watcher.UserID
  214. act.Repo.Units = nil
  215. switch act.OpType {
  216. case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
  217. if !permCode[i] {
  218. continue
  219. }
  220. case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
  221. if !permIssue[i] {
  222. continue
  223. }
  224. case ActionCreatePullRequest, ActionCommentPull, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
  225. if !permPR[i] {
  226. continue
  227. }
  228. }
  229. if _, err = e.InsertOne(act); err != nil {
  230. return fmt.Errorf("insert new action: %v", err)
  231. }
  232. }
  233. }
  234. return nil
  235. }
  236. // NotifyWatchers creates batch of actions for every watcher.
  237. func NotifyWatchers(actions ...*Action) error {
  238. return notifyWatchers(x, actions...)
  239. }
  240. // NotifyWatchersActions creates batch of actions for every watcher.
  241. func NotifyWatchersActions(acts []*Action) error {
  242. sess := x.NewSession()
  243. defer sess.Close()
  244. if err := sess.Begin(); err != nil {
  245. return err
  246. }
  247. for _, act := range acts {
  248. if err := notifyWatchers(sess, act); err != nil {
  249. return err
  250. }
  251. }
  252. return sess.Commit()
  253. }
  254. func watchIfAuto(e Engine, userID, repoID int64, isWrite bool) error {
  255. if !isWrite || !setting.Service.AutoWatchOnChanges {
  256. return nil
  257. }
  258. watch, err := getWatch(e, userID, repoID)
  259. if err != nil {
  260. return err
  261. }
  262. if watch.Mode != RepoWatchModeNone {
  263. return nil
  264. }
  265. return watchRepoMode(e, watch, RepoWatchModeAuto)
  266. }
  267. // WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
  268. func WatchIfAuto(userID int64, repoID int64, isWrite bool) error {
  269. return watchIfAuto(x, userID, repoID, isWrite)
  270. }