1
0

issue_comment.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package repo
  2. import (
  3. "net/http"
  4. "time"
  5. api "github.com/gogs/go-gogs-client"
  6. "gogs.io/gogs/internal/context"
  7. "gogs.io/gogs/internal/database"
  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([]*api.Comment, len(comments))
  31. for i := range comments {
  32. apiComments[i] = comments[i].APIFormat()
  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([]*api.Comment, len(comments))
  52. for i := range comments {
  53. apiComments[i] = comments[i].APIFormat()
  54. }
  55. c.JSONSuccess(&apiComments)
  56. }
  57. func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
  58. issue, err := database.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  59. if err != nil {
  60. c.Error(err, "get issue by index")
  61. return
  62. }
  63. comment, err := database.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
  64. if err != nil {
  65. c.Error(err, "create issue comment")
  66. return
  67. }
  68. c.JSON(http.StatusCreated, comment.APIFormat())
  69. }
  70. func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
  71. comment, err := database.GetCommentByID(c.ParamsInt64(":id"))
  72. if err != nil {
  73. c.NotFoundOrError(err, "get comment by ID")
  74. return
  75. }
  76. issue, err := database.GetIssueByID(comment.IssueID)
  77. if err != nil {
  78. c.NotFoundOrError(err, "get issue by ID")
  79. return
  80. }
  81. if issue.RepoID != c.Repo.Repository.ID {
  82. c.NotFound()
  83. return
  84. }
  85. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  86. c.Status(http.StatusForbidden)
  87. return
  88. } else if comment.Type != database.CommentTypeComment {
  89. c.NoContent()
  90. return
  91. }
  92. oldContent := comment.Content
  93. comment.Content = form.Body
  94. if err := database.UpdateComment(c.User, comment, oldContent); err != nil {
  95. c.Error(err, "update comment")
  96. return
  97. }
  98. c.JSONSuccess(comment.APIFormat())
  99. }
  100. func DeleteIssueComment(c *context.APIContext) {
  101. comment, err := database.GetCommentByID(c.ParamsInt64(":id"))
  102. if err != nil {
  103. c.NotFoundOrError(err, "get comment by ID")
  104. return
  105. }
  106. issue, err := database.GetIssueByID(comment.IssueID)
  107. if err != nil {
  108. c.NotFoundOrError(err, "get issue by ID")
  109. return
  110. }
  111. if issue.RepoID != c.Repo.Repository.ID {
  112. c.NotFound()
  113. return
  114. }
  115. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  116. c.Status(http.StatusForbidden)
  117. return
  118. } else if comment.Type != database.CommentTypeComment {
  119. c.NoContent()
  120. return
  121. }
  122. if err = database.DeleteCommentByID(c.User, comment.ID); err != nil {
  123. c.Error(err, "delete comment by ID")
  124. return
  125. }
  126. c.NoContent()
  127. }