issue_comment.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. func ListIssueComments(c *context.APIContext) {
  14. var since time.Time
  15. if len(c.Query("since")) > 0 {
  16. var err error
  17. since, err = time.Parse(time.RFC3339, c.Query("since"))
  18. if err != nil {
  19. c.Error(422, "", err)
  20. return
  21. }
  22. }
  23. // comments,err:=models.GetCommentsByIssueIDSince(, since)
  24. issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  25. if err != nil {
  26. c.Error(500, "GetRawIssueByIndex", err)
  27. return
  28. }
  29. comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
  30. if err != nil {
  31. c.Error(500, "GetCommentsByIssueIDSince", err)
  32. return
  33. }
  34. apiComments := make([]*api.Comment, len(comments))
  35. for i := range comments {
  36. apiComments[i] = comments[i].APIFormat()
  37. }
  38. c.JSON(200, &apiComments)
  39. }
  40. func ListRepoIssueComments(c *context.APIContext) {
  41. var since time.Time
  42. if len(c.Query("since")) > 0 {
  43. var err error
  44. since, err = time.Parse(time.RFC3339, c.Query("since"))
  45. if err != nil {
  46. c.Error(422, "", err)
  47. return
  48. }
  49. }
  50. comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
  51. if err != nil {
  52. c.Error(500, "GetCommentsByRepoIDSince", err)
  53. return
  54. }
  55. apiComments := make([]*api.Comment, len(comments))
  56. for i := range comments {
  57. apiComments[i] = comments[i].APIFormat()
  58. }
  59. c.JSON(200, &apiComments)
  60. }
  61. func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
  62. issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
  63. if err != nil {
  64. c.Error(500, "GetIssueByIndex", err)
  65. return
  66. }
  67. comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
  68. if err != nil {
  69. c.Error(500, "CreateIssueComment", err)
  70. return
  71. }
  72. c.JSON(201, comment.APIFormat())
  73. }
  74. func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
  75. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  76. if err != nil {
  77. if models.IsErrCommentNotExist(err) {
  78. c.Error(404, "GetCommentByID", err)
  79. } else {
  80. c.Error(500, "GetCommentByID", err)
  81. }
  82. return
  83. }
  84. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  85. c.Status(403)
  86. return
  87. } else if comment.Type != models.CommentTypeComment {
  88. c.Status(204)
  89. return
  90. }
  91. oldContent := comment.Content
  92. comment.Content = form.Body
  93. if err := models.UpdateComment(c.User, comment, oldContent); err != nil {
  94. c.Error(500, "UpdateComment", err)
  95. return
  96. }
  97. c.JSON(200, comment.APIFormat())
  98. }
  99. func DeleteIssueComment(c *context.APIContext) {
  100. comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
  101. if err != nil {
  102. if models.IsErrCommentNotExist(err) {
  103. c.Error(404, "GetCommentByID", err)
  104. } else {
  105. c.Error(500, "GetCommentByID", err)
  106. }
  107. return
  108. }
  109. if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
  110. c.Status(403)
  111. return
  112. } else if comment.Type != models.CommentTypeComment {
  113. c.Status(204)
  114. return
  115. }
  116. if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
  117. c.Error(500, "DeleteCommentByID", err)
  118. return
  119. }
  120. c.Status(204)
  121. }