issue_comment.go 3.1 KB

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