Skip to content

Commit 7434986

Browse files
committed
more efficient multiple notifyWatchers
1 parent 6bc317d commit 7434986

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

models/repo_watch.go

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,22 @@ func (repo *Repository) GetWatchers(page int) ([]*User, error) {
165165
}
166166

167167
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+
168175
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+
}
173184
}
174185

175186
// Add feed for actioner.
@@ -178,10 +189,16 @@ func notifyWatchers(e Engine, actions ...*Action) error {
178189
return fmt.Errorf("insert new actioner: %v", err)
179190
}
180191

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
185202
}
186203

187204
// Add feed for organization
@@ -193,26 +210,84 @@ func notifyWatchers(e Engine, actions ...*Action) error {
193210
}
194211
}
195212

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 {
198273
continue
199274
}
200275

201276
act.ID = 0
202-
act.UserID = watches[i].UserID
277+
act.UserID = watcher.UserID
203278
act.Repo.Units = nil
204279

205280
switch act.OpType {
206281
case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
207-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
282+
if !permCode[i] {
208283
continue
209284
}
210285
case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
211-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
286+
if !permIssue[i] {
212287
continue
213288
}
214289
case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
215-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
290+
if !permPR[i] {
216291
continue
217292
}
218293
}

0 commit comments

Comments
 (0)