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.6KB

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