You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. // CommentList defines a list of comments
  6. type CommentList []*Comment
  7. func (comments CommentList) getPosterIDs() []int64 {
  8. commentIDs := make(map[int64]struct{}, len(comments))
  9. for _, comment := range comments {
  10. if _, ok := commentIDs[comment.PosterID]; !ok {
  11. commentIDs[comment.PosterID] = struct{}{}
  12. }
  13. }
  14. return keysInt64(commentIDs)
  15. }
  16. // LoadPosters loads posters from database
  17. func (comments CommentList) LoadPosters() error {
  18. return comments.loadPosters(x)
  19. }
  20. func (comments CommentList) loadPosters(e Engine) error {
  21. if len(comments) == 0 {
  22. return nil
  23. }
  24. posterIDs := comments.getPosterIDs()
  25. posterMaps := make(map[int64]*User, len(posterIDs))
  26. var left = len(posterIDs)
  27. for left > 0 {
  28. var limit = defaultMaxInSize
  29. if left < limit {
  30. limit = left
  31. }
  32. err := e.
  33. In("id", posterIDs[:limit]).
  34. Find(&posterMaps)
  35. if err != nil {
  36. return err
  37. }
  38. left = left - limit
  39. posterIDs = posterIDs[limit:]
  40. }
  41. for _, comment := range comments {
  42. if comment.PosterID <= 0 {
  43. continue
  44. }
  45. var ok bool
  46. if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
  47. comment.Poster = NewGhostUser()
  48. }
  49. }
  50. return nil
  51. }