issue_comment.go 3.3 KB

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