diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-20 22:08:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-20 22:08:52 +0800 |
commit | fd7d83ace60258acf7139c4c787aa8af75b7ba8c (patch) | |
tree | 50038348ec10485f72344f3ac80324e04abc1283 /models/issue_watch.go | |
parent | d81e31ad7826a81fc7139f329f250594610a274b (diff) | |
download | gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.tar.gz gitea-fd7d83ace60258acf7139c4c787aa8af75b7ba8c.zip |
Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context
* remove some unnecessary wrap functions
Diffstat (limited to 'models/issue_watch.go')
-rw-r--r-- | models/issue_watch.go | 43 |
1 files changed, 14 insertions, 29 deletions
diff --git a/models/issue_watch.go b/models/issue_watch.go index 92dc847415..9f41d36e19 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -5,6 +5,8 @@ package models import ( + "context" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" @@ -30,7 +32,7 @@ type IssueWatchList []*IssueWatch // CreateOrUpdateIssueWatch set watching for a user and issue func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error { - iw, exists, err := getIssueWatch(db.GetEngine(db.DefaultContext), userID, issueID) + iw, exists, err := GetIssueWatch(db.DefaultContext, userID, issueID) if err != nil { return err } @@ -57,14 +59,9 @@ func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error { // GetIssueWatch returns all IssueWatch objects from db by user and issue // the current Web-UI need iw object for watchers AND explicit non-watchers -func GetIssueWatch(userID, issueID int64) (iw *IssueWatch, exists bool, err error) { - return getIssueWatch(db.GetEngine(db.DefaultContext), userID, issueID) -} - -// Return watcher AND explicit non-watcher if entry in db exist -func getIssueWatch(e db.Engine, userID, issueID int64) (iw *IssueWatch, exists bool, err error) { +func GetIssueWatch(ctx context.Context, userID, issueID int64) (iw *IssueWatch, exists bool, err error) { iw = new(IssueWatch) - exists, err = e. + exists, err = db.GetEngine(ctx). Where("user_id = ?", userID). And("issue_id = ?", issueID). Get(iw) @@ -74,7 +71,7 @@ func getIssueWatch(e db.Engine, userID, issueID int64) (iw *IssueWatch, exists b // CheckIssueWatch check if an user is watching an issue // it takes participants and repo watch into account func CheckIssueWatch(user *user_model.User, issue *Issue) (bool, error) { - iw, exist, err := getIssueWatch(db.GetEngine(db.DefaultContext), user.ID, issue.ID) + iw, exist, err := GetIssueWatch(db.DefaultContext, user.ID, issue.ID) if err != nil { return false, err } @@ -91,13 +88,9 @@ func CheckIssueWatch(user *user_model.User, issue *Issue) (bool, error) { // GetIssueWatchersIDs returns IDs of subscribers or explicit unsubscribers to a given issue id // but avoids joining with `user` for performance reasons // User permissions must be verified elsewhere if required -func GetIssueWatchersIDs(issueID int64, watching bool) ([]int64, error) { - return getIssueWatchersIDs(db.GetEngine(db.DefaultContext), issueID, watching) -} - -func getIssueWatchersIDs(e db.Engine, issueID int64, watching bool) ([]int64, error) { +func GetIssueWatchersIDs(ctx context.Context, issueID int64, watching bool) ([]int64, error) { ids := make([]int64, 0, 64) - return ids, e.Table("issue_watch"). + return ids, db.GetEngine(ctx).Table("issue_watch"). Where("issue_id=?", issueID). And("is_watching = ?", watching). Select("user_id"). @@ -105,12 +98,8 @@ func getIssueWatchersIDs(e db.Engine, issueID int64, watching bool) ([]int64, er } // GetIssueWatchers returns watchers/unwatchers of a given issue -func GetIssueWatchers(issueID int64, listOptions db.ListOptions) (IssueWatchList, error) { - return getIssueWatchers(db.GetEngine(db.DefaultContext), issueID, listOptions) -} - -func getIssueWatchers(e db.Engine, issueID int64, listOptions db.ListOptions) (IssueWatchList, error) { - sess := e. +func GetIssueWatchers(ctx context.Context, issueID int64, listOptions db.ListOptions) (IssueWatchList, error) { + sess := db.GetEngine(ctx). Where("`issue_watch`.issue_id = ?", issueID). And("`issue_watch`.is_watching = ?", true). And("`user`.is_active = ?", true). @@ -127,12 +116,8 @@ func getIssueWatchers(e db.Engine, issueID int64, listOptions db.ListOptions) (I } // CountIssueWatchers count watchers/unwatchers of a given issue -func CountIssueWatchers(issueID int64) (int64, error) { - return countIssueWatchers(db.GetEngine(db.DefaultContext), issueID) -} - -func countIssueWatchers(e db.Engine, issueID int64) (int64, error) { - return e. +func CountIssueWatchers(ctx context.Context, issueID int64) (int64, error) { + return db.GetEngine(ctx). Where("`issue_watch`.issue_id = ?", issueID). And("`issue_watch`.is_watching = ?", true). And("`user`.is_active = ?", true). @@ -140,8 +125,8 @@ func countIssueWatchers(e db.Engine, issueID int64) (int64, error) { Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id").Count(new(IssueWatch)) } -func removeIssueWatchersByRepoID(e db.Engine, userID, repoID int64) error { - _, err := e. +func removeIssueWatchersByRepoID(ctx context.Context, userID, repoID int64) error { + _, err := db.GetEngine(ctx). Join("INNER", "issue", "`issue`.id = `issue_watch`.issue_id AND `issue`.repo_id = ?", repoID). Where("`issue_watch`.user_id = ?", userID). Delete(new(IssueWatch)) |