From 7854178da4ff3a432135c28e664b7daca6710bad Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sat, 26 Nov 2022 20:51:45 +0800 Subject: [PATCH] chore: fix duplicate to satisfy --- models/issues/comment_list.go | 19 +++-------------- models/issues/issue_list.go | 39 +++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/models/issues/comment_list.go b/models/issues/comment_list.go index aeba51c2a8..c12fff4a88 100644 --- a/models/issues/comment_list.go +++ b/models/issues/comment_list.go @@ -30,22 +30,9 @@ func (comments CommentList) LoadPosters(ctx context.Context) error { return nil } - posterIDs := comments.getPosterIDs() - posterMaps := make(map[int64]*user_model.User, len(posterIDs)) - left := len(posterIDs) - for left > 0 { - limit := db.DefaultMaxInSize - if left < limit { - limit = left - } - err := db.GetEngine(ctx). - In("id", posterIDs[:limit]). - Find(&posterMaps) - if err != nil { - return err - } - left -= limit - posterIDs = posterIDs[limit:] + posterMaps, err := getPosters(ctx, comments.getPosterIDs()) + if err != nil { + return err } for _, comment := range comments { diff --git a/models/issues/issue_list.go b/models/issues/issue_list.go index 2cdf2f2fbb..07812a4a41 100644 --- a/models/issues/issue_list.go +++ b/models/issues/issue_list.go @@ -87,22 +87,9 @@ func (issues IssueList) loadPosters(ctx context.Context) error { return nil } - posterIDs := issues.getPosterIDs() - posterMaps := make(map[int64]*user_model.User, len(posterIDs)) - left := len(posterIDs) - for left > 0 { - limit := db.DefaultMaxInSize - if left < limit { - limit = left - } - err := db.GetEngine(ctx). - In("id", posterIDs[:limit]). - Find(&posterMaps) - if err != nil { - return err - } - left -= limit - posterIDs = posterIDs[limit:] + posterMaps, err := getPosters(ctx, issues.getPosterIDs()) + if err != nil { + return err } for _, issue := range issues { @@ -120,6 +107,26 @@ func (issues IssueList) loadPosters(ctx context.Context) error { return nil } +func getPosters(ctx context.Context, posterIDs []int64) (map[int64]*user_model.User, error) { + posterMaps := make(map[int64]*user_model.User, len(posterIDs)) + left := len(posterIDs) + for left > 0 { + limit := db.DefaultMaxInSize + if left < limit { + limit = left + } + err := db.GetEngine(ctx). + In("id", posterIDs[:limit]). + Find(&posterMaps) + if err != nil { + return nil, err + } + left -= limit + posterIDs = posterIDs[limit:] + } + return posterMaps, nil +} + func (issues IssueList) getIssueIDs() []int64 { ids := make([]int64, 0, len(issues)) for _, issue := range issues {