Skip to content

Commit 4d42422

Browse files
committed
Fix lint
1 parent 44a49f5 commit 4d42422

File tree

3 files changed

+68
-67
lines changed

3 files changed

+68
-67
lines changed

models/activities/action.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,13 @@ func (a *Action) LoadActUser(ctx context.Context) {
198198
}
199199
}
200200

201-
func (a *Action) LoadRepo(ctx context.Context) {
201+
func (a *Action) LoadRepo(ctx context.Context) error {
202202
if a.Repo != nil {
203-
return
203+
return nil
204204
}
205205
var err error
206206
a.Repo, err = repo_model.GetRepositoryByID(ctx, a.RepoID)
207-
if err != nil {
208-
log.Error("repo_model.GetRepositoryByID(%d): %v", a.RepoID, err)
209-
}
207+
return err
210208
}
211209

212210
// GetActFullName gets the action's user full name.
@@ -248,7 +246,7 @@ func (a *Action) GetActDisplayNameTitle(ctx context.Context) string {
248246

249247
// GetRepoUserName returns the name of the action repository owner.
250248
func (a *Action) GetRepoUserName(ctx context.Context) string {
251-
a.LoadRepo(ctx)
249+
_ = a.LoadRepo(ctx)
252250
if a.Repo == nil {
253251
return "(non-existing-repo)"
254252
}
@@ -263,7 +261,7 @@ func (a *Action) ShortRepoUserName(ctx context.Context) string {
263261

264262
// GetRepoName returns the name of the action repository.
265263
func (a *Action) GetRepoName(ctx context.Context) string {
266-
a.LoadRepo(ctx)
264+
_ = a.LoadRepo(ctx)
267265
if a.Repo == nil {
268266
return "(non-existing-repo)"
269267
}

routers/web/feed/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func toReleaseLink(ctx *context.Context, act *activities_model.Action) string {
5050

5151
// renderCommentMarkdown renders the comment markdown to html
5252
func renderCommentMarkdown(ctx *context.Context, act *activities_model.Action, content string) template.HTML {
53-
act.LoadRepo(ctx)
53+
_ = act.LoadRepo(ctx)
5454
if act.Repo == nil {
5555
return ""
5656
}

services/feed/feed.go

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
repo_model "code.gitea.io/gitea/models/repo"
1414
"code.gitea.io/gitea/models/unit"
1515
user_model "code.gitea.io/gitea/models/user"
16+
"code.gitea.io/gitea/modules/setting"
1617
)
1718

1819
// GetFeeds returns actions according to the provided options
@@ -25,76 +26,22 @@ func GetFeeds(ctx context.Context, opts activities_model.GetFeedsOptions) (activ
2526
// * Original action: UserID=1 (the real actor), ActUserID=1
2627
// * Organization action: UserID=100 (the repo's org), ActUserID=1
2728
// * Watcher action: UserID=20 (a user who is watching a repo), ActUserID=1
28-
func notifyWatchers(ctx context.Context, act *activities_model.Action) error {
29-
var watchers []*repo_model.Watch
30-
var repo *repo_model.Repository
31-
var err error
32-
var permCode []bool
33-
var permIssue []bool
34-
var permPR []bool
35-
36-
repoChanged := repo == nil || repo.ID != act.RepoID
37-
38-
if repoChanged {
39-
// Add feeds for user self and all watchers.
40-
watchers, err = repo_model.GetWatchers(ctx, act.RepoID)
41-
if err != nil {
42-
return fmt.Errorf("get watchers: %w", err)
43-
}
44-
}
45-
29+
func notifyWatchers(ctx context.Context, act *activities_model.Action, watchers []*repo_model.Watch, permCode, permIssue, permPR []bool) error {
4630
// Add feed for actioner.
4731
act.UserID = act.ActUserID
48-
if err = db.Insert(ctx, act); err != nil {
32+
if err := db.Insert(ctx, act); err != nil {
4933
return fmt.Errorf("insert new actioner: %w", err)
5034
}
5135

52-
if repoChanged {
53-
act.LoadRepo(ctx)
54-
repo = act.Repo
55-
56-
// check repo owner exist.
57-
if err := act.Repo.LoadOwner(ctx); err != nil {
58-
return fmt.Errorf("can't get repo owner: %w", err)
59-
}
60-
} else if act.Repo == nil {
61-
act.Repo = repo
62-
}
63-
6436
// Add feed for organization
6537
if act.Repo.Owner.IsOrganization() && act.ActUserID != act.Repo.Owner.ID {
6638
act.ID = 0
6739
act.UserID = act.Repo.Owner.ID
68-
if err = db.Insert(ctx, act); err != nil {
40+
if err := db.Insert(ctx, act); err != nil {
6941
return fmt.Errorf("insert new actioner: %w", err)
7042
}
7143
}
7244

73-
if repoChanged {
74-
permCode = make([]bool, len(watchers))
75-
permIssue = make([]bool, len(watchers))
76-
permPR = make([]bool, len(watchers))
77-
for i, watcher := range watchers {
78-
user, err := user_model.GetUserByID(ctx, watcher.UserID)
79-
if err != nil {
80-
permCode[i] = false
81-
permIssue[i] = false
82-
permPR[i] = false
83-
continue
84-
}
85-
perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
86-
if err != nil {
87-
permCode[i] = false
88-
permIssue[i] = false
89-
permPR[i] = false
90-
continue
91-
}
92-
permCode[i] = perm.CanRead(unit.TypeCode)
93-
permIssue[i] = perm.CanRead(unit.TypeIssues)
94-
permPR[i] = perm.CanRead(unit.TypePullRequests)
95-
}
96-
}
97-
9845
for i, watcher := range watchers {
9946
if act.ActUserID == watcher.UserID {
10047
continue
@@ -118,7 +65,7 @@ func notifyWatchers(ctx context.Context, act *activities_model.Action) error {
11865
}
11966
}
12067

121-
if err = db.Insert(ctx, act); err != nil {
68+
if err := db.Insert(ctx, act); err != nil {
12269
return fmt.Errorf("insert new action: %w", err)
12370
}
12471
}
@@ -129,8 +76,64 @@ func notifyWatchers(ctx context.Context, act *activities_model.Action) error {
12976
// NotifyWatchersActions creates batch of actions for every watcher.
13077
func NotifyWatchers(ctx context.Context, acts ...*activities_model.Action) error {
13178
return db.WithTx(ctx, func(ctx context.Context) error {
79+
if len(acts) == 0 {
80+
return nil
81+
}
82+
83+
repoID := acts[0].RepoID
84+
if repoID == 0 {
85+
setting.PanicInDevOrTesting("action should belong to a repo")
86+
return nil
87+
}
88+
if err := acts[0].LoadRepo(ctx); err != nil {
89+
return err
90+
}
91+
repo := acts[0].Repo
92+
if err := repo.LoadOwner(ctx); err != nil {
93+
return err
94+
}
95+
96+
actUserID := acts[0].ActUserID
97+
98+
// Add feeds for user self and all watchers.
99+
watchers, err := repo_model.GetWatchers(ctx, repoID)
100+
if err != nil {
101+
return fmt.Errorf("get watchers: %w", err)
102+
}
103+
104+
permCode := make([]bool, len(watchers))
105+
permIssue := make([]bool, len(watchers))
106+
permPR := make([]bool, len(watchers))
107+
for i, watcher := range watchers {
108+
user, err := user_model.GetUserByID(ctx, watcher.UserID)
109+
if err != nil {
110+
permCode[i] = false
111+
permIssue[i] = false
112+
permPR[i] = false
113+
continue
114+
}
115+
perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
116+
if err != nil {
117+
permCode[i] = false
118+
permIssue[i] = false
119+
permPR[i] = false
120+
continue
121+
}
122+
permCode[i] = perm.CanRead(unit.TypeCode)
123+
permIssue[i] = perm.CanRead(unit.TypeIssues)
124+
permPR[i] = perm.CanRead(unit.TypePullRequests)
125+
}
126+
132127
for _, act := range acts {
133-
if err := notifyWatchers(ctx, act); err != nil {
128+
if act.RepoID != repoID {
129+
setting.PanicInDevOrTesting("action should belong to the same repo, expected[%d], got[%d] ", repoID, act.RepoID)
130+
}
131+
if act.ActUserID != actUserID {
132+
setting.PanicInDevOrTesting("action should have the same actor, expected[%d], got[%d] ", actUserID, act.ActUserID)
133+
}
134+
135+
act.Repo = repo
136+
if err := notifyWatchers(ctx, act, watchers, permCode, permIssue, permPR); err != nil {
134137
return err
135138
}
136139
}

0 commit comments

Comments
 (0)