@@ -52,3 +52,83 @@ func (n *Notification) BeforeUpdate() {
52
52
n .Updated = now
53
53
n .UpdatedUnix = nowUnix
54
54
}
55
+
56
+ func CreateOrUpdateIssueNotifications (issue * Issue ) error {
57
+ watches , err := getWatchers (x , issue .RepoID )
58
+ if err != nil {
59
+ return err
60
+ }
61
+
62
+ sess := x .NewSession ()
63
+ if err := sess .Begin (); err != nil {
64
+ return err
65
+ }
66
+
67
+ defer sess .Close ()
68
+
69
+ for _ , watch := range watches {
70
+ exists , err := issueNotificationExists (sess , watch .UserID , watch .RepoID )
71
+ if err != nil {
72
+ return err
73
+ }
74
+
75
+ if exists {
76
+ err = updateIssueNotification (sess , watch .UserID , issue .ID )
77
+ } else {
78
+ err = createIssueNotification (sess , watch .UserID , issue )
79
+ }
80
+
81
+ if err != nil {
82
+ return err
83
+ }
84
+ }
85
+
86
+ return sess .Commit ()
87
+ }
88
+
89
+ func issueNotificationExists (e Engine , userID , issueID int64 ) (bool , error ) {
90
+ count , err := e .
91
+ Where ("user_id = ?" , userID ).
92
+ And ("issue_id = ?" , issueID ).
93
+ Count (Notification {})
94
+ return count > 0 , err
95
+ }
96
+
97
+ func createIssueNotification (e Engine , userID int64 , issue * Issue ) error {
98
+ notification := & Notification {
99
+ UserID : userID ,
100
+ RepoID : issue .RepoID ,
101
+ Status : NotificationStatusUnread ,
102
+ IssueID : issue .ID ,
103
+ }
104
+
105
+ if issue .IsPull {
106
+ notification .Source = NotificationSourcePullRequest
107
+ } else {
108
+ notification .Source = NotificationSourceIssue
109
+ }
110
+
111
+ _ , err := e .Insert (notification )
112
+ return err
113
+ }
114
+
115
+ func updateIssueNotification (e Engine , userID , issueID int64 ) error {
116
+ notification , err := getIssueNotification (e , userID , issueID )
117
+ if err != nil {
118
+ return err
119
+ }
120
+
121
+ notification .Status = NotificationStatusUnread
122
+
123
+ _ , err = e .Id (notification .ID ).Update (notification )
124
+ return err
125
+ }
126
+
127
+ func getIssueNotification (e Engine , userID , issueID int64 ) (* Notification , error ) {
128
+ notification := new (Notification )
129
+ _ , err := e .
130
+ Where ("user_id = ?" ).
131
+ And ("issue_id = ?" , issueID ).
132
+ Get (notification )
133
+ return notification , err
134
+ }
0 commit comments