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

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