gitea源码

comment_code.go 4.0KB

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