mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
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>
This commit is contained in:
@ -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 {
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user