8
8
"code.gitea.io/gitea/modules/setting"
9
9
10
10
"github.com/go-xorm/xorm"
11
+ "code.gitea.io/gitea/modules/log"
11
12
)
12
13
13
14
func removeStaleWatches (x * xorm.Engine ) error {
@@ -17,6 +18,13 @@ func removeStaleWatches(x *xorm.Engine) error {
17
18
RepoID int64
18
19
}
19
20
21
+ type IssueWatch struct {
22
+ ID int64
23
+ UserID int64
24
+ RepoID int64
25
+ IsWatching bool
26
+ }
27
+
20
28
type Repository struct {
21
29
ID int64
22
30
IsPrivate bool
@@ -57,8 +65,10 @@ func removeStaleWatches(x *xorm.Engine) error {
57
65
return a .Mode , nil
58
66
}
59
67
68
+ sess := x .NewSession ()
69
+
60
70
repoCache := make (map [int64 ]* Repository )
61
- return x .BufferSize (setting .IterateBufferSize ).Iterate (new (Watch ),
71
+ err := x .BufferSize (setting .IterateBufferSize ).Iterate (new (Watch ),
62
72
func (idx int , bean interface {}) error {
63
73
watch := bean .(* Watch )
64
74
@@ -72,7 +82,7 @@ func removeStaleWatches(x *xorm.Engine) error {
72
82
}
73
83
}
74
84
75
- // Remove watches from now unaccessible
85
+ // Remove watches from now unaccessible repositories
76
86
mode , err := accessLevel (watch .UserID , repo )
77
87
if err != nil {
78
88
return err
@@ -82,12 +92,64 @@ func removeStaleWatches(x *xorm.Engine) error {
82
92
return nil
83
93
}
84
94
85
- sess := x .NewSession ()
86
95
if _ , err = sess .Delete (& Watch {0 , watch .UserID , repo .ID }); err != nil {
87
96
return err
88
97
}
89
98
_ , err = sess .Exec ("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?" , repo .ID )
90
99
91
- return sess . Commit ()
100
+ return err
92
101
})
102
+ if err != nil {
103
+ return err
104
+ }
105
+
106
+ repoCache = make (map [int64 ]* Repository )
107
+ err = x .BufferSize (setting .IterateBufferSize ).
108
+ Distinct ("issue_watch.user_id" , "issue.repo_id" ).
109
+ Join ("INNER" , "issue" , "issue_watch.issue_id = issue.id" ).
110
+ Where ("issue_watch.is_watching = ?" , true ).
111
+ Iterate (new (IssueWatch ),
112
+ func (idx int , bean interface {}) error {
113
+ watch := bean .(* IssueWatch )
114
+
115
+ log .Info ("watch issues from repo %s" , watch .RepoID )
116
+
117
+ repo := repoCache [watch .RepoID ]
118
+ if repo == nil {
119
+ repo = & Repository {
120
+ ID : watch .RepoID ,
121
+ }
122
+ if _ , err := x .Get (repo ); err != nil {
123
+ return err
124
+ }
125
+ }
126
+
127
+ // Remove issue watches from now unaccssible repositories
128
+ mode , err := accessLevel (watch .UserID , repo )
129
+ if err != nil {
130
+ return err
131
+ }
132
+ has := AccessModeRead <= mode
133
+ if has {
134
+ return nil
135
+ }
136
+
137
+ iw := & IssueWatch {
138
+ IsWatching : false ,
139
+ }
140
+
141
+ _ , err = sess .
142
+ Join ("INNER" , "issue" , "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?" , watch .RepoID ).
143
+ Cols ("is_watching" , "updated_unix" ).
144
+ Where ("`issue_watch`.user_id = ?" , watch .UserID ).
145
+ Update (iw )
146
+
147
+ return err
148
+
149
+ })
150
+ if err != nil {
151
+ return err
152
+ }
153
+
154
+ return sess .Commit ()
93
155
}
0 commit comments