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.

comment_list.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "context"
  6. "code.gitea.io/gitea/models/db"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/container"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // CommentList defines a list of comments
  13. type CommentList []*Comment
  14. func (comments CommentList) getPosterIDs() []int64 {
  15. return container.FilterSlice(comments, func(c *Comment) (int64, bool) {
  16. return c.PosterID, c.PosterID > 0
  17. })
  18. }
  19. // LoadPosters loads posters
  20. func (comments CommentList) LoadPosters(ctx context.Context) error {
  21. if len(comments) == 0 {
  22. return nil
  23. }
  24. posterMaps, err := getPosters(ctx, comments.getPosterIDs())
  25. if err != nil {
  26. return err
  27. }
  28. for _, comment := range comments {
  29. comment.Poster = getPoster(comment.PosterID, posterMaps)
  30. }
  31. return nil
  32. }
  33. func (comments CommentList) getLabelIDs() []int64 {
  34. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  35. return comment.LabelID, comment.LabelID > 0
  36. })
  37. }
  38. func (comments CommentList) loadLabels(ctx context.Context) error {
  39. if len(comments) == 0 {
  40. return nil
  41. }
  42. labelIDs := comments.getLabelIDs()
  43. commentLabels := make(map[int64]*Label, len(labelIDs))
  44. left := len(labelIDs)
  45. for left > 0 {
  46. limit := db.DefaultMaxInSize
  47. if left < limit {
  48. limit = left
  49. }
  50. rows, err := db.GetEngine(ctx).
  51. In("id", labelIDs[:limit]).
  52. Rows(new(Label))
  53. if err != nil {
  54. return err
  55. }
  56. for rows.Next() {
  57. var label Label
  58. err = rows.Scan(&label)
  59. if err != nil {
  60. _ = rows.Close()
  61. return err
  62. }
  63. commentLabels[label.ID] = &label
  64. }
  65. _ = rows.Close()
  66. left -= limit
  67. labelIDs = labelIDs[limit:]
  68. }
  69. for _, comment := range comments {
  70. comment.Label = commentLabels[comment.ID]
  71. }
  72. return nil
  73. }
  74. func (comments CommentList) getMilestoneIDs() []int64 {
  75. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  76. return comment.MilestoneID, comment.MilestoneID > 0
  77. })
  78. }
  79. func (comments CommentList) loadMilestones(ctx context.Context) error {
  80. if len(comments) == 0 {
  81. return nil
  82. }
  83. milestoneIDs := comments.getMilestoneIDs()
  84. if len(milestoneIDs) == 0 {
  85. return nil
  86. }
  87. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  88. left := len(milestoneIDs)
  89. for left > 0 {
  90. limit := db.DefaultMaxInSize
  91. if left < limit {
  92. limit = left
  93. }
  94. err := db.GetEngine(ctx).
  95. In("id", milestoneIDs[:limit]).
  96. Find(&milestoneMaps)
  97. if err != nil {
  98. return err
  99. }
  100. left -= limit
  101. milestoneIDs = milestoneIDs[limit:]
  102. }
  103. for _, issue := range comments {
  104. issue.Milestone = milestoneMaps[issue.MilestoneID]
  105. }
  106. return nil
  107. }
  108. func (comments CommentList) getOldMilestoneIDs() []int64 {
  109. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  110. return comment.OldMilestoneID, comment.OldMilestoneID > 0
  111. })
  112. }
  113. func (comments CommentList) loadOldMilestones(ctx context.Context) error {
  114. if len(comments) == 0 {
  115. return nil
  116. }
  117. milestoneIDs := comments.getOldMilestoneIDs()
  118. if len(milestoneIDs) == 0 {
  119. return nil
  120. }
  121. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  122. left := len(milestoneIDs)
  123. for left > 0 {
  124. limit := db.DefaultMaxInSize
  125. if left < limit {
  126. limit = left
  127. }
  128. err := db.GetEngine(ctx).
  129. In("id", milestoneIDs[:limit]).
  130. Find(&milestoneMaps)
  131. if err != nil {
  132. return err
  133. }
  134. left -= limit
  135. milestoneIDs = milestoneIDs[limit:]
  136. }
  137. for _, issue := range comments {
  138. issue.OldMilestone = milestoneMaps[issue.MilestoneID]
  139. }
  140. return nil
  141. }
  142. func (comments CommentList) getAssigneeIDs() []int64 {
  143. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  144. return comment.AssigneeID, comment.AssigneeID > 0
  145. })
  146. }
  147. func (comments CommentList) loadAssignees(ctx context.Context) error {
  148. if len(comments) == 0 {
  149. return nil
  150. }
  151. assigneeIDs := comments.getAssigneeIDs()
  152. assignees := make(map[int64]*user_model.User, len(assigneeIDs))
  153. left := len(assigneeIDs)
  154. for left > 0 {
  155. limit := db.DefaultMaxInSize
  156. if left < limit {
  157. limit = left
  158. }
  159. rows, err := db.GetEngine(ctx).
  160. In("id", assigneeIDs[:limit]).
  161. Rows(new(user_model.User))
  162. if err != nil {
  163. return err
  164. }
  165. for rows.Next() {
  166. var user user_model.User
  167. err = rows.Scan(&user)
  168. if err != nil {
  169. rows.Close()
  170. return err
  171. }
  172. assignees[user.ID] = &user
  173. }
  174. _ = rows.Close()
  175. left -= limit
  176. assigneeIDs = assigneeIDs[limit:]
  177. }
  178. for _, comment := range comments {
  179. comment.Assignee = assignees[comment.AssigneeID]
  180. if comment.Assignee == nil {
  181. comment.AssigneeID = user_model.GhostUserID
  182. comment.Assignee = user_model.NewGhostUser()
  183. }
  184. }
  185. return nil
  186. }
  187. // getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
  188. func (comments CommentList) getIssueIDs() []int64 {
  189. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  190. return comment.IssueID, comment.Issue == nil
  191. })
  192. }
  193. // Issues returns all the issues of comments
  194. func (comments CommentList) Issues() IssueList {
  195. issues := make(map[int64]*Issue, len(comments))
  196. for _, comment := range comments {
  197. if comment.Issue != nil {
  198. if _, ok := issues[comment.Issue.ID]; !ok {
  199. issues[comment.Issue.ID] = comment.Issue
  200. }
  201. }
  202. }
  203. issueList := make([]*Issue, 0, len(issues))
  204. for _, issue := range issues {
  205. issueList = append(issueList, issue)
  206. }
  207. return issueList
  208. }
  209. // LoadIssues loads issues of comments
  210. func (comments CommentList) LoadIssues(ctx context.Context) error {
  211. if len(comments) == 0 {
  212. return nil
  213. }
  214. issueIDs := comments.getIssueIDs()
  215. issues := make(map[int64]*Issue, len(issueIDs))
  216. left := len(issueIDs)
  217. for left > 0 {
  218. limit := db.DefaultMaxInSize
  219. if left < limit {
  220. limit = left
  221. }
  222. rows, err := db.GetEngine(ctx).
  223. In("id", issueIDs[:limit]).
  224. Rows(new(Issue))
  225. if err != nil {
  226. return err
  227. }
  228. for rows.Next() {
  229. var issue Issue
  230. err = rows.Scan(&issue)
  231. if err != nil {
  232. rows.Close()
  233. return err
  234. }
  235. issues[issue.ID] = &issue
  236. }
  237. _ = rows.Close()
  238. left -= limit
  239. issueIDs = issueIDs[limit:]
  240. }
  241. for _, comment := range comments {
  242. if comment.Issue == nil {
  243. comment.Issue = issues[comment.IssueID]
  244. }
  245. }
  246. return nil
  247. }
  248. func (comments CommentList) getDependentIssueIDs() []int64 {
  249. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  250. if comment.DependentIssue != nil {
  251. return 0, false
  252. }
  253. return comment.DependentIssueID, comment.DependentIssueID > 0
  254. })
  255. }
  256. func (comments CommentList) loadDependentIssues(ctx context.Context) error {
  257. if len(comments) == 0 {
  258. return nil
  259. }
  260. e := db.GetEngine(ctx)
  261. issueIDs := comments.getDependentIssueIDs()
  262. issues := make(map[int64]*Issue, len(issueIDs))
  263. left := len(issueIDs)
  264. for left > 0 {
  265. limit := db.DefaultMaxInSize
  266. if left < limit {
  267. limit = left
  268. }
  269. rows, err := e.
  270. In("id", issueIDs[:limit]).
  271. Rows(new(Issue))
  272. if err != nil {
  273. return err
  274. }
  275. for rows.Next() {
  276. var issue Issue
  277. err = rows.Scan(&issue)
  278. if err != nil {
  279. _ = rows.Close()
  280. return err
  281. }
  282. issues[issue.ID] = &issue
  283. }
  284. _ = rows.Close()
  285. left -= limit
  286. issueIDs = issueIDs[limit:]
  287. }
  288. for _, comment := range comments {
  289. if comment.DependentIssue == nil {
  290. comment.DependentIssue = issues[comment.DependentIssueID]
  291. if comment.DependentIssue != nil {
  292. if err := comment.DependentIssue.LoadRepo(ctx); err != nil {
  293. return err
  294. }
  295. }
  296. }
  297. }
  298. return nil
  299. }
  300. // getAttachmentCommentIDs only return the comment ids which possibly has attachments
  301. func (comments CommentList) getAttachmentCommentIDs() []int64 {
  302. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  303. return comment.ID, comment.Type.HasAttachmentSupport()
  304. })
  305. }
  306. // LoadAttachmentsByIssue loads attachments by issue id
  307. func (comments CommentList) LoadAttachmentsByIssue(ctx context.Context) error {
  308. if len(comments) == 0 {
  309. return nil
  310. }
  311. attachments := make([]*repo_model.Attachment, 0, len(comments)/2)
  312. if err := db.GetEngine(ctx).Where("issue_id=? AND comment_id>0", comments[0].IssueID).Find(&attachments); err != nil {
  313. return err
  314. }
  315. commentAttachmentsMap := make(map[int64][]*repo_model.Attachment, len(comments))
  316. for _, attach := range attachments {
  317. commentAttachmentsMap[attach.CommentID] = append(commentAttachmentsMap[attach.CommentID], attach)
  318. }
  319. for _, comment := range comments {
  320. comment.Attachments = commentAttachmentsMap[comment.ID]
  321. }
  322. return nil
  323. }
  324. // LoadAttachments loads attachments
  325. func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
  326. if len(comments) == 0 {
  327. return nil
  328. }
  329. attachments := make(map[int64][]*repo_model.Attachment, len(comments))
  330. commentsIDs := comments.getAttachmentCommentIDs()
  331. left := len(commentsIDs)
  332. for left > 0 {
  333. limit := db.DefaultMaxInSize
  334. if left < limit {
  335. limit = left
  336. }
  337. rows, err := db.GetEngine(ctx).
  338. In("comment_id", commentsIDs[:limit]).
  339. Rows(new(repo_model.Attachment))
  340. if err != nil {
  341. return err
  342. }
  343. for rows.Next() {
  344. var attachment repo_model.Attachment
  345. err = rows.Scan(&attachment)
  346. if err != nil {
  347. _ = rows.Close()
  348. return err
  349. }
  350. attachments[attachment.CommentID] = append(attachments[attachment.CommentID], &attachment)
  351. }
  352. _ = rows.Close()
  353. left -= limit
  354. commentsIDs = commentsIDs[limit:]
  355. }
  356. for _, comment := range comments {
  357. comment.Attachments = attachments[comment.ID]
  358. }
  359. return nil
  360. }
  361. func (comments CommentList) getReviewIDs() []int64 {
  362. return container.FilterSlice(comments, func(comment *Comment) (int64, bool) {
  363. return comment.ReviewID, comment.ReviewID > 0
  364. })
  365. }
  366. func (comments CommentList) loadReviews(ctx context.Context) error {
  367. if len(comments) == 0 {
  368. return nil
  369. }
  370. reviewIDs := comments.getReviewIDs()
  371. reviews := make(map[int64]*Review, len(reviewIDs))
  372. if err := db.GetEngine(ctx).In("id", reviewIDs).Find(&reviews); err != nil {
  373. return err
  374. }
  375. for _, comment := range comments {
  376. comment.Review = reviews[comment.ReviewID]
  377. if comment.Review == nil {
  378. // review request which has been replaced by actual reviews doesn't exist in database anymore, so don't log errors for them.
  379. if comment.ReviewID > 0 && comment.Type != CommentTypeReviewRequest {
  380. log.Error("comment with review id [%d] but has no review record", comment.ReviewID)
  381. }
  382. continue
  383. }
  384. // If the comment dismisses a review, we need to load the reviewer to show whose review has been dismissed.
  385. // Otherwise, the reviewer is the poster of the comment, so we don't need to load it.
  386. if comment.Type == CommentTypeDismissReview {
  387. if err := comment.Review.LoadReviewer(ctx); err != nil {
  388. return err
  389. }
  390. }
  391. }
  392. return nil
  393. }
  394. // LoadAttributes loads attributes of the comments, except for attachments and
  395. // comments
  396. func (comments CommentList) LoadAttributes(ctx context.Context) (err error) {
  397. if err = comments.LoadPosters(ctx); err != nil {
  398. return err
  399. }
  400. if err = comments.loadLabels(ctx); err != nil {
  401. return err
  402. }
  403. if err = comments.loadMilestones(ctx); err != nil {
  404. return err
  405. }
  406. if err = comments.loadOldMilestones(ctx); err != nil {
  407. return err
  408. }
  409. if err = comments.loadAssignees(ctx); err != nil {
  410. return err
  411. }
  412. if err = comments.LoadAttachments(ctx); err != nil {
  413. return err
  414. }
  415. if err = comments.loadReviews(ctx); err != nil {
  416. return err
  417. }
  418. if err = comments.LoadIssues(ctx); err != nil {
  419. return err
  420. }
  421. return comments.loadDependentIssues(ctx)
  422. }