repo_issue_comment.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package v1
  2. import (
  3. "net/http"
  4. "time"
  5. "gogs.io/gogs/internal/context"
  6. "gogs.io/gogs/internal/database"
  7. "gogs.io/gogs/internal/route/api/v1/types"
  8. )
  9. func listIssueComments(c *context.APIContext) {
  10. var since time.Time
  11. if len(c.Query("since")) > 0 {
  12. var err error
  13. since, err = time.Parse(time.RFC3339, c.Query("since"))
  14. if err != nil {
  15. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  16. return
  17. }
  18. }
  19. // comments,err:=db.GetCommentsByIssueIDSince(, since)
  20. issue, err := database.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  21. if err != nil {
  22. c.Error(err, "get raw issue by index")
  23. return
  24. }
  25. comments, err := database.GetCommentsByIssueIDSince(issue.ID, since.Unix())
  26. if err != nil {
  27. c.Error(err, "get comments by issue ID")
  28. return
  29. }
  30. apiComments := make([]*types.IssueComment, len(comments))
  31. for i := range comments {
  32. apiComments[i] = toIssueComment(comments[i])
  33. }
  34. c.JSONSuccess(&apiComments)
  35. }
  36. func listRepoIssueComments(c *context.APIContext) {
  37. var since time.Time
  38. if len(c.Query("since")) > 0 {
  39. var err error
  40. since, err = time.Parse(time.RFC3339, c.Query("since"))
  41. if err != nil {
  42. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  43. return
  44. }
  45. }
  46. comments, err := database.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
  47. if err != nil {
  48. c.Error(err, "get comments by repository ID")
  49. return
  50. }
  51. apiComments := make([]*types.IssueComment, len(comments))
  52. for i := range comments {
  53. apiComments[i] = toIssueComment(comments[i])
  54. }
  55. c.JSONSuccess(&apiComments)
  56. }
  57. type createIssueCommentRequest struct {
  58. Body string `json:"body" binding:"Required"`
  59. }
  60. func createIssueComment(c *context.APIContext, form createIssueCommentRequest) {
  61. issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  62. if err != nil {
  63. c.Error(err, "get issue by index")
  64. return
  65. }
  66. comment, err := database.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
  67. if err != nil {
  68. c.Error(err, "create issue comment")
  69. return
  70. }
  71. c.JSON(http.StatusCreated, toIssueComment(comment))
  72. }
  73. type editIssueCommentRequest struct {
  74. Body string `json:"body" binding:"Required"`
  75. }
  76. func editIssueComment(c *context.APIContext, form editIssueCommentRequest) {
  77. comment, err := database.GetCommentByID(c.ParamsInt64(":id"))
  78. if err != nil {
  79. c.NotFoundOrError(err, "get comment by ID")
  80. return
  81. }
  82. issue, err := database.GetIssueByID(comment.IssueID)
  83. if err != nil {
  84. c.NotFoundOrError(err, "get issue by ID")
  85. return
  86. }
  87. if issue.RepoID != c.Repo.Repository.ID {
  88. c.NotFound()
  89. return
  90. }
  91. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  92. c.Status(http.StatusForbidden)
  93. return
  94. } else if comment.Type != database.CommentTypeComment {
  95. c.NoContent()
  96. return
  97. }
  98. oldContent := comment.Content
  99. comment.Content = form.Body
  100. if err := database.UpdateComment(c.User, comment, oldContent); err != nil {
  101. c.Error(err, "update comment")
  102. return
  103. }
  104. c.JSONSuccess(toIssueComment(comment))
  105. }
  106. func deleteIssueComment(c *context.APIContext) {
  107. comment, err := database.GetCommentByID(c.ParamsInt64(":id"))
  108. if err != nil {
  109. c.NotFoundOrError(err, "get comment by ID")
  110. return
  111. }
  112. issue, err := database.GetIssueByID(comment.IssueID)
  113. if err != nil {
  114. c.NotFoundOrError(err, "get issue by ID")
  115. return
  116. }
  117. if issue.RepoID != c.Repo.Repository.ID {
  118. c.NotFound()
  119. return
  120. }
  121. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  122. c.Status(http.StatusForbidden)
  123. return
  124. } else if comment.Type != database.CommentTypeComment {
  125. c.NoContent()
  126. return
  127. }
  128. if err = database.DeleteCommentByID(c.User, comment.ID); err != nil {
  129. c.Error(err, "delete comment by ID")
  130. return
  131. }
  132. c.NoContent()
  133. }