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

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