Skip to content

Commit fa758ae

Browse files
committed
remove stale issue watches; fix missing userID in issue_watch delete statement
1 parent 7d54006 commit fa758ae

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

models/issue_watch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func removeIssueWatchersByRepoID(e Engine, userID int64, repoID int64) error {
7979
_, err := e.
8080
Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID).
8181
Cols("is_watching", "updated_unix").
82+
Where("`issue_watch`.user_id = ?", userID).
8283
Update(iw)
8384
return err
8485
}

models/migrations/v67.go

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/modules/setting"
99

1010
"github.com/go-xorm/xorm"
11+
"code.gitea.io/gitea/modules/log"
1112
)
1213

1314
func removeStaleWatches(x *xorm.Engine) error {
@@ -17,6 +18,13 @@ func removeStaleWatches(x *xorm.Engine) error {
1718
RepoID int64
1819
}
1920

21+
type IssueWatch struct {
22+
ID int64
23+
UserID int64
24+
RepoID int64
25+
IsWatching bool
26+
}
27+
2028
type Repository struct {
2129
ID int64
2230
IsPrivate bool
@@ -57,8 +65,10 @@ func removeStaleWatches(x *xorm.Engine) error {
5765
return a.Mode, nil
5866
}
5967

68+
sess := x.NewSession()
69+
6070
repoCache := make(map[int64]*Repository)
61-
return x.BufferSize(setting.IterateBufferSize).Iterate(new(Watch),
71+
err := x.BufferSize(setting.IterateBufferSize).Iterate(new(Watch),
6272
func(idx int, bean interface{}) error {
6373
watch := bean.(*Watch)
6474

@@ -72,7 +82,7 @@ func removeStaleWatches(x *xorm.Engine) error {
7282
}
7383
}
7484

75-
// Remove watches from now unaccessible
85+
// Remove watches from now unaccessible repositories
7686
mode, err := accessLevel(watch.UserID, repo)
7787
if err != nil {
7888
return err
@@ -82,12 +92,64 @@ func removeStaleWatches(x *xorm.Engine) error {
8292
return nil
8393
}
8494

85-
sess := x.NewSession()
8695
if _, err = sess.Delete(&Watch{0, watch.UserID, repo.ID}); err != nil {
8796
return err
8897
}
8998
_, err = sess.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repo.ID)
9099

91-
return sess.Commit()
100+
return err
92101
})
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()
93155
}

0 commit comments

Comments
 (0)