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_code.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2022 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. user_model "code.gitea.io/gitea/models/user"
  8. "code.gitea.io/gitea/modules/markup"
  9. "code.gitea.io/gitea/modules/markup/markdown"
  10. "xorm.io/builder"
  11. )
  12. // CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
  13. type CodeComments map[string]map[int64][]*Comment
  14. // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
  15. func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) {
  16. return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments)
  17. }
  18. func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) (CodeComments, error) {
  19. pathToLineToComment := make(CodeComments)
  20. if review == nil {
  21. review = &Review{ID: 0}
  22. }
  23. opts := FindCommentsOptions{
  24. Type: CommentTypeCode,
  25. IssueID: issue.ID,
  26. ReviewID: review.ID,
  27. }
  28. comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments)
  29. if err != nil {
  30. return nil, err
  31. }
  32. for _, comment := range comments {
  33. if pathToLineToComment[comment.TreePath] == nil {
  34. pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
  35. }
  36. pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
  37. }
  38. return pathToLineToComment, nil
  39. }
  40. func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) ([]*Comment, error) {
  41. var comments CommentList
  42. if review == nil {
  43. review = &Review{ID: 0}
  44. }
  45. conds := opts.ToConds()
  46. if !showOutdatedComments && review.ID == 0 {
  47. conds = conds.And(builder.Eq{"invalidated": false})
  48. }
  49. e := db.GetEngine(ctx)
  50. if err := e.Where(conds).
  51. Asc("comment.created_unix").
  52. Asc("comment.id").
  53. Find(&comments); err != nil {
  54. return nil, err
  55. }
  56. if err := issue.LoadRepo(ctx); err != nil {
  57. return nil, err
  58. }
  59. if err := comments.LoadPosters(ctx); err != nil {
  60. return nil, err
  61. }
  62. // Find all reviews by ReviewID
  63. reviews := make(map[int64]*Review)
  64. ids := make([]int64, 0, len(comments))
  65. for _, comment := range comments {
  66. if comment.ReviewID != 0 {
  67. ids = append(ids, comment.ReviewID)
  68. }
  69. }
  70. if err := e.In("id", ids).Find(&reviews); err != nil {
  71. return nil, err
  72. }
  73. n := 0
  74. for _, comment := range comments {
  75. if re, ok := reviews[comment.ReviewID]; ok && re != nil {
  76. // If the review is pending only the author can see the comments (except if the review is set)
  77. if review.ID == 0 && re.Type == ReviewTypePending &&
  78. (currentUser == nil || currentUser.ID != re.ReviewerID) {
  79. continue
  80. }
  81. comment.Review = re
  82. }
  83. comments[n] = comment
  84. n++
  85. if err := comment.LoadResolveDoer(ctx); err != nil {
  86. return nil, err
  87. }
  88. if err := comment.LoadReactions(ctx, issue.Repo); err != nil {
  89. return nil, err
  90. }
  91. var err error
  92. if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{
  93. Ctx: ctx,
  94. Links: markup.Links{
  95. Base: issue.Repo.Link(),
  96. },
  97. Metas: issue.Repo.ComposeMetas(),
  98. }, comment.Content); err != nil {
  99. return nil, err
  100. }
  101. }
  102. return comments[:n], nil
  103. }
  104. // FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
  105. func FetchCodeCommentsByLine(ctx context.Context, issue *Issue, currentUser *user_model.User, treePath string, line int64, showOutdatedComments bool) ([]*Comment, error) {
  106. opts := FindCommentsOptions{
  107. Type: CommentTypeCode,
  108. IssueID: issue.ID,
  109. TreePath: treePath,
  110. Line: line,
  111. }
  112. return findCodeComments(ctx, opts, issue, currentUser, nil, showOutdatedComments)
  113. }