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.

issue_comment_list.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. import "code.gitea.io/gitea/models/db"
  6. // CommentList defines a list of comments
  7. type CommentList []*Comment
  8. func (comments CommentList) getPosterIDs() []int64 {
  9. posterIDs := make(map[int64]struct{}, len(comments))
  10. for _, comment := range comments {
  11. if _, ok := posterIDs[comment.PosterID]; !ok {
  12. posterIDs[comment.PosterID] = struct{}{}
  13. }
  14. }
  15. return keysInt64(posterIDs)
  16. }
  17. func (comments CommentList) loadPosters(e db.Engine) error {
  18. if len(comments) == 0 {
  19. return nil
  20. }
  21. posterIDs := comments.getPosterIDs()
  22. posterMaps := make(map[int64]*User, len(posterIDs))
  23. left := len(posterIDs)
  24. for left > 0 {
  25. limit := defaultMaxInSize
  26. if left < limit {
  27. limit = left
  28. }
  29. err := e.
  30. In("id", posterIDs[:limit]).
  31. Find(&posterMaps)
  32. if err != nil {
  33. return err
  34. }
  35. left -= limit
  36. posterIDs = posterIDs[limit:]
  37. }
  38. for _, comment := range comments {
  39. if comment.PosterID <= 0 {
  40. continue
  41. }
  42. var ok bool
  43. if comment.Poster, ok = posterMaps[comment.PosterID]; !ok {
  44. comment.Poster = NewGhostUser()
  45. }
  46. }
  47. return nil
  48. }
  49. func (comments CommentList) getCommentIDs() []int64 {
  50. ids := make([]int64, 0, len(comments))
  51. for _, comment := range comments {
  52. ids = append(ids, comment.ID)
  53. }
  54. return ids
  55. }
  56. func (comments CommentList) getLabelIDs() []int64 {
  57. ids := make(map[int64]struct{}, len(comments))
  58. for _, comment := range comments {
  59. if _, ok := ids[comment.LabelID]; !ok {
  60. ids[comment.LabelID] = struct{}{}
  61. }
  62. }
  63. return keysInt64(ids)
  64. }
  65. func (comments CommentList) loadLabels(e db.Engine) error {
  66. if len(comments) == 0 {
  67. return nil
  68. }
  69. labelIDs := comments.getLabelIDs()
  70. commentLabels := make(map[int64]*Label, len(labelIDs))
  71. left := len(labelIDs)
  72. for left > 0 {
  73. limit := defaultMaxInSize
  74. if left < limit {
  75. limit = left
  76. }
  77. rows, err := e.
  78. In("id", labelIDs[:limit]).
  79. Rows(new(Label))
  80. if err != nil {
  81. return err
  82. }
  83. for rows.Next() {
  84. var label Label
  85. err = rows.Scan(&label)
  86. if err != nil {
  87. _ = rows.Close()
  88. return err
  89. }
  90. commentLabels[label.ID] = &label
  91. }
  92. _ = rows.Close()
  93. left -= limit
  94. labelIDs = labelIDs[limit:]
  95. }
  96. for _, comment := range comments {
  97. comment.Label = commentLabels[comment.ID]
  98. }
  99. return nil
  100. }
  101. func (comments CommentList) getMilestoneIDs() []int64 {
  102. ids := make(map[int64]struct{}, len(comments))
  103. for _, comment := range comments {
  104. if _, ok := ids[comment.MilestoneID]; !ok {
  105. ids[comment.MilestoneID] = struct{}{}
  106. }
  107. }
  108. return keysInt64(ids)
  109. }
  110. func (comments CommentList) loadMilestones(e db.Engine) error {
  111. if len(comments) == 0 {
  112. return nil
  113. }
  114. milestoneIDs := comments.getMilestoneIDs()
  115. if len(milestoneIDs) == 0 {
  116. return nil
  117. }
  118. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  119. left := len(milestoneIDs)
  120. for left > 0 {
  121. limit := defaultMaxInSize
  122. if left < limit {
  123. limit = left
  124. }
  125. err := e.
  126. In("id", milestoneIDs[:limit]).
  127. Find(&milestoneMaps)
  128. if err != nil {
  129. return err
  130. }
  131. left -= limit
  132. milestoneIDs = milestoneIDs[limit:]
  133. }
  134. for _, issue := range comments {
  135. issue.Milestone = milestoneMaps[issue.MilestoneID]
  136. }
  137. return nil
  138. }
  139. func (comments CommentList) getOldMilestoneIDs() []int64 {
  140. ids := make(map[int64]struct{}, len(comments))
  141. for _, comment := range comments {
  142. if _, ok := ids[comment.OldMilestoneID]; !ok {
  143. ids[comment.OldMilestoneID] = struct{}{}
  144. }
  145. }
  146. return keysInt64(ids)
  147. }
  148. func (comments CommentList) loadOldMilestones(e db.Engine) error {
  149. if len(comments) == 0 {
  150. return nil
  151. }
  152. milestoneIDs := comments.getOldMilestoneIDs()
  153. if len(milestoneIDs) == 0 {
  154. return nil
  155. }
  156. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  157. left := len(milestoneIDs)
  158. for left > 0 {
  159. limit := defaultMaxInSize
  160. if left < limit {
  161. limit = left
  162. }
  163. err := e.
  164. In("id", milestoneIDs[:limit]).
  165. Find(&milestoneMaps)
  166. if err != nil {
  167. return err
  168. }
  169. left -= limit
  170. milestoneIDs = milestoneIDs[limit:]
  171. }
  172. for _, issue := range comments {
  173. issue.OldMilestone = milestoneMaps[issue.MilestoneID]
  174. }
  175. return nil
  176. }
  177. func (comments CommentList) getAssigneeIDs() []int64 {
  178. ids := make(map[int64]struct{}, len(comments))
  179. for _, comment := range comments {
  180. if _, ok := ids[comment.AssigneeID]; !ok {
  181. ids[comment.AssigneeID] = struct{}{}
  182. }
  183. }
  184. return keysInt64(ids)
  185. }
  186. func (comments CommentList) loadAssignees(e db.Engine) error {
  187. if len(comments) == 0 {
  188. return nil
  189. }
  190. assigneeIDs := comments.getAssigneeIDs()
  191. assignees := make(map[int64]*User, len(assigneeIDs))
  192. left := len(assigneeIDs)
  193. for left > 0 {
  194. limit := defaultMaxInSize
  195. if left < limit {
  196. limit = left
  197. }
  198. rows, err := e.
  199. In("id", assigneeIDs[:limit]).
  200. Rows(new(User))
  201. if err != nil {
  202. return err
  203. }
  204. for rows.Next() {
  205. var user User
  206. err = rows.Scan(&user)
  207. if err != nil {
  208. rows.Close()
  209. return err
  210. }
  211. assignees[user.ID] = &user
  212. }
  213. _ = rows.Close()
  214. left -= limit
  215. assigneeIDs = assigneeIDs[limit:]
  216. }
  217. for _, comment := range comments {
  218. comment.Assignee = assignees[comment.AssigneeID]
  219. }
  220. return nil
  221. }
  222. // getIssueIDs returns all the issue ids on this comment list which issue hasn't been loaded
  223. func (comments CommentList) getIssueIDs() []int64 {
  224. ids := make(map[int64]struct{}, len(comments))
  225. for _, comment := range comments {
  226. if comment.Issue != nil {
  227. continue
  228. }
  229. if _, ok := ids[comment.IssueID]; !ok {
  230. ids[comment.IssueID] = struct{}{}
  231. }
  232. }
  233. return keysInt64(ids)
  234. }
  235. // Issues returns all the issues of comments
  236. func (comments CommentList) Issues() IssueList {
  237. issues := make(map[int64]*Issue, len(comments))
  238. for _, comment := range comments {
  239. if comment.Issue != nil {
  240. if _, ok := issues[comment.Issue.ID]; !ok {
  241. issues[comment.Issue.ID] = comment.Issue
  242. }
  243. }
  244. }
  245. issueList := make([]*Issue, 0, len(issues))
  246. for _, issue := range issues {
  247. issueList = append(issueList, issue)
  248. }
  249. return issueList
  250. }
  251. func (comments CommentList) loadIssues(e db.Engine) error {
  252. if len(comments) == 0 {
  253. return nil
  254. }
  255. issueIDs := comments.getIssueIDs()
  256. issues := make(map[int64]*Issue, len(issueIDs))
  257. left := len(issueIDs)
  258. for left > 0 {
  259. limit := defaultMaxInSize
  260. if left < limit {
  261. limit = left
  262. }
  263. rows, err := e.
  264. In("id", issueIDs[:limit]).
  265. Rows(new(Issue))
  266. if err != nil {
  267. return err
  268. }
  269. for rows.Next() {
  270. var issue Issue
  271. err = rows.Scan(&issue)
  272. if err != nil {
  273. rows.Close()
  274. return err
  275. }
  276. issues[issue.ID] = &issue
  277. }
  278. _ = rows.Close()
  279. left -= limit
  280. issueIDs = issueIDs[limit:]
  281. }
  282. for _, comment := range comments {
  283. if comment.Issue == nil {
  284. comment.Issue = issues[comment.IssueID]
  285. }
  286. }
  287. return nil
  288. }
  289. func (comments CommentList) getDependentIssueIDs() []int64 {
  290. ids := make(map[int64]struct{}, len(comments))
  291. for _, comment := range comments {
  292. if comment.DependentIssue != nil {
  293. continue
  294. }
  295. if _, ok := ids[comment.DependentIssueID]; !ok {
  296. ids[comment.DependentIssueID] = struct{}{}
  297. }
  298. }
  299. return keysInt64(ids)
  300. }
  301. func (comments CommentList) loadDependentIssues(e db.Engine) error {
  302. if len(comments) == 0 {
  303. return nil
  304. }
  305. issueIDs := comments.getDependentIssueIDs()
  306. issues := make(map[int64]*Issue, len(issueIDs))
  307. left := len(issueIDs)
  308. for left > 0 {
  309. limit := defaultMaxInSize
  310. if left < limit {
  311. limit = left
  312. }
  313. rows, err := e.
  314. In("id", issueIDs[:limit]).
  315. Rows(new(Issue))
  316. if err != nil {
  317. return err
  318. }
  319. for rows.Next() {
  320. var issue Issue
  321. err = rows.Scan(&issue)
  322. if err != nil {
  323. _ = rows.Close()
  324. return err
  325. }
  326. issues[issue.ID] = &issue
  327. }
  328. _ = rows.Close()
  329. left -= limit
  330. issueIDs = issueIDs[limit:]
  331. }
  332. for _, comment := range comments {
  333. if comment.DependentIssue == nil {
  334. comment.DependentIssue = issues[comment.DependentIssueID]
  335. if comment.DependentIssue != nil {
  336. if err := comment.DependentIssue.loadRepo(e); err != nil {
  337. return err
  338. }
  339. }
  340. }
  341. }
  342. return nil
  343. }
  344. func (comments CommentList) loadAttachments(e db.Engine) (err error) {
  345. if len(comments) == 0 {
  346. return nil
  347. }
  348. attachments := make(map[int64][]*Attachment, len(comments))
  349. commentsIDs := comments.getCommentIDs()
  350. left := len(commentsIDs)
  351. for left > 0 {
  352. limit := defaultMaxInSize
  353. if left < limit {
  354. limit = left
  355. }
  356. rows, err := e.Table("attachment").
  357. Join("INNER", "comment", "comment.id = attachment.comment_id").
  358. In("comment.id", commentsIDs[:limit]).
  359. Rows(new(Attachment))
  360. if err != nil {
  361. return err
  362. }
  363. for rows.Next() {
  364. var attachment Attachment
  365. err = rows.Scan(&attachment)
  366. if err != nil {
  367. _ = rows.Close()
  368. return err
  369. }
  370. attachments[attachment.CommentID] = append(attachments[attachment.CommentID], &attachment)
  371. }
  372. _ = rows.Close()
  373. left -= limit
  374. commentsIDs = commentsIDs[limit:]
  375. }
  376. for _, comment := range comments {
  377. comment.Attachments = attachments[comment.ID]
  378. }
  379. return nil
  380. }
  381. func (comments CommentList) getReviewIDs() []int64 {
  382. ids := make(map[int64]struct{}, len(comments))
  383. for _, comment := range comments {
  384. if _, ok := ids[comment.ReviewID]; !ok {
  385. ids[comment.ReviewID] = struct{}{}
  386. }
  387. }
  388. return keysInt64(ids)
  389. }
  390. func (comments CommentList) loadReviews(e db.Engine) error {
  391. if len(comments) == 0 {
  392. return nil
  393. }
  394. reviewIDs := comments.getReviewIDs()
  395. reviews := make(map[int64]*Review, len(reviewIDs))
  396. left := len(reviewIDs)
  397. for left > 0 {
  398. limit := defaultMaxInSize
  399. if left < limit {
  400. limit = left
  401. }
  402. rows, err := e.
  403. In("id", reviewIDs[:limit]).
  404. Rows(new(Review))
  405. if err != nil {
  406. return err
  407. }
  408. for rows.Next() {
  409. var review Review
  410. err = rows.Scan(&review)
  411. if err != nil {
  412. _ = rows.Close()
  413. return err
  414. }
  415. reviews[review.ID] = &review
  416. }
  417. _ = rows.Close()
  418. left -= limit
  419. reviewIDs = reviewIDs[limit:]
  420. }
  421. for _, comment := range comments {
  422. comment.Review = reviews[comment.ReviewID]
  423. }
  424. return nil
  425. }
  426. // loadAttributes loads all attributes
  427. func (comments CommentList) loadAttributes(e db.Engine) (err error) {
  428. if err = comments.loadPosters(e); err != nil {
  429. return
  430. }
  431. if err = comments.loadLabels(e); err != nil {
  432. return
  433. }
  434. if err = comments.loadMilestones(e); err != nil {
  435. return
  436. }
  437. if err = comments.loadOldMilestones(e); err != nil {
  438. return
  439. }
  440. if err = comments.loadAssignees(e); err != nil {
  441. return
  442. }
  443. if err = comments.loadAttachments(e); err != nil {
  444. return
  445. }
  446. if err = comments.loadReviews(e); err != nil {
  447. return
  448. }
  449. if err = comments.loadIssues(e); err != nil {
  450. return
  451. }
  452. if err = comments.loadDependentIssues(e); err != nil {
  453. return
  454. }
  455. return nil
  456. }
  457. // LoadAttributes loads attributes of the comments, except for attachments and
  458. // comments
  459. func (comments CommentList) LoadAttributes() error {
  460. return comments.loadAttributes(db.GetEngine(db.DefaultContext))
  461. }
  462. // LoadAttachments loads attachments
  463. func (comments CommentList) LoadAttachments() error {
  464. return comments.loadAttachments(db.GetEngine(db.DefaultContext))
  465. }
  466. // LoadPosters loads posters
  467. func (comments CommentList) LoadPosters() error {
  468. return comments.loadPosters(db.GetEngine(db.DefaultContext))
  469. }
  470. // LoadIssues loads issues of comments
  471. func (comments CommentList) LoadIssues() error {
  472. return comments.loadIssues(db.GetEngine(db.DefaultContext))
  473. }