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