issue_comment.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  77. c.Status(http.StatusForbidden)
  78. return
  79. } else if comment.Type != database.CommentTypeComment {
  80. c.NoContent()
  81. return
  82. }
  83. oldContent := comment.Content
  84. comment.Content = form.Body
  85. if err := database.UpdateComment(c.User, comment, oldContent); err != nil {
  86. c.Error(err, "update comment")
  87. return
  88. }
  89. c.JSONSuccess(comment.APIFormat())
  90. }
  91. func DeleteIssueComment(c *context.APIContext) {
  92. comment, err := database.GetCommentByID(c.ParamsInt64(":id"))
  93. if err != nil {
  94. c.NotFoundOrError(err, "get comment by ID")
  95. return
  96. }
  97. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  98. c.Status(http.StatusForbidden)
  99. return
  100. } else if comment.Type != database.CommentTypeComment {
  101. c.NoContent()
  102. return
  103. }
  104. if err = database.DeleteCommentByID(c.User, comment.ID); err != nil {
  105. c.Error(err, "delete comment by ID")
  106. return
  107. }
  108. c.NoContent()
  109. }