Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_watch.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "time"
  7. )
  8. // IssueWatch is connection request for receiving issue notification.
  9. type IssueWatch struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. UserID int64 `xorm:"UNIQUE(watch) NOT NULL"`
  12. IssueID int64 `xorm:"UNIQUE(watch) NOT NULL"`
  13. IsWatching bool `xorm:"NOT NULL"`
  14. Created time.Time `xorm:"-"`
  15. CreatedUnix int64 `xorm:"NOT NULL"`
  16. Updated time.Time `xorm:"-"`
  17. UpdatedUnix int64 `xorm:"NOT NULL"`
  18. }
  19. // BeforeInsert is invoked from XORM before inserting an object of this type.
  20. func (iw *IssueWatch) BeforeInsert() {
  21. var (
  22. t = time.Now()
  23. u = t.Unix()
  24. )
  25. iw.Created = t
  26. iw.CreatedUnix = u
  27. iw.Updated = t
  28. iw.UpdatedUnix = u
  29. }
  30. // BeforeUpdate is invoked from XORM before updating an object of this type.
  31. func (iw *IssueWatch) BeforeUpdate() {
  32. var (
  33. t = time.Now()
  34. u = t.Unix()
  35. )
  36. iw.Updated = t
  37. iw.UpdatedUnix = u
  38. }
  39. // CreateOrUpdateIssueWatch set watching for a user and issue
  40. func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
  41. iw, exists, err := getIssueWatch(x, userID, issueID)
  42. if err != nil {
  43. return err
  44. }
  45. if !exists {
  46. iw = &IssueWatch{
  47. UserID: userID,
  48. IssueID: issueID,
  49. IsWatching: isWatching,
  50. }
  51. if _, err := x.Insert(iw); err != nil {
  52. return err
  53. }
  54. } else {
  55. iw.IsWatching = isWatching
  56. if _, err := x.ID(iw.ID).Cols("is_watching", "updated_unix").Update(iw); err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. // GetIssueWatch returns an issue watch by user and issue
  63. func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  64. return getIssueWatch(x, userID, issueID)
  65. }
  66. func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) {
  67. iw = new(IssueWatch)
  68. exists, err = e.
  69. Where("user_id = ?", userID).
  70. And("issue_id = ?", issueID).
  71. Get(iw)
  72. return
  73. }
  74. // GetIssueWatchers returns watchers/unwatchers of a given issue
  75. func GetIssueWatchers(issueID int64) ([]*IssueWatch, error) {
  76. return getIssueWatchers(x, issueID)
  77. }
  78. func getIssueWatchers(e Engine, issueID int64) (watches []*IssueWatch, err error) {
  79. err = e.
  80. Where("`issue_watch`.issue_id = ?", issueID).
  81. And("`user`.is_active = ?", true).
  82. And("`user`.prohibit_login = ?", false).
  83. Join("INNER", "user", "`user`.id = `issue_watch`.user_id").
  84. Find(&watches)
  85. return
  86. }