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.

107 lines
2.9 KiB

  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. for i := range watches {
  78. if act.ActUserID == watches[i].UserID {
  79. continue
  80. }
  81. act.ID = 0
  82. act.UserID = watches[i].UserID
  83. if _, err = e.InsertOne(act); err != nil {
  84. return fmt.Errorf("insert new action: %v", err)
  85. }
  86. }
  87. return nil
  88. }
  89. // NotifyWatchers creates batch of actions for every watcher.
  90. func NotifyWatchers(act *Action) error {
  91. return notifyWatchers(x, act)
  92. }