issue_comment.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "net/http"
  7. "time"
  8. api "github.com/gogs/go-gogs-client"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/db"
  11. )
  12. func ListIssueComments(c *context.APIContext) {
  13. var since time.Time
  14. if len(c.Query("since")) > 0 {
  15. var err error
  16. since, err = time.Parse(time.RFC3339, c.Query("since"))
  17. if err != nil {
  18. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  19. return
  20. }
  21. }
  22. // comments,err:=db.GetCommentsByIssueIDSince(, since)
  23. issue, err := db.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  24. if err != nil {
  25. c.Error(err, "get raw issue by index")
  26. return
  27. }
  28. comments, err := db.GetCommentsByIssueIDSince(issue.ID, since.Unix())
  29. if err != nil {
  30. c.Error(err, "get comments by issue ID")
  31. return
  32. }
  33. apiComments := make([]*api.Comment, len(comments))
  34. for i := range comments {
  35. apiComments[i] = comments[i].APIFormat()
  36. }
  37. c.JSONSuccess(&apiComments)
  38. }
  39. func ListRepoIssueComments(c *context.APIContext) {
  40. var since time.Time
  41. if len(c.Query("since")) > 0 {
  42. var err error
  43. since, err = time.Parse(time.RFC3339, c.Query("since"))
  44. if err != nil {
  45. c.ErrorStatus(http.StatusUnprocessableEntity, err)
  46. return
  47. }
  48. }
  49. comments, err := db.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
  50. if err != nil {
  51. c.Error(err, "get comments by repository ID")
  52. return
  53. }
  54. apiComments := make([]*api.Comment, len(comments))
  55. for i := range comments {
  56. apiComments[i] = comments[i].APIFormat()
  57. }
  58. c.JSONSuccess(&apiComments)
  59. }
  60. func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
  61. issue, err := db.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 := db.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, comment.APIFormat())
  72. }
  73. func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
  74. comment, err := db.GetCommentByID(c.ParamsInt64(":id"))
  75. if err != nil {
  76. c.NotFoundOrError(err, "get comment by ID")
  77. return
  78. }
  79. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  80. c.Status(http.StatusForbidden)
  81. return
  82. } else if comment.Type != db.COMMENT_TYPE_COMMENT {
  83. c.NoContent()
  84. return
  85. }
  86. oldContent := comment.Content
  87. comment.Content = form.Body
  88. if err := db.UpdateComment(c.User, comment, oldContent); err != nil {
  89. c.Error(err, "update comment")
  90. return
  91. }
  92. c.JSONSuccess(comment.APIFormat())
  93. }
  94. func DeleteIssueComment(c *context.APIContext) {
  95. comment, err := db.GetCommentByID(c.ParamsInt64(":id"))
  96. if err != nil {
  97. c.NotFoundOrError(err, "get comment by ID")
  98. return
  99. }
  100. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  101. c.Status(http.StatusForbidden)
  102. return
  103. } else if comment.Type != db.COMMENT_TYPE_COMMENT {
  104. c.NoContent()
  105. return
  106. }
  107. if err = db.DeleteCommentByID(c.User, comment.ID); err != nil {
  108. c.Error(err, "delete comment by ID")
  109. return
  110. }
  111. c.NoContent()
  112. }