Skip to content

Commit 6bc317d

Browse files
committed
make notifyWatchers work on multiple actions
1 parent 3366239 commit 6bc317d

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

models/repo_watch.go

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -164,68 +164,70 @@ func (repo *Repository) GetWatchers(page int) ([]*User, error) {
164164
return users, sess.Find(&users)
165165
}
166166

167-
func notifyWatchers(e Engine, act *Action) error {
168-
// Add feeds for user self and all watchers.
169-
watches, err := getWatchers(e, act.RepoID)
170-
if err != nil {
171-
return fmt.Errorf("get watchers: %v", err)
172-
}
173-
174-
// Add feed for actioner.
175-
act.UserID = act.ActUserID
176-
if _, err = e.InsertOne(act); err != nil {
177-
return fmt.Errorf("insert new actioner: %v", err)
178-
}
179-
180-
act.loadRepo()
181-
// check repo owner exist.
182-
if err := act.Repo.getOwner(e); err != nil {
183-
return fmt.Errorf("can't get repo owner: %v", err)
184-
}
167+
func notifyWatchers(e Engine, actions ...*Action) error {
168+
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)
173+
}
185174

186-
// Add feed for organization
187-
if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
188-
act.ID = 0
189-
act.UserID = act.Repo.Owner.ID
175+
// Add feed for actioner.
176+
act.UserID = act.ActUserID
190177
if _, err = e.InsertOne(act); err != nil {
191178
return fmt.Errorf("insert new actioner: %v", err)
192179
}
193-
}
194180

195-
for i := range watches {
196-
if act.ActUserID == watches[i].UserID {
197-
continue
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)
198185
}
199186

200-
act.ID = 0
201-
act.UserID = watches[i].UserID
202-
act.Repo.Units = nil
203-
204-
switch act.OpType {
205-
case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
206-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
207-
continue
187+
// Add feed for organization
188+
if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
189+
act.ID = 0
190+
act.UserID = act.Repo.Owner.ID
191+
if _, err = e.InsertOne(act); err != nil {
192+
return fmt.Errorf("insert new actioner: %v", err)
208193
}
209-
case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
210-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
194+
}
195+
196+
for i := range watches {
197+
if act.ActUserID == watches[i].UserID {
211198
continue
212199
}
213-
case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
214-
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
215-
continue
200+
201+
act.ID = 0
202+
act.UserID = watches[i].UserID
203+
act.Repo.Units = nil
204+
205+
switch act.OpType {
206+
case ActionCommitRepo, ActionPushTag, ActionDeleteTag, ActionDeleteBranch:
207+
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeCode) {
208+
continue
209+
}
210+
case ActionCreateIssue, ActionCommentIssue, ActionCloseIssue, ActionReopenIssue:
211+
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypeIssues) {
212+
continue
213+
}
214+
case ActionCreatePullRequest, ActionMergePullRequest, ActionClosePullRequest, ActionReopenPullRequest:
215+
if !act.Repo.checkUnitUser(e, act.UserID, false, UnitTypePullRequests) {
216+
continue
217+
}
216218
}
217-
}
218219

219-
if _, err = e.InsertOne(act); err != nil {
220-
return fmt.Errorf("insert new action: %v", err)
220+
if _, err = e.InsertOne(act); err != nil {
221+
return fmt.Errorf("insert new action: %v", err)
222+
}
221223
}
222224
}
223225
return nil
224226
}
225227

226228
// NotifyWatchers creates batch of actions for every watcher.
227-
func NotifyWatchers(act *Action) error {
228-
return notifyWatchers(x, act)
229+
func NotifyWatchers(actions ...*Action) error {
230+
return notifyWatchers(x, actions...)
229231
}
230232

231233
// NotifyWatchersActions creates batch of actions for every watcher.

0 commit comments

Comments
 (0)