summaryrefslogtreecommitdiffstats
path: root/models/issue_comment_list.go
blob: 35c1253f6b358173a2e39bd64dfe6ea1f68e6056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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

// CommentList defines a list of comments
type CommentList []*Comment

func (comments CommentList) getPosterIDs() []int64 {
	commentIDs := make(map[int64]struct{}, len(comments))
	for _, comment := range comments {
		if _, ok := commentIDs[comment.PosterID]; !ok {
			commentIDs[comment.PosterID] = struct{}{}
		}
	}
	return keysInt64(commentIDs)
}

// LoadPosters loads posters from database
func (comments CommentList) LoadPosters() error {
	return comments.loadPosters(x)
}

func (comments CommentList) loadPosters(e Engine) error {
	if len(comments) == 0 {
		return nil
	}

	posterIDs := comments.getPosterIDs()
	posterMaps := make(map[int64]*User, len(posterIDs))
	var left = len(posterIDs)
	for left > 0 {
		var limit = defaultMaxInSize
		if left < limit {
			limit = left
		}
		err := e.
			In("id", posterIDs[:limit]).
			Find(&posterMaps)
		if err != nil {
			return err
		}
		left = left - limit
		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
}