diff options
author | guillep2k <18600385+guillep2k@users.noreply.github.com> | 2019-11-18 05:08:20 -0300 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-11-18 08:08:20 +0000 |
commit | 08ae6bb7edb9582c38edb8a0dba1b1be10fb00fc (patch) | |
tree | c64e9a1c9cfaeb6cd0c2ee012d3ad32c38e78ce3 /models | |
parent | 9ff63126274b0df6e035541eafd48970c402e61e (diff) | |
download | gitea-08ae6bb7edb9582c38edb8a0dba1b1be10fb00fc.tar.gz gitea-08ae6bb7edb9582c38edb8a0dba1b1be10fb00fc.zip |
Rewrite delivery of issue and comment mails (#9009)
* Mail issue subscribers, rework the function
* Simplify a little more
* Fix unused variable
* Refactor mail delivery to avoid heavy load on server
* Avoid splitting into too many goroutines
* Fix comments and optimize GetMaileableUsersByIDs()
* Fix return on errors
Diffstat (limited to 'models')
-rw-r--r-- | models/issue.go | 13 | ||||
-rw-r--r-- | models/issue_assignees.go | 12 | ||||
-rw-r--r-- | models/issue_watch.go | 12 | ||||
-rw-r--r-- | models/repo_watch.go | 12 | ||||
-rw-r--r-- | models/user.go | 14 |
5 files changed, 63 insertions, 0 deletions
diff --git a/models/issue.go b/models/issue.go index 1fe9140dad..0113f0a404 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1219,6 +1219,19 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { return issues, nil } +// GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue, +// but skips joining with `user` for performance reasons. +// User permissions must be verified elsewhere if required. +func GetParticipantsIDsByIssueID(issueID int64) ([]int64, error) { + userIDs := make([]int64, 0, 5) + return userIDs, x.Table("comment"). + Cols("poster_id"). + Where("issue_id = ?", issueID). + And("type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview). + Distinct("poster_id"). + Find(&userIDs) +} + // GetParticipantsByIssueID returns all users who are participated in comments of an issue. func GetParticipantsByIssueID(issueID int64) ([]*User, error) { return getParticipantsByIssueID(x, issueID) diff --git a/models/issue_assignees.go b/models/issue_assignees.go index 4d71cc6e51..08a567c4eb 100644 --- a/models/issue_assignees.go +++ b/models/issue_assignees.go @@ -41,6 +41,18 @@ func (issue *Issue) loadAssignees(e Engine) (err error) { return } +// GetAssigneeIDsByIssue returns the IDs of users assigned to an issue +// but skips joining with `user` for performance reasons. +// User permissions must be verified elsewhere if required. +func GetAssigneeIDsByIssue(issueID int64) ([]int64, error) { + userIDs := make([]int64, 0, 5) + return userIDs, x.Table("issue_assignees"). + Cols("assignee_id"). + Where("issue_id = ?", issueID). + Distinct("assignee_id"). + Find(&userIDs) +} + // GetAssigneesByIssue returns everyone assigned to that issue func GetAssigneesByIssue(issue *Issue) (assignees []*User, err error) { return getAssigneesByIssue(x, issue) diff --git a/models/issue_watch.go b/models/issue_watch.go index 1ae0c9d474..3d7d48a125 100644 --- a/models/issue_watch.go +++ b/models/issue_watch.go @@ -60,6 +60,18 @@ func getIssueWatch(e Engine, userID, issueID int64) (iw *IssueWatch, exists bool return } +// GetIssueWatchersIDs returns IDs of subscribers 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) ([]int64, error) { + ids := make([]int64, 0, 64) + return ids, x.Table("issue_watch"). + Where("issue_id=?", issueID). + And("is_watching = ?", true). + Select("user_id"). + Find(&ids) +} + // GetIssueWatchers returns watchers/unwatchers of a given issue func GetIssueWatchers(issueID int64) (IssueWatchList, error) { return getIssueWatchers(x, issueID) diff --git a/models/repo_watch.go b/models/repo_watch.go index 2de4f8b320..2279dcb115 100644 --- a/models/repo_watch.go +++ b/models/repo_watch.go @@ -140,6 +140,18 @@ func GetWatchers(repoID int64) ([]*Watch, error) { return getWatchers(x, repoID) } +// GetRepoWatchersIDs returns IDs of watchers for a given repo ID +// but avoids joining with `user` for performance reasons +// User permissions must be verified elsewhere if required +func GetRepoWatchersIDs(repoID int64) ([]int64, error) { + ids := make([]int64, 0, 64) + return ids, x.Table("watch"). + Where("watch.repo_id=?", repoID). + And("watch.mode<>?", RepoWatchModeDont). + Select("user_id"). + Find(&ids) +} + // GetWatchers returns range of users watching given repository. func (repo *Repository) GetWatchers(page int) ([]*User, error) { users := make([]*User, 0, ItemsPerPage) diff --git a/models/user.go b/models/user.go index 4a8c644ccd..ce78e5bfce 100644 --- a/models/user.go +++ b/models/user.go @@ -1307,6 +1307,20 @@ func getUserEmailsByNames(e Engine, names []string) []string { return mails } +// GetMaileableUsersByIDs gets users from ids, but only if they can receive mails +func GetMaileableUsersByIDs(ids []int64) ([]*User, error) { + if len(ids) == 0 { + return nil, nil + } + ous := make([]*User, 0, len(ids)) + return ous, x.In("id", ids). + Where("`type` = ?", UserTypeIndividual). + And("`prohibit_login` = ?", false). + And("`is_active` = ?", true). + And("`email_notifications_preference` = ?", EmailNotificationsEnabled). + Find(&ous) +} + // GetUsersByIDs returns all resolved users from a list of Ids. func GetUsersByIDs(ids []int64) ([]*User, error) { ous := make([]*User, 0, len(ids)) |