Skip to content
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

Add generic set type #21408

Merged
merged 7 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions models/activities/action_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
46 changes: 17 additions & 29 deletions models/activities/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -248,7 +242,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
return err
}
for _, id := range issueUnWatches {
delete(toNotify, id)
toNotify.Remove(id)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
64 changes: 24 additions & 40 deletions models/issues/comment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import (
type CommentList []*Comment

func (comments CommentList) getPosterIDs() []int64 {
posterIDs := make(map[int64]struct{}, len(comments))
posterIDs := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := posterIDs[comment.PosterID]; !ok {
posterIDs[comment.PosterID] = struct{}{}
}
posterIDs.Add(comment.PosterID)
}
return container.KeysInt64(posterIDs)
return posterIDs.Values()
}

func (comments CommentList) loadPosters(ctx context.Context) error {
Expand Down Expand Up @@ -70,13 +68,11 @@ func (comments CommentList) getCommentIDs() []int64 {
}

func (comments CommentList) getLabelIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := ids[comment.LabelID]; !ok {
ids[comment.LabelID] = struct{}{}
}
ids.Add(comment.LabelID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
Expand Down Expand Up @@ -120,13 +116,11 @@ func (comments CommentList) loadLabels(ctx context.Context) error { //nolint
}

func (comments CommentList) getMilestoneIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := ids[comment.MilestoneID]; !ok {
ids[comment.MilestoneID] = struct{}{}
}
ids.Add(comment.MilestoneID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadMilestones(ctx context.Context) error {
Expand Down Expand Up @@ -163,13 +157,11 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
}

func (comments CommentList) getOldMilestoneIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := ids[comment.OldMilestoneID]; !ok {
ids[comment.OldMilestoneID] = struct{}{}
}
ids.Add(comment.OldMilestoneID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadOldMilestones(ctx context.Context) error {
Expand Down Expand Up @@ -206,13 +198,11 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
}

func (comments CommentList) getAssigneeIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := ids[comment.AssigneeID]; !ok {
ids[comment.AssigneeID] = struct{}{}
}
ids.Add(comment.AssigneeID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadAssignees(ctx context.Context) error {
Expand Down Expand Up @@ -259,16 +249,14 @@ func (comments CommentList) loadAssignees(ctx context.Context) error {

// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
func (comments CommentList) getIssueIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.Issue != nil {
continue
}
if _, ok := ids[comment.IssueID]; !ok {
ids[comment.IssueID] = struct{}{}
}
ids.Add(comment.IssueID)
}
return container.KeysInt64(ids)
return ids.Values()
}

// Issues returns all the issues of comments
Expand Down Expand Up @@ -334,16 +322,14 @@ func (comments CommentList) loadIssues(ctx context.Context) error {
}

func (comments CommentList) getDependentIssueIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if comment.DependentIssue != nil {
continue
}
if _, ok := ids[comment.DependentIssueID]; !ok {
ids[comment.DependentIssueID] = struct{}{}
}
ids.Add(comment.DependentIssueID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadDependentIssues(ctx context.Context) error {
Expand Down Expand Up @@ -439,13 +425,11 @@ func (comments CommentList) loadAttachments(ctx context.Context) (err error) {
}

func (comments CommentList) getReviewIDs() []int64 {
ids := make(map[int64]struct{}, len(comments))
ids := make(container.Set[int64], len(comments))
for _, comment := range comments {
if _, ok := ids[comment.ReviewID]; !ok {
ids[comment.ReviewID] = struct{}{}
}
ids.Add(comment.ReviewID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (comments CommentList) loadReviews(ctx context.Context) error { //nolint
Expand Down
33 changes: 13 additions & 20 deletions models/issues/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ type IssueList []*Issue

// get the repo IDs to be loaded later, these IDs are for issue.Repo and issue.PullRequest.HeadRepo
func (issues IssueList) getRepoIDs() []int64 {
repoIDs := make(map[int64]struct{}, len(issues))
repoIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
if issue.Repo == nil {
repoIDs[issue.RepoID] = struct{}{}
repoIDs.Add(issue.RepoID)
}
if issue.PullRequest != nil && issue.PullRequest.HeadRepo == nil {
repoIDs[issue.PullRequest.HeadRepoID] = struct{}{}
repoIDs.Add(issue.PullRequest.HeadRepoID)
}
}
return container.KeysInt64(repoIDs)
return repoIDs.Values()
}

func (issues IssueList) loadRepositories(ctx context.Context) ([]*repo_model.Repository, error) {
Expand Down Expand Up @@ -79,13 +79,11 @@ func (issues IssueList) LoadRepositories() ([]*repo_model.Repository, error) {
}

func (issues IssueList) getPosterIDs() []int64 {
posterIDs := make(map[int64]struct{}, len(issues))
posterIDs := make(container.Set[int64], len(issues))
for _, issue := range issues {
if _, ok := posterIDs[issue.PosterID]; !ok {
posterIDs[issue.PosterID] = struct{}{}
}
posterIDs.Add(issue.PosterID)
}
return container.KeysInt64(posterIDs)
return posterIDs.Values()
}

func (issues IssueList) loadPosters(ctx context.Context) error {
Expand Down Expand Up @@ -185,13 +183,11 @@ func (issues IssueList) loadLabels(ctx context.Context) error {
}

func (issues IssueList) getMilestoneIDs() []int64 {
ids := make(map[int64]struct{}, len(issues))
ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
if _, ok := ids[issue.MilestoneID]; !ok {
ids[issue.MilestoneID] = struct{}{}
}
ids.Add(issue.MilestoneID)
}
return container.KeysInt64(ids)
return ids.Values()
}

func (issues IssueList) loadMilestones(ctx context.Context) error {
Expand Down Expand Up @@ -224,14 +220,11 @@ func (issues IssueList) loadMilestones(ctx context.Context) error {
}

func (issues IssueList) getProjectIDs() []int64 {
ids := make(map[int64]struct{}, len(issues))
ids := make(container.Set[int64], len(issues))
for _, issue := range issues {
projectID := issue.ProjectID()
if _, ok := ids[projectID]; !ok {
ids[projectID] = struct{}{}
}
ids.Add(issue.ProjectID())
}
return container.KeysInt64(ids)
return ids.Values()
}

func (issues IssueList) loadProjects(ctx context.Context) error {
Expand Down
10 changes: 4 additions & 6 deletions models/issues/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type ReactionOptions struct {

// CreateReaction creates reaction for issue or comment.
func CreateReaction(opts *ReactionOptions) (*Reaction, error) {
if !setting.UI.ReactionsMap[opts.Type] {
if !setting.UI.ReactionsLookup.Contains(opts.Type) {
return nil, ErrForbiddenIssueReaction{opts.Type}
}

Expand Down Expand Up @@ -316,16 +316,14 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
}

func (list ReactionList) getUserIDs() []int64 {
userIDs := make(map[int64]struct{}, len(list))
userIDs := make(container.Set[int64], len(list))
for _, reaction := range list {
if reaction.OriginalAuthor != "" {
continue
}
if _, ok := userIDs[reaction.UserID]; !ok {
userIDs[reaction.UserID] = struct{}{}
}
userIDs.Add(reaction.UserID)
}
return container.KeysInt64(userIDs)
return userIDs.Values()
}

func valuesUser(m map[int64]*user_model.User) []*user_model.User {
Expand Down
Loading