diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-10-12 07:18:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-12 13:18:26 +0800 |
commit | 0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch) | |
tree | e5a92c55af5366924bd40ae14b4bf12842239193 /models/activities | |
parent | e84558b0931309cf1f4f2767bc47296483b9b3e1 (diff) | |
download | gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip |
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets.
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'models/activities')
-rw-r--r-- | models/activities/action_list.go | 16 | ||||
-rw-r--r-- | models/activities/notification.go | 46 |
2 files changed, 23 insertions, 39 deletions
diff --git a/models/activities/action_list.go b/models/activities/action_list.go index 16fb4bac8c..f349b94ac8 100644 --- a/models/activities/action_list.go +++ b/models/activities/action_list.go @@ -18,13 +18,11 @@ import ( type ActionList []*Action func (actions ActionList) getUserIDs() []int64 { - userIDs := make(map[int64]struct{}, len(actions)) + userIDs := make(container.Set[int64], len(actions)) for _, action := range actions { - if _, ok := userIDs[action.ActUserID]; !ok { - userIDs[action.ActUserID] = struct{}{} - } + userIDs.Add(action.ActUserID) } - return container.KeysInt64(userIDs) + return userIDs.Values() } func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.User, error) { @@ -48,13 +46,11 @@ func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model. } func (actions ActionList) getRepoIDs() []int64 { - repoIDs := make(map[int64]struct{}, len(actions)) + repoIDs := make(container.Set[int64], len(actions)) for _, action := range actions { - if _, ok := repoIDs[action.RepoID]; !ok { - repoIDs[action.RepoID] = struct{}{} - } + repoIDs.Add(action.RepoID) } - return container.KeysInt64(repoIDs) + return repoIDs.Values() } func (actions ActionList) loadRepositories(ctx context.Context) error { diff --git a/models/activities/notification.go b/models/activities/notification.go index 88776db42b..2f21dc74d1 100644 --- a/models/activities/notification.go +++ b/models/activities/notification.go @@ -200,7 +200,7 @@ func CreateOrUpdateIssueNotifications(issueID, commentID, notificationAuthorID, func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, notificationAuthorID, receiverID int64) error { // init - var toNotify map[int64]struct{} + var toNotify container.Set[int64] notifications, err := getNotificationsByIssueID(ctx, issueID) if err != nil { return err @@ -212,33 +212,27 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n } if receiverID > 0 { - toNotify = make(map[int64]struct{}, 1) - toNotify[receiverID] = struct{}{} + toNotify = make(container.Set[int64], 1) + toNotify.Add(receiverID) } else { - toNotify = make(map[int64]struct{}, 32) + toNotify = make(container.Set[int64], 32) issueWatches, err := issues_model.GetIssueWatchersIDs(ctx, issueID, true) if err != nil { return err } - for _, id := range issueWatches { - toNotify[id] = struct{}{} - } + toNotify.AddMultiple(issueWatches...) if !(issue.IsPull && issues_model.HasWorkInProgressPrefix(issue.Title)) { repoWatches, err := repo_model.GetRepoWatchersIDs(ctx, issue.RepoID) if err != nil { return err } - for _, id := range repoWatches { - toNotify[id] = struct{}{} - } + toNotify.AddMultiple(repoWatches...) } issueParticipants, err := issue.GetParticipantIDsByIssue(ctx) if err != nil { return err } - for _, id := range issueParticipants { - toNotify[id] = struct{}{} - } + toNotify.AddMultiple(issueParticipants...) // dont notify user who cause notification delete(toNotify, notificationAuthorID) @@ -248,7 +242,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n return err } for _, id := range issueUnWatches { - delete(toNotify, id) + toNotify.Remove(id) } } @@ -499,16 +493,14 @@ func (nl NotificationList) LoadAttributes() error { } func (nl NotificationList) getPendingRepoIDs() []int64 { - ids := make(map[int64]struct{}, len(nl)) + ids := make(container.Set[int64], len(nl)) for _, notification := range nl { if notification.Repository != nil { continue } - if _, ok := ids[notification.RepoID]; !ok { - ids[notification.RepoID] = struct{}{} - } + ids.Add(notification.RepoID) } - return container.KeysInt64(ids) + return ids.Values() } // LoadRepos loads repositories from database @@ -575,16 +567,14 @@ func (nl NotificationList) LoadRepos() (repo_model.RepositoryList, []int, error) } func (nl NotificationList) getPendingIssueIDs() []int64 { - ids := make(map[int64]struct{}, len(nl)) + ids := make(container.Set[int64], len(nl)) for _, notification := range nl { if notification.Issue != nil { continue } - if _, ok := ids[notification.IssueID]; !ok { - ids[notification.IssueID] = struct{}{} - } + ids.Add(notification.IssueID) } - return container.KeysInt64(ids) + return ids.Values() } // LoadIssues loads issues from database @@ -661,16 +651,14 @@ func (nl NotificationList) Without(failures []int) NotificationList { } func (nl NotificationList) getPendingCommentIDs() []int64 { - ids := make(map[int64]struct{}, len(nl)) + ids := make(container.Set[int64], len(nl)) for _, notification := range nl { if notification.CommentID == 0 || notification.Comment != nil { continue } - if _, ok := ids[notification.CommentID]; !ok { - ids[notification.CommentID] = struct{}{} - } + ids.Add(notification.CommentID) } - return container.KeysInt64(ids) + return ids.Values() } // LoadComments loads comments from database |