You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_watch.go 3.5 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "code.gitea.io/gitea/modules/timeutil"
  6. // IssueWatch is connection request for receiving issue notification.
  7. type IssueWatch struct {
  8. ID int64 `xorm:"pk autoincr"`
  9. UserID int64 `xorm:"UNIQUE(watch) NOT NULL"`
  10. IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"`
  11. IsWatching bool `xorm:"NOT NULL"`
  12. CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
  13. UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`
  14. }
  15. // IssueWatchList contains IssueWatch
  16. type IssueWatchList []*IssueWatch
  17. // CreateOrUpdateIssueWatch set watching for a user and issue
  18. func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
  19. iw, exists, err := getIssueWatch(x, userID, issueID)
  20. if err != nil {
  21. return err
  22. }
  23. if !exists {
  24. iw = &IssueWatch{
  25. UserID: userID,
  26. IssueID: issueID,
  27. IsWatching: isWatching,
  28. }
  29. if _, err := x.Insert(iw); err != nil {
  30. return err
  31. }
  32. } else {
  33. iw.IsWatching = isWatching
  34. if _, err := x.ID(iw.ID).Cols("is_watching", "updated_unix").Update(iw); err != nil {
  35. return err
  36. }
  37. }
  38. return nil
  39. }
  40. // GetIssueWatch returns an issue watch by user and issue
  41. func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  42. return getIssueWatch(x, userID, issueID)
  43. }
  44. func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  45. iw = new(IssueWatch)
  46. exists, err = e.
  47. Where("user_id = ?", userID).
  48. And("issue_id = ?", issueID).
  49. Get(iw)
  50. return
  51. }
  52. // GetIssueWatchersIDs returns IDs of subscribers to a given issue id
  53. // but avoids joining with `user` for performance reasons
  54. // User permissions must be verified elsewhere if required
  55. func GetIssueWatchersIDs(issueID int64) ([]int64, error) {
  56. ids := make([]int64, 0, 64)
  57. return ids, x.Table("issue_watch").
  58. Where("issue_id=?", issueID).
  59. And("is_watching = ?", true).
  60. Select("user_id").
  61. Find(&ids)
  62. }
  63. // GetIssueWatchers returns watchers/unwatchers of a given issue
  64. func GetIssueWatchers(issueID int64) (IssueWatchList, error) {
  65. return getIssueWatchers(x, issueID)
  66. }
  67. func getIssueWatchers(e Engine, issueID int64) (watches IssueWatchList, err error) {
  68. err = e.
  69. Where("`issue_watch`.issue_id = ?", issueID).
  70. And("`user`.is_active = ?", true).
  71. And("`user`.prohibit_login = ?", false).
  72. Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id").
  73. Find(&watches)
  74. return
  75. }
  76. func removeIssueWatchersByRepoID(e Engine, userID int64, repoID int64) error {
  77. iw := &IssueWatch{
  78. IsWatching: false,
  79. }
  80. _, err := e.
  81. Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID).
  82. Cols("is_watching", "updated_unix").
  83. Where("`issue_watch`.user_id = ?", userID).
  84. Update(iw)
  85. return err
  86. }
  87. // LoadWatchUsers return watching users
  88. func (iwl IssueWatchList) LoadWatchUsers() (users UserList, err error) {
  89. return iwl.loadWatchUsers(x)
  90. }
  91. func (iwl IssueWatchList) loadWatchUsers(e Engine) (users UserList, err error) {
  92. if len(iwl) == 0 {
  93. return []*User{}, nil
  94. }
  95. var userIDs = make([]int64, 0, len(iwl))
  96. for _, iw := range iwl {
  97. if iw.IsWatching {
  98. userIDs = append(userIDs, iw.UserID)
  99. }
  100. }
  101. if len(userIDs) == 0 {
  102. return []*User{}, nil
  103. }
  104. err = e.In("id", userIDs).Find(&users)
  105. return
  106. }