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.

141 lines
3.6 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.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. }