issue_comment.go 3.3 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. "github.com/gogs/gogs/models"
  10. "github.com/gogs/gogs/pkg/context"
  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.Error(http.StatusUnprocessableEntity, "", err)
  19. return
  20. }
  21. }
  22. // comments,err:=models.GetCommentsByIssueIDSince(, since)
  23. issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  24. if err != nil {
  25. c.ServerError("GetRawIssueByIndex", err)
  26. return
  27. }
  28. comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
  29. if err != nil {
  30. c.ServerError("GetCommentsByIssueIDSince", err)
  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.Error(http.StatusUnprocessableEntity, "", err)
  46. return
  47. }
  48. }
  49. comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
  50. if err != nil {
  51. c.ServerError("GetCommentsByRepoIDSince", err)
  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 := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  62. if err != nil {
  63. c.ServerError("GetIssueByIndex", err)
  64. return
  65. }
  66. comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
  67. if err != nil {
  68. c.ServerError("CreateIssueComment", err)
  69. return
  70. }
  71. c.JSON(http.StatusCreated, comment.APIFormat())
  72. }
  73. func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
  74. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  75. if err != nil {
  76. c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  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 != models.COMMENT_TYPE_COMMENT {
  83. c.NoContent()
  84. return
  85. }
  86. oldContent := comment.Content
  87. comment.Content = form.Body
  88. if err := models.UpdateComment(c.User, comment, oldContent); err != nil {
  89. c.ServerError("UpdateComment", err)
  90. return
  91. }
  92. c.JSONSuccess(comment.APIFormat())
  93. }
  94. func DeleteIssueComment(c *context.APIContext) {
  95. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  96. if err != nil {
  97. c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
  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 != models.COMMENT_TYPE_COMMENT {
  104. c.NoContent()
  105. return
  106. }
  107. if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
  108. c.ServerError("DeleteCommentByID", err)
  109. return
  110. }
  111. c.NoContent()
  112. }