@@ -165,11 +165,22 @@ func (repo *Repository) GetWatchers(page int) ([]*User, error) {
165
165
}
166
166
167
167
func notifyWatchers (e Engine , actions ... * Action ) error {
168
+ var watchers []* Watch
169
+ var repo * Repository
170
+ var err error
171
+ var permCode []bool
172
+ var permIssue []bool
173
+ var permPR []bool
174
+
168
175
for _ , act := range actions {
169
- // Add feeds for user self and all watchers.
170
- watches , err := getWatchers (e , act .RepoID )
171
- if err != nil {
172
- return fmt .Errorf ("get watchers: %v" , err )
176
+ repoChanged := repo == nil || repo .ID != act .RepoID
177
+
178
+ if repoChanged {
179
+ // Add feeds for user self and all watchers.
180
+ watchers , err = getWatchers (e , act .RepoID )
181
+ if err != nil {
182
+ return fmt .Errorf ("get watchers: %v" , err )
183
+ }
173
184
}
174
185
175
186
// Add feed for actioner.
@@ -178,10 +189,16 @@ func notifyWatchers(e Engine, actions ...*Action) error {
178
189
return fmt .Errorf ("insert new actioner: %v" , err )
179
190
}
180
191
181
- act .loadRepo ()
182
- // check repo owner exist.
183
- if err := act .Repo .getOwner (e ); err != nil {
184
- return fmt .Errorf ("can't get repo owner: %v" , err )
192
+ if repoChanged {
193
+ act .loadRepo ()
194
+ repo = act .Repo
195
+
196
+ // check repo owner exist.
197
+ if err := act .Repo .getOwner (e ); err != nil {
198
+ return fmt .Errorf ("can't get repo owner: %v" , err )
199
+ }
200
+ } else if act .Repo == nil {
201
+ act .Repo = repo
185
202
}
186
203
187
204
// Add feed for organization
@@ -193,26 +210,84 @@ func notifyWatchers(e Engine, actions ...*Action) error {
193
210
}
194
211
}
195
212
196
- for i := range watches {
197
- if act .ActUserID == watches [i ].UserID {
213
+ if len (actions ) == 1 {
214
+ // More efficient to just check the code and not cache
215
+ for _ , watcher := range watchers {
216
+ if act .ActUserID == watcher .UserID {
217
+ continue
218
+ }
219
+
220
+ act .ID = 0
221
+ act .UserID = watcher .UserID
222
+ act .Repo .Units = nil
223
+
224
+ switch act .OpType {
225
+ case ActionCommitRepo , ActionPushTag , ActionDeleteTag , ActionDeleteBranch :
226
+ if ! act .Repo .checkUnitUser (e , act .UserID , false , UnitTypeCode ) {
227
+ continue
228
+ }
229
+ case ActionCreateIssue , ActionCommentIssue , ActionCloseIssue , ActionReopenIssue :
230
+ if ! act .Repo .checkUnitUser (e , act .UserID , false , UnitTypeIssues ) {
231
+ continue
232
+ }
233
+ case ActionCreatePullRequest , ActionMergePullRequest , ActionClosePullRequest , ActionReopenPullRequest :
234
+ if ! act .Repo .checkUnitUser (e , act .UserID , false , UnitTypePullRequests ) {
235
+ continue
236
+ }
237
+ }
238
+
239
+ if _ , err = e .InsertOne (act ); err != nil {
240
+ return fmt .Errorf ("insert new action: %v" , err )
241
+ }
242
+ }
243
+ return nil
244
+ }
245
+
246
+ if repoChanged {
247
+ permCode = make ([]bool , len (watchers ))
248
+ permIssue = make ([]bool , len (watchers ))
249
+ permPR = make ([]bool , len (watchers ))
250
+ for i , watcher := range watchers {
251
+ user , err := getUserByID (e , watcher .UserID )
252
+ if err != nil {
253
+ permCode [i ] = false
254
+ permIssue [i ] = false
255
+ permPR [i ] = false
256
+ continue
257
+ }
258
+ perm , err := getUserRepoPermission (e , repo , user )
259
+ if err != nil {
260
+ permCode [i ] = false
261
+ permIssue [i ] = false
262
+ permPR [i ] = false
263
+ continue
264
+ }
265
+ permCode [i ] = perm .CanRead (UnitTypeCode )
266
+ permIssue [i ] = perm .CanRead (UnitTypeIssues )
267
+ permPR [i ] = perm .CanRead (UnitTypePullRequests )
268
+ }
269
+ }
270
+
271
+ for i , watcher := range watchers {
272
+ if act .ActUserID == watcher .UserID {
198
273
continue
199
274
}
200
275
201
276
act .ID = 0
202
- act .UserID = watches [ i ] .UserID
277
+ act .UserID = watcher .UserID
203
278
act .Repo .Units = nil
204
279
205
280
switch act .OpType {
206
281
case ActionCommitRepo , ActionPushTag , ActionDeleteTag , ActionDeleteBranch :
207
- if ! act . Repo . checkUnitUser ( e , act . UserID , false , UnitTypeCode ) {
282
+ if ! permCode [ i ] {
208
283
continue
209
284
}
210
285
case ActionCreateIssue , ActionCommentIssue , ActionCloseIssue , ActionReopenIssue :
211
- if ! act . Repo . checkUnitUser ( e , act . UserID , false , UnitTypeIssues ) {
286
+ if ! permIssue [ i ] {
212
287
continue
213
288
}
214
289
case ActionCreatePullRequest , ActionMergePullRequest , ActionClosePullRequest , ActionReopenPullRequest :
215
- if ! act . Repo . checkUnitUser ( e , act . UserID , false , UnitTypePullRequests ) {
290
+ if ! permPR [ i ] {
216
291
continue
217
292
}
218
293
}
0 commit comments