本站源代码
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.

86 lines
2.4KB

  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. // CreateOrUpdateIssueWatch set watching for a user and issue
  16. func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
  17. iw, exists, err := getIssueWatch(x, userID, issueID)
  18. if err != nil {
  19. return err
  20. }
  21. if !exists {
  22. iw = &IssueWatch{
  23. UserID: userID,
  24. IssueID: issueID,
  25. IsWatching: isWatching,
  26. }
  27. if _, err := x.Insert(iw); err != nil {
  28. return err
  29. }
  30. } else {
  31. iw.IsWatching = isWatching
  32. if _, err := x.ID(iw.ID).Cols("is_watching", "updated_unix").Update(iw); err != nil {
  33. return err
  34. }
  35. }
  36. return nil
  37. }
  38. // GetIssueWatch returns an issue watch by user and issue
  39. func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  40. return getIssueWatch(x, userID, issueID)
  41. }
  42. func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  43. iw = new(IssueWatch)
  44. exists, err = e.
  45. Where("user_id = ?", userID).
  46. And("issue_id = ?", issueID).
  47. Get(iw)
  48. return
  49. }
  50. // GetIssueWatchers returns watchers/unwatchers of a given issue
  51. func GetIssueWatchers(issueID int64) ([]*IssueWatch, error) {
  52. return getIssueWatchers(x, issueID)
  53. }
  54. func getIssueWatchers(e Engine, issueID int64) (watches []*IssueWatch, err error) {
  55. err = e.
  56. Where("`issue_watch`.issue_id = ?", issueID).
  57. And("`user`.is_active = ?", true).
  58. And("`user`.prohibit_login = ?", false).
  59. Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id").
  60. Find(&watches)
  61. return
  62. }
  63. func removeIssueWatchersByRepoID(e Engine, userID int64, repoID int64) error {
  64. iw := &IssueWatch{
  65. IsWatching: false,
  66. }
  67. _, err := e.
  68. Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID).
  69. Cols("is_watching", "updated_unix").
  70. Where("`issue_watch`.user_id = ?", userID).
  71. Update(iw)
  72. return err
  73. }
上海开阖软件有限公司 沪ICP备12045867号-1