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.

543 lines
11 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. 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. if comment.DependentIssue != nil {
  335. if err := comment.DependentIssue.loadRepo(e); err != nil {
  336. return err
  337. }
  338. }
  339. }
  340. }
  341. return nil
  342. }
  343. func (comments CommentList) loadAttachments(e Engine) (err error) {
  344. if len(comments) == 0 {
  345. return nil
  346. }
  347. var attachments = make(map[int64][]*Attachment, len(comments))
  348. var commentsIDs = comments.getCommentIDs()
  349. var left = len(commentsIDs)
  350. for left > 0 {
  351. var limit = defaultMaxInSize
  352. if left < limit {
  353. limit = left
  354. }
  355. rows, err := e.Table("attachment").
  356. Join("INNER", "comment", "comment.id = attachment.comment_id").
  357. In("comment.id", commentsIDs[:limit]).
  358. Rows(new(Attachment))
  359. if err != nil {
  360. return err
  361. }
  362. for rows.Next() {
  363. var attachment Attachment
  364. err = rows.Scan(&attachment)
  365. if err != nil {
  366. _ = rows.Close()
  367. return err
  368. }
  369. attachments[attachment.CommentID] = append(attachments[attachment.CommentID], &attachment)
  370. }
  371. _ = rows.Close()
  372. left -= limit
  373. commentsIDs = commentsIDs[limit:]
  374. }
  375. for _, comment := range comments {
  376. comment.Attachments = attachments[comment.ID]
  377. }
  378. return nil
  379. }
  380. func (comments CommentList) getReviewIDs() []int64 {
  381. var ids = make(map[int64]struct{}, len(comments))
  382. for _, comment := range comments {
  383. if _, ok := ids[comment.ReviewID]; !ok {
  384. ids[comment.ReviewID] = struct{}{}
  385. }
  386. }
  387. return keysInt64(ids)
  388. }
  389. func (comments CommentList) loadReviews(e Engine) error {
  390. if len(comments) == 0 {
  391. return nil
  392. }
  393. var reviewIDs = comments.getReviewIDs()
  394. var reviews = make(map[int64]*Review, len(reviewIDs))
  395. var left = len(reviewIDs)
  396. for left > 0 {
  397. var limit = defaultMaxInSize
  398. if left < limit {
  399. limit = left
  400. }
  401. rows, err := e.
  402. In("id", reviewIDs[:limit]).
  403. Rows(new(Review))
  404. if err != nil {
  405. return err
  406. }
  407. for rows.Next() {
  408. var review Review
  409. err = rows.Scan(&review)
  410. if err != nil {
  411. _ = rows.Close()
  412. return err
  413. }
  414. reviews[review.ID] = &review
  415. }
  416. _ = rows.Close()
  417. left -= limit
  418. reviewIDs = reviewIDs[limit:]
  419. }
  420. for _, comment := range comments {
  421. comment.Review = reviews[comment.ReviewID]
  422. }
  423. return nil
  424. }
  425. // loadAttributes loads all attributes
  426. func (comments CommentList) loadAttributes(e Engine) (err error) {
  427. if err = comments.loadPosters(e); err != nil {
  428. return
  429. }
  430. if err = comments.loadLabels(e); err != nil {
  431. return
  432. }
  433. if err = comments.loadMilestones(e); err != nil {
  434. return
  435. }
  436. if err = comments.loadOldMilestones(e); err != nil {
  437. return
  438. }
  439. if err = comments.loadAssignees(e); err != nil {
  440. return
  441. }
  442. if err = comments.loadAttachments(e); err != nil {
  443. return
  444. }
  445. if err = comments.loadReviews(e); err != nil {
  446. return
  447. }
  448. if err = comments.loadIssues(e); err != nil {
  449. return
  450. }
  451. if err = comments.loadDependentIssues(e); err != nil {
  452. return
  453. }
  454. return nil
  455. }
  456. // LoadAttributes loads attributes of the comments, except for attachments and
  457. // comments
  458. func (comments CommentList) LoadAttributes() error {
  459. return comments.loadAttributes(x)
  460. }
  461. // LoadAttachments loads attachments
  462. func (comments CommentList) LoadAttachments() error {
  463. return comments.loadAttachments(x)
  464. }
  465. // LoadPosters loads posters
  466. func (comments CommentList) LoadPosters() error {
  467. return comments.loadPosters(x)
  468. }
  469. // LoadIssues loads issues of comments
  470. func (comments CommentList) LoadIssues() error {
  471. return comments.loadIssues(x)
  472. }