@@ -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.
13077func 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