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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.FindComments(models.FindCommentsOptions{
  24. IssueID: issue.ID,
  25. Since: since.Unix(),
  26. Type: models.CommentTypeComment,
  27. })
  28. if err != nil {
  29. ctx.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. ctx.JSON(200, &apiComments)
  37. }
  38. // ListRepoIssueComments returns all issue-comments for an issue
  39. func ListRepoIssueComments(ctx *context.APIContext) {
  40. var since time.Time
  41. if len(ctx.Query("since")) > 0 {
  42. since, _ = time.Parse(time.RFC3339, ctx.Query("since"))
  43. }
  44. comments, err := models.FindComments(models.FindCommentsOptions{
  45. RepoID: ctx.Repo.Repository.ID,
  46. Since: since.Unix(),
  47. Type: models.CommentTypeComment,
  48. })
  49. if err != nil {
  50. ctx.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. ctx.JSON(200, &apiComments)
  58. }
  59. // CreateIssueComment create a comment for an issue
  60. func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
  61. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  62. if err != nil {
  63. ctx.Error(500, "GetIssueByIndex", err)
  64. return
  65. }
  66. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
  67. if err != nil {
  68. ctx.Error(500, "CreateIssueComment", err)
  69. return
  70. }
  71. ctx.JSON(201, comment.APIFormat())
  72. }
  73. // EditIssueComment modify a comment of an issue
  74. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  75. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  76. if err != nil {
  77. if models.IsErrCommentNotExist(err) {
  78. ctx.Error(404, "GetCommentByID", err)
  79. } else {
  80. ctx.Error(500, "GetCommentByID", err)
  81. }
  82. return
  83. }
  84. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  85. ctx.Status(403)
  86. return
  87. } else if comment.Type != models.CommentTypeComment {
  88. ctx.Status(204)
  89. return
  90. }
  91. comment.Content = form.Body
  92. if err := models.UpdateComment(comment); err != nil {
  93. ctx.Error(500, "UpdateComment", err)
  94. return
  95. }
  96. ctx.JSON(200, comment.APIFormat())
  97. }
  98. // DeleteIssueComment delete a comment from an issue
  99. func DeleteIssueComment(ctx *context.APIContext) {
  100. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  101. if err != nil {
  102. if models.IsErrCommentNotExist(err) {
  103. ctx.Error(404, "GetCommentByID", err)
  104. } else {
  105. ctx.Error(500, "GetCommentByID", err)
  106. }
  107. return
  108. }
  109. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  110. ctx.Status(403)
  111. return
  112. } else if comment.Type != models.CommentTypeComment {
  113. ctx.Status(204)
  114. return
  115. }
  116. if err = models.DeleteComment(comment); err != nil {
  117. ctx.Error(500, "DeleteCommentByID", err)
  118. return
  119. }
  120. ctx.Status(204)
  121. }