issue_comment.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package repo
  7. import (
  8. "gitote/gitote/models"
  9. "gitote/gitote/pkg/context"
  10. "time"
  11. api "gitlab.com/gitote/go-gitote-client"
  12. )
  13. // ListIssueComments list all the comments of an issue
  14. func ListIssueComments(c *context.APIContext) {
  15. var since time.Time
  16. if len(c.Query("since")) > 0 {
  17. var err error
  18. since, err = time.Parse(time.RFC3339, c.Query("since"))
  19. if err != nil {
  20. c.Error(422, "", err)
  21. return
  22. }
  23. }
  24. // comments,err:=models.GetCommentsByIssueIDSince(, since)
  25. issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  26. if err != nil {
  27. c.Error(500, "GetRawIssueByIndex", err)
  28. return
  29. }
  30. comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
  31. if err != nil {
  32. c.Error(500, "GetCommentsByIssueIDSince", err)
  33. return
  34. }
  35. apiComments := make([]*api.Comment, len(comments))
  36. for i := range comments {
  37. apiComments[i] = comments[i].APIFormat()
  38. }
  39. c.JSON(200, &apiComments)
  40. }
  41. // ListRepoIssueComments returns all issue-comments for a repo
  42. func ListRepoIssueComments(c *context.APIContext) {
  43. var since time.Time
  44. if len(c.Query("since")) > 0 {
  45. var err error
  46. since, err = time.Parse(time.RFC3339, c.Query("since"))
  47. if err != nil {
  48. c.Error(422, "", err)
  49. return
  50. }
  51. }
  52. comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
  53. if err != nil {
  54. c.Error(500, "GetCommentsByRepoIDSince", err)
  55. return
  56. }
  57. apiComments := make([]*api.Comment, len(comments))
  58. for i := range comments {
  59. apiComments[i] = comments[i].APIFormat()
  60. }
  61. c.JSON(200, &apiComments)
  62. }
  63. // CreateIssueComment create a comment for an issue
  64. func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
  65. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  66. if err != nil {
  67. c.Error(500, "GetIssueByIndex", err)
  68. return
  69. }
  70. comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
  71. if err != nil {
  72. c.Error(500, "CreateIssueComment", err)
  73. return
  74. }
  75. c.JSON(201, comment.APIFormat())
  76. }
  77. // EditIssueComment modify a comment of an issue
  78. func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
  79. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  80. if err != nil {
  81. if models.IsErrCommentNotExist(err) {
  82. c.Error(404, "GetCommentByID", err)
  83. } else {
  84. c.Error(500, "GetCommentByID", err)
  85. }
  86. return
  87. }
  88. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  89. c.Status(403)
  90. return
  91. } else if comment.Type != models.CommentTypeComment {
  92. c.Status(204)
  93. return
  94. }
  95. oldContent := comment.Content
  96. comment.Content = form.Body
  97. if err := models.UpdateComment(c.User, comment, oldContent); err != nil {
  98. c.Error(500, "UpdateComment", err)
  99. return
  100. }
  101. c.JSON(200, comment.APIFormat())
  102. }
  103. // DeleteIssueComment delete a comment from an issue
  104. func DeleteIssueComment(c *context.APIContext) {
  105. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  106. if err != nil {
  107. if models.IsErrCommentNotExist(err) {
  108. c.Error(404, "GetCommentByID", err)
  109. } else {
  110. c.Error(500, "GetCommentByID", err)
  111. }
  112. return
  113. }
  114. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  115. c.Status(403)
  116. return
  117. } else if comment.Type != models.CommentTypeComment {
  118. c.Status(204)
  119. return
  120. }
  121. if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
  122. c.Error(500, "DeleteCommentByID", err)
  123. return
  124. }
  125. c.Status(204)
  126. }