You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_comment.go 3.4KB

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