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.

133 lines
3.4 KiB

8 years ago
  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. }