2018-12-13 18:55:43 +03:00
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
2021-09-19 14:49:59 +03:00
import "code.gitea.io/gitea/models/db"
2018-12-13 18:55:43 +03:00
// CommentList defines a list of comments
type CommentList [ ] * Comment
func ( comments CommentList ) getPosterIDs ( ) [ ] int64 {
2019-04-18 08:00:03 +03:00
posterIDs := make ( map [ int64 ] struct { } , len ( comments ) )
2018-12-13 18:55:43 +03:00
for _ , comment := range comments {
2019-04-18 08:00:03 +03:00
if _ , ok := posterIDs [ comment . PosterID ] ; ! ok {
posterIDs [ comment . PosterID ] = struct { } { }
2018-12-13 18:55:43 +03:00
}
}
2019-04-18 08:00:03 +03:00
return keysInt64 ( posterIDs )
2018-12-13 18:55:43 +03:00
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadPosters ( e db . Engine ) error {
2018-12-13 18:55:43 +03:00
if len ( comments ) == 0 {
return nil
}
posterIDs := comments . getPosterIDs ( )
posterMaps := make ( map [ int64 ] * User , len ( posterIDs ) )
2021-03-14 21:52:12 +03:00
left := len ( posterIDs )
2018-12-13 18:55:43 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2018-12-13 18:55:43 +03:00
if left < limit {
limit = left
}
err := e .
In ( "id" , posterIDs [ : limit ] ) .
Find ( & posterMaps )
if err != nil {
return err
}
2019-06-12 22:41:28 +03:00
left -= limit
2018-12-13 18:55:43 +03:00
posterIDs = posterIDs [ limit : ]
}
for _ , comment := range comments {
if comment . PosterID <= 0 {
continue
}
var ok bool
if comment . Poster , ok = posterMaps [ comment . PosterID ] ; ! ok {
comment . Poster = NewGhostUser ( )
}
}
return nil
}
2019-04-18 08:00:03 +03:00
func ( comments CommentList ) getCommentIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( [ ] int64 , 0 , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
ids = append ( ids , comment . ID )
}
return ids
}
func ( comments CommentList ) getLabelIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if _ , ok := ids [ comment . LabelID ] ; ! ok {
ids [ comment . LabelID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadLabels ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
labelIDs := comments . getLabelIDs ( )
commentLabels := make ( map [ int64 ] * Label , len ( labelIDs ) )
left := len ( labelIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e .
In ( "id" , labelIDs [ : limit ] ) .
Rows ( new ( Label ) )
if err != nil {
return err
}
for rows . Next ( ) {
var label Label
err = rows . Scan ( & label )
if err != nil {
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
return err
}
commentLabels [ label . ID ] = & label
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
left -= limit
2019-04-18 08:00:03 +03:00
labelIDs = labelIDs [ limit : ]
}
for _ , comment := range comments {
comment . Label = commentLabels [ comment . ID ]
}
return nil
}
func ( comments CommentList ) getMilestoneIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if _ , ok := ids [ comment . MilestoneID ] ; ! ok {
ids [ comment . MilestoneID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadMilestones ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
milestoneIDs := comments . getMilestoneIDs ( )
if len ( milestoneIDs ) == 0 {
return nil
}
milestoneMaps := make ( map [ int64 ] * Milestone , len ( milestoneIDs ) )
2021-03-14 21:52:12 +03:00
left := len ( milestoneIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
err := e .
In ( "id" , milestoneIDs [ : limit ] ) .
Find ( & milestoneMaps )
if err != nil {
return err
}
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
milestoneIDs = milestoneIDs [ limit : ]
}
for _ , issue := range comments {
issue . Milestone = milestoneMaps [ issue . MilestoneID ]
}
return nil
}
func ( comments CommentList ) getOldMilestoneIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if _ , ok := ids [ comment . OldMilestoneID ] ; ! ok {
ids [ comment . OldMilestoneID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadOldMilestones ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
milestoneIDs := comments . getOldMilestoneIDs ( )
if len ( milestoneIDs ) == 0 {
return nil
}
milestoneMaps := make ( map [ int64 ] * Milestone , len ( milestoneIDs ) )
2021-03-14 21:52:12 +03:00
left := len ( milestoneIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
err := e .
In ( "id" , milestoneIDs [ : limit ] ) .
Find ( & milestoneMaps )
if err != nil {
return err
}
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
milestoneIDs = milestoneIDs [ limit : ]
}
for _ , issue := range comments {
issue . OldMilestone = milestoneMaps [ issue . MilestoneID ]
}
return nil
}
func ( comments CommentList ) getAssigneeIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if _ , ok := ids [ comment . AssigneeID ] ; ! ok {
ids [ comment . AssigneeID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadAssignees ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
assigneeIDs := comments . getAssigneeIDs ( )
assignees := make ( map [ int64 ] * User , len ( assigneeIDs ) )
left := len ( assigneeIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e .
In ( "id" , assigneeIDs [ : limit ] ) .
Rows ( new ( User ) )
if err != nil {
return err
}
for rows . Next ( ) {
var user User
err = rows . Scan ( & user )
if err != nil {
rows . Close ( )
return err
}
assignees [ user . ID ] = & user
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
assigneeIDs = assigneeIDs [ limit : ]
}
for _ , comment := range comments {
comment . Assignee = assignees [ comment . AssigneeID ]
}
return nil
}
// getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
func ( comments CommentList ) getIssueIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if comment . Issue != nil {
continue
}
if _ , ok := ids [ comment . IssueID ] ; ! ok {
ids [ comment . IssueID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
// Issues returns all the issues of comments
func ( comments CommentList ) Issues ( ) IssueList {
2021-03-14 21:52:12 +03:00
issues := make ( map [ int64 ] * Issue , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if comment . Issue != nil {
if _ , ok := issues [ comment . Issue . ID ] ; ! ok {
issues [ comment . Issue . ID ] = comment . Issue
}
}
}
2021-03-14 21:52:12 +03:00
issueList := make ( [ ] * Issue , 0 , len ( issues ) )
2019-04-18 08:00:03 +03:00
for _ , issue := range issues {
issueList = append ( issueList , issue )
}
return issueList
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadIssues ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
issueIDs := comments . getIssueIDs ( )
issues := make ( map [ int64 ] * Issue , len ( issueIDs ) )
left := len ( issueIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e .
In ( "id" , issueIDs [ : limit ] ) .
Rows ( new ( Issue ) )
if err != nil {
return err
}
for rows . Next ( ) {
var issue Issue
err = rows . Scan ( & issue )
if err != nil {
rows . Close ( )
return err
}
issues [ issue . ID ] = & issue
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
issueIDs = issueIDs [ limit : ]
}
for _ , comment := range comments {
if comment . Issue == nil {
comment . Issue = issues [ comment . IssueID ]
}
}
return nil
}
func ( comments CommentList ) getDependentIssueIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if comment . DependentIssue != nil {
continue
}
if _ , ok := ids [ comment . DependentIssueID ] ; ! ok {
ids [ comment . DependentIssueID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadDependentIssues ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
issueIDs := comments . getDependentIssueIDs ( )
issues := make ( map [ int64 ] * Issue , len ( issueIDs ) )
left := len ( issueIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e .
In ( "id" , issueIDs [ : limit ] ) .
Rows ( new ( Issue ) )
if err != nil {
return err
}
for rows . Next ( ) {
var issue Issue
err = rows . Scan ( & issue )
if err != nil {
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
return err
}
issues [ issue . ID ] = & issue
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
issueIDs = issueIDs [ limit : ]
}
for _ , comment := range comments {
if comment . DependentIssue == nil {
comment . DependentIssue = issues [ comment . DependentIssueID ]
2020-01-21 13:18:52 +03:00
if comment . DependentIssue != nil {
if err := comment . DependentIssue . loadRepo ( e ) ; err != nil {
return err
}
}
2019-04-18 08:00:03 +03:00
}
}
return nil
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadAttachments ( e db . Engine ) ( err error ) {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
attachments := make ( map [ int64 ] [ ] * Attachment , len ( comments ) )
commentsIDs := comments . getCommentIDs ( )
left := len ( commentsIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e . Table ( "attachment" ) .
Join ( "INNER" , "comment" , "comment.id = attachment.comment_id" ) .
In ( "comment.id" , commentsIDs [ : limit ] ) .
Rows ( new ( Attachment ) )
if err != nil {
return err
}
for rows . Next ( ) {
var attachment Attachment
err = rows . Scan ( & attachment )
if err != nil {
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
return err
}
attachments [ attachment . CommentID ] = append ( attachments [ attachment . CommentID ] , & attachment )
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
left -= limit
2019-04-18 08:00:03 +03:00
commentsIDs = commentsIDs [ limit : ]
}
for _ , comment := range comments {
comment . Attachments = attachments [ comment . ID ]
}
return nil
}
func ( comments CommentList ) getReviewIDs ( ) [ ] int64 {
2021-03-14 21:52:12 +03:00
ids := make ( map [ int64 ] struct { } , len ( comments ) )
2019-04-18 08:00:03 +03:00
for _ , comment := range comments {
if _ , ok := ids [ comment . ReviewID ] ; ! ok {
ids [ comment . ReviewID ] = struct { } { }
}
}
return keysInt64 ( ids )
}
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadReviews ( e db . Engine ) error {
2019-04-18 08:00:03 +03:00
if len ( comments ) == 0 {
return nil
}
2021-03-14 21:52:12 +03:00
reviewIDs := comments . getReviewIDs ( )
reviews := make ( map [ int64 ] * Review , len ( reviewIDs ) )
left := len ( reviewIDs )
2019-04-18 08:00:03 +03:00
for left > 0 {
2021-03-14 21:52:12 +03:00
limit := defaultMaxInSize
2019-04-18 08:00:03 +03:00
if left < limit {
limit = left
}
rows , err := e .
In ( "id" , reviewIDs [ : limit ] ) .
Rows ( new ( Review ) )
if err != nil {
return err
}
for rows . Next ( ) {
var review Review
err = rows . Scan ( & review )
if err != nil {
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
return err
}
reviews [ review . ID ] = & review
}
2019-06-12 22:41:28 +03:00
_ = rows . Close ( )
2019-04-18 08:00:03 +03:00
2019-06-12 22:41:28 +03:00
left -= limit
2019-04-18 08:00:03 +03:00
reviewIDs = reviewIDs [ limit : ]
}
for _ , comment := range comments {
comment . Review = reviews [ comment . ReviewID ]
}
return nil
}
// loadAttributes loads all attributes
2021-09-19 14:49:59 +03:00
func ( comments CommentList ) loadAttributes ( e db . Engine ) ( err error ) {
2019-04-18 08:00:03 +03:00
if err = comments . loadPosters ( e ) ; err != nil {
return
}
if err = comments . loadLabels ( e ) ; err != nil {
return
}
if err = comments . loadMilestones ( e ) ; err != nil {
return
}
if err = comments . loadOldMilestones ( e ) ; err != nil {
return
}
if err = comments . loadAssignees ( e ) ; err != nil {
return
}
if err = comments . loadAttachments ( e ) ; err != nil {
return
}
if err = comments . loadReviews ( e ) ; err != nil {
return
}
if err = comments . loadIssues ( e ) ; err != nil {
return
}
if err = comments . loadDependentIssues ( e ) ; err != nil {
return
}
return nil
}
// LoadAttributes loads attributes of the comments, except for attachments and
// comments
func ( comments CommentList ) LoadAttributes ( ) error {
2021-09-23 18:45:36 +03:00
return comments . loadAttributes ( db . GetEngine ( db . DefaultContext ) )
2019-04-18 08:00:03 +03:00
}
// LoadAttachments loads attachments
func ( comments CommentList ) LoadAttachments ( ) error {
2021-09-23 18:45:36 +03:00
return comments . loadAttachments ( db . GetEngine ( db . DefaultContext ) )
2019-04-18 08:00:03 +03:00
}
// LoadPosters loads posters
func ( comments CommentList ) LoadPosters ( ) error {
2021-09-23 18:45:36 +03:00
return comments . loadPosters ( db . GetEngine ( db . DefaultContext ) )
2019-04-18 08:00:03 +03:00
}
// LoadIssues loads issues of comments
func ( comments CommentList ) LoadIssues ( ) error {
2021-09-23 18:45:36 +03:00
return comments . loadIssues ( db . GetEngine ( db . DefaultContext ) )
2019-04-18 08:00:03 +03:00
}