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.

103 lines
2.8 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.Find(&watches, &Watch{RepoID: repoID})
  47. }
  48. // GetWatchers returns all watchers of given repository.
  49. func GetWatchers(repoID int64) ([]*Watch, error) {
  50. return getWatchers(x, repoID)
  51. }
  52. // GetWatchers returns range of users watching given repository.
  53. func (repo *Repository) GetWatchers(page int) ([]*User, error) {
  54. users := make([]*User, 0, ItemsPerPage)
  55. sess := x.Where("watch.repo_id=?", repo.ID).
  56. Join("LEFT", "watch", "`user`.id=`watch`.user_id")
  57. if page > 0 {
  58. sess = sess.Limit(ItemsPerPage, (page-1)*ItemsPerPage)
  59. }
  60. return users, sess.Find(&users)
  61. }
  62. func notifyWatchers(e Engine, act *Action) error {
  63. // Add feeds for user self and all watchers.
  64. watches, err := getWatchers(e, act.RepoID)
  65. if err != nil {
  66. return fmt.Errorf("get watchers: %v", err)
  67. }
  68. // Add feed for actioner.
  69. act.UserID = act.ActUserID
  70. if _, err = e.InsertOne(act); err != nil {
  71. return fmt.Errorf("insert new actioner: %v", err)
  72. }
  73. for i := range watches {
  74. if act.ActUserID == watches[i].UserID {
  75. continue
  76. }
  77. act.ID = 0
  78. act.UserID = watches[i].UserID
  79. if _, err = e.InsertOne(act); err != nil {
  80. return fmt.Errorf("insert new action: %v", err)
  81. }
  82. }
  83. return nil
  84. }
  85. // NotifyWatchers creates batch of actions for every watcher.
  86. func NotifyWatchers(act *Action) error {
  87. return notifyWatchers(x, act)
  88. }