Skip to content

Commit dd1acd7

Browse files
lunnytechknowlogick
authored andcommitted
Comments list performance optimization (#5305)
1 parent 2262811 commit dd1acd7

File tree

4 files changed

+553
-40
lines changed

4 files changed

+553
-40
lines changed

models/issue.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
272272
if err = issue.loadComments(e); err != nil {
273273
return err
274274
}
275+
276+
if err = CommentList(issue.Comments).loadAttributes(e); err != nil {
277+
return err
278+
}
275279
if issue.isTimetrackerEnabled(e) {
276280
if err = issue.loadTotalTimes(e); err != nil {
277281
return err

models/issue_comment.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ func (c *Comment) LoadIssue() (err error) {
154154
return
155155
}
156156

157+
func (c *Comment) loadPoster(e Engine) (err error) {
158+
if c.Poster != nil {
159+
return nil
160+
}
161+
162+
c.Poster, err = getUserByID(e, c.PosterID)
163+
if err != nil {
164+
if IsErrUserNotExist(err) {
165+
c.PosterID = -1
166+
c.Poster = NewGhostUser()
167+
} else {
168+
log.Error("getUserByID[%d]: %v", c.ID, err)
169+
}
170+
}
171+
return err
172+
}
173+
174+
func (c *Comment) loadAttachments(e Engine) (err error) {
175+
if len(c.Attachments) > 0 {
176+
return
177+
}
178+
c.Attachments, err = getAttachmentsByCommentID(e, c.ID)
179+
if err != nil {
180+
log.Error("getAttachmentsByCommentID[%d]: %v", c.ID, err)
181+
}
182+
return err
183+
}
184+
157185
// AfterDelete is invoked from XORM after the object is deleted.
158186
func (c *Comment) AfterDelete() {
159187
if c.ID <= 0 {
@@ -997,32 +1025,6 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
9971025
return findComments(x, opts)
9981026
}
9991027

1000-
// GetCommentsByIssueID returns all comments of an issue.
1001-
func GetCommentsByIssueID(issueID int64) ([]*Comment, error) {
1002-
return findComments(x, FindCommentsOptions{
1003-
IssueID: issueID,
1004-
Type: CommentTypeUnknown,
1005-
})
1006-
}
1007-
1008-
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
1009-
func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) {
1010-
return findComments(x, FindCommentsOptions{
1011-
IssueID: issueID,
1012-
Type: CommentTypeUnknown,
1013-
Since: since,
1014-
})
1015-
}
1016-
1017-
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
1018-
func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
1019-
return findComments(x, FindCommentsOptions{
1020-
RepoID: repoID,
1021-
Type: CommentTypeUnknown,
1022-
Since: since,
1023-
})
1024-
}
1025-
10261028
// UpdateComment updates information of comment.
10271029
func UpdateComment(doer *User, c *Comment, oldContent string) error {
10281030
if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
@@ -1039,6 +1041,9 @@ func UpdateComment(doer *User, c *Comment, oldContent string) error {
10391041
if err := c.Issue.LoadAttributes(); err != nil {
10401042
return err
10411043
}
1044+
if err := c.loadPoster(x); err != nil {
1045+
return err
1046+
}
10421047

10431048
mode, _ := AccessLevel(doer, c.Issue.Repo)
10441049
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
@@ -1087,6 +1092,7 @@ func DeleteComment(doer *User, comment *Comment) error {
10871092
if err := sess.Commit(); err != nil {
10881093
return err
10891094
}
1095+
sess.Close()
10901096

10911097
if err := comment.LoadPoster(); err != nil {
10921098
return err
@@ -1098,6 +1104,9 @@ func DeleteComment(doer *User, comment *Comment) error {
10981104
if err := comment.Issue.LoadAttributes(); err != nil {
10991105
return err
11001106
}
1107+
if err := comment.loadPoster(x); err != nil {
1108+
return err
1109+
}
11011110

11021111
mode, _ := AccessLevel(doer, comment.Issue.Repo)
11031112

@@ -1154,6 +1163,11 @@ func fetchCodeCommentsByReview(e Engine, issue *Issue, currentUser *User, review
11541163
if err := issue.loadRepo(e); err != nil {
11551164
return nil, err
11561165
}
1166+
1167+
if err := CommentList(comments).loadPosters(e); err != nil {
1168+
return nil, err
1169+
}
1170+
11571171
// Find all reviews by ReviewID
11581172
reviews := make(map[int64]*Review)
11591173
var ids = make([]int64, 0, len(comments))

0 commit comments

Comments
 (0)