@@ -19,7 +19,9 @@ type CommentList []*Comment
1919func  (comments  CommentList ) getPosterIDs () []int64  {
2020	posterIDs  :=  make (container.Set [int64 ], len (comments ))
2121	for  _ , comment  :=  range  comments  {
22- 		posterIDs .Add (comment .PosterID )
22+ 		if  comment .PosterID  >  0  {
23+ 			posterIDs .Add (comment .PosterID )
24+ 		}
2325	}
2426	return  posterIDs .Values ()
2527}
@@ -41,18 +43,12 @@ func (comments CommentList) LoadPosters(ctx context.Context) error {
4143	return  nil 
4244}
4345
44- func  (comments  CommentList ) getCommentIDs () []int64  {
45- 	ids  :=  make ([]int64 , 0 , len (comments ))
46- 	for  _ , comment  :=  range  comments  {
47- 		ids  =  append (ids , comment .ID )
48- 	}
49- 	return  ids 
50- }
51- 
5246func  (comments  CommentList ) getLabelIDs () []int64  {
5347	ids  :=  make (container.Set [int64 ], len (comments ))
5448	for  _ , comment  :=  range  comments  {
55- 		ids .Add (comment .LabelID )
49+ 		if  comment .LabelID  >  0  {
50+ 			ids .Add (comment .LabelID )
51+ 		}
5652	}
5753	return  ids .Values ()
5854}
@@ -100,7 +96,9 @@ func (comments CommentList) loadLabels(ctx context.Context) error {
10096func  (comments  CommentList ) getMilestoneIDs () []int64  {
10197	ids  :=  make (container.Set [int64 ], len (comments ))
10298	for  _ , comment  :=  range  comments  {
103- 		ids .Add (comment .MilestoneID )
99+ 		if  comment .MilestoneID  >  0  {
100+ 			ids .Add (comment .MilestoneID )
101+ 		}
104102	}
105103	return  ids .Values ()
106104}
@@ -141,7 +139,9 @@ func (comments CommentList) loadMilestones(ctx context.Context) error {
141139func  (comments  CommentList ) getOldMilestoneIDs () []int64  {
142140	ids  :=  make (container.Set [int64 ], len (comments ))
143141	for  _ , comment  :=  range  comments  {
144- 		ids .Add (comment .OldMilestoneID )
142+ 		if  comment .OldMilestoneID  >  0  {
143+ 			ids .Add (comment .OldMilestoneID )
144+ 		}
145145	}
146146	return  ids .Values ()
147147}
@@ -182,7 +182,9 @@ func (comments CommentList) loadOldMilestones(ctx context.Context) error {
182182func  (comments  CommentList ) getAssigneeIDs () []int64  {
183183	ids  :=  make (container.Set [int64 ], len (comments ))
184184	for  _ , comment  :=  range  comments  {
185- 		ids .Add (comment .AssigneeID )
185+ 		if  comment .AssigneeID  >  0  {
186+ 			ids .Add (comment .AssigneeID )
187+ 		}
186188	}
187189	return  ids .Values ()
188190}
@@ -314,7 +316,9 @@ func (comments CommentList) getDependentIssueIDs() []int64 {
314316		if  comment .DependentIssue  !=  nil  {
315317			continue 
316318		}
317- 		ids .Add (comment .DependentIssueID )
319+ 		if  comment .DependentIssueID  >  0  {
320+ 			ids .Add (comment .DependentIssueID )
321+ 		}
318322	}
319323	return  ids .Values ()
320324}
@@ -369,23 +373,57 @@ func (comments CommentList) loadDependentIssues(ctx context.Context) error {
369373	return  nil 
370374}
371375
376+ // getAttachmentCommentIDs only return the comment ids which possibly has attachments 
377+ func  (comments  CommentList ) getAttachmentCommentIDs () []int64  {
378+ 	ids  :=  make (container.Set [int64 ], len (comments ))
379+ 	for  _ , comment  :=  range  comments  {
380+ 		if  comment .Type  ==  CommentTypeComment  || 
381+ 			comment .Type  ==  CommentTypeReview  || 
382+ 			comment .Type  ==  CommentTypeCode  {
383+ 			ids .Add (comment .ID )
384+ 		}
385+ 	}
386+ 	return  ids .Values ()
387+ }
388+ 
389+ // LoadAttachmentsByIssue loads attachments by issue id 
390+ func  (comments  CommentList ) LoadAttachmentsByIssue (ctx  context.Context ) error  {
391+ 	if  len (comments ) ==  0  {
392+ 		return  nil 
393+ 	}
394+ 
395+ 	attachments  :=  make ([]* repo_model.Attachment , 0 , len (comments )/ 2 )
396+ 	if  err  :=  db .GetEngine (ctx ).Where ("issue_id=? AND comment_id>0" , comments [0 ].IssueID ).Find (& attachments ); err  !=  nil  {
397+ 		return  err 
398+ 	}
399+ 
400+ 	commentAttachmentsMap  :=  make (map [int64 ][]* repo_model.Attachment , len (comments ))
401+ 	for  _ , attach  :=  range  attachments  {
402+ 		commentAttachmentsMap [attach .CommentID ] =  append (commentAttachmentsMap [attach .CommentID ], attach )
403+ 	}
404+ 
405+ 	for  _ , comment  :=  range  comments  {
406+ 		comment .Attachments  =  commentAttachmentsMap [comment .ID ]
407+ 	}
408+ 	return  nil 
409+ }
410+ 
372411// LoadAttachments loads attachments 
373412func  (comments  CommentList ) LoadAttachments (ctx  context.Context ) (err  error ) {
374413	if  len (comments ) ==  0  {
375414		return  nil 
376415	}
377416
378417	attachments  :=  make (map [int64 ][]* repo_model.Attachment , len (comments ))
379- 	commentsIDs  :=  comments .getCommentIDs ()
418+ 	commentsIDs  :=  comments .getAttachmentCommentIDs ()
380419	left  :=  len (commentsIDs )
381420	for  left  >  0  {
382421		limit  :=  db .DefaultMaxInSize 
383422		if  left  <  limit  {
384423			limit  =  left 
385424		}
386- 		rows , err  :=  db .GetEngine (ctx ).Table ("attachment" ).
387- 			Join ("INNER" , "comment" , "comment.id = attachment.comment_id" ).
388- 			In ("comment.id" , commentsIDs [:limit ]).
425+ 		rows , err  :=  db .GetEngine (ctx ).
426+ 			In ("comment_id" , commentsIDs [:limit ]).
389427			Rows (new (repo_model.Attachment ))
390428		if  err  !=  nil  {
391429			return  err 
@@ -415,7 +453,9 @@ func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
415453func  (comments  CommentList ) getReviewIDs () []int64  {
416454	ids  :=  make (container.Set [int64 ], len (comments ))
417455	for  _ , comment  :=  range  comments  {
418- 		ids .Add (comment .ReviewID )
456+ 		if  comment .ReviewID  >  0  {
457+ 			ids .Add (comment .ReviewID )
458+ 		}
419459	}
420460	return  ids .Values ()
421461}
0 commit comments