issue_comment.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. comment.Content = form.Body
  80. if err := models.UpdateComment(comment); err != nil {
  81. ctx.Error(500, "UpdateComment", err)
  82. return
  83. }
  84. ctx.JSON(200, comment.APIFormat())
  85. }
  86. func DeleteIssueComment(ctx *context.APIContext) {
  87. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  88. if err != nil {
  89. if models.IsErrCommentNotExist(err) {
  90. ctx.Error(404, "GetCommentByID", err)
  91. } else {
  92. ctx.Error(500, "GetCommentByID", err)
  93. }
  94. return
  95. }
  96. if ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin() {
  97. ctx.Status(403)
  98. return
  99. } else if comment.Type != models.COMMENT_TYPE_COMMENT {
  100. ctx.Status(204)
  101. return
  102. }
  103. if err = models.DeleteCommentByID(comment.ID); err != nil {
  104. ctx.Error(500, "DeleteCommentByID", err)
  105. return
  106. }
  107. ctx.Status(204)
  108. }