-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix not removed watches on unallowed repositories #4201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4baa117
remove watches if user gets removed from team, and team is deleted as…
daviian 4774973
let new team member automatically watch all repositories
daviian a7a0b85
remove watch if user gets removed from collaborators
daviian ffc60ff
undo unneeded change
daviian fd3cf7e
remove stale watches
daviian 7d54006
remove issue_watches when team member is removed, team repository is …
daviian fa758ae
remove stale issue watches; fix missing userID in issue_watch delete …
daviian dcd1cba
add missing beginning and closing of session in migration
daviian edb0169
change to correct user id
daviian 39f51c5
remove debug log output from migration
daviian 4525560
build up cache
daviian ab794a3
Merge branch 'master' into bugfix/watches
daviian 75f9efb
Merge branch 'master' into bugfix/watches
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2018 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
"github.com/go-xorm/xorm" | ||
) | ||
|
||
func removeStaleWatches(x *xorm.Engine) error { | ||
type Watch struct { | ||
ID int64 | ||
UserID int64 | ||
RepoID int64 | ||
} | ||
|
||
type IssueWatch struct { | ||
ID int64 | ||
UserID int64 | ||
RepoID int64 | ||
IsWatching bool | ||
} | ||
|
||
type Repository struct { | ||
ID int64 | ||
IsPrivate bool | ||
OwnerID int64 | ||
} | ||
|
||
type Access struct { | ||
UserID int64 | ||
RepoID int64 | ||
Mode int | ||
} | ||
|
||
const ( | ||
// AccessModeNone no access | ||
AccessModeNone int = iota // 0 | ||
// AccessModeRead read access | ||
AccessModeRead // 1 | ||
) | ||
|
||
accessLevel := func(userID int64, repo *Repository) (int, error) { | ||
mode := AccessModeNone | ||
if !repo.IsPrivate { | ||
mode = AccessModeRead | ||
} | ||
|
||
if userID == 0 { | ||
return mode, nil | ||
} | ||
|
||
if userID == repo.OwnerID { | ||
return 4, nil | ||
} | ||
|
||
a := &Access{UserID: userID, RepoID: repo.ID} | ||
if has, err := x.Get(a); !has || err != nil { | ||
return mode, err | ||
} | ||
return a.Mode, nil | ||
} | ||
|
||
sess := x.NewSession() | ||
defer sess.Close() | ||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
repoCache := make(map[int64]*Repository) | ||
err := x.BufferSize(setting.IterateBufferSize).Iterate(new(Watch), | ||
func(idx int, bean interface{}) error { | ||
watch := bean.(*Watch) | ||
|
||
repo := repoCache[watch.RepoID] | ||
if repo == nil { | ||
repo = &Repository{ | ||
ID: watch.RepoID, | ||
} | ||
if _, err := x.Get(repo); err != nil { | ||
return err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should |
||
repoCache[watch.RepoID] = repo | ||
} | ||
|
||
// Remove watches from now unaccessible repositories | ||
mode, err := accessLevel(watch.UserID, repo) | ||
if err != nil { | ||
return err | ||
} | ||
has := AccessModeRead <= mode | ||
if has { | ||
return nil | ||
} | ||
|
||
if _, err = sess.Delete(&Watch{0, watch.UserID, repo.ID}); err != nil { | ||
return err | ||
} | ||
_, err = sess.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repo.ID) | ||
|
||
return err | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
repoCache = make(map[int64]*Repository) | ||
err = x.BufferSize(setting.IterateBufferSize). | ||
Distinct("issue_watch.user_id", "issue.repo_id"). | ||
Join("INNER", "issue", "issue_watch.issue_id = issue.id"). | ||
Where("issue_watch.is_watching = ?", true). | ||
Iterate(new(IssueWatch), | ||
func(idx int, bean interface{}) error { | ||
watch := bean.(*IssueWatch) | ||
|
||
repo := repoCache[watch.RepoID] | ||
if repo == nil { | ||
repo = &Repository{ | ||
ID: watch.RepoID, | ||
} | ||
if _, err := x.Get(repo); err != nil { | ||
return err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as above |
||
repoCache[watch.RepoID] = repo | ||
} | ||
|
||
// Remove issue watches from now unaccssible repositories | ||
mode, err := accessLevel(watch.UserID, repo) | ||
if err != nil { | ||
return err | ||
} | ||
has := AccessModeRead <= mode | ||
if has { | ||
return nil | ||
} | ||
|
||
iw := &IssueWatch{ | ||
IsWatching: false, | ||
} | ||
|
||
_, err = sess. | ||
Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", watch.RepoID). | ||
Cols("is_watching", "updated_unix"). | ||
Where("`issue_watch`.user_id = ?", watch.UserID). | ||
Update(iw) | ||
|
||
return err | ||
|
||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return sess.Commit() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes. Totally forgot.