issue_comment.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
  34. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  35. if err != nil {
  36. ctx.Error(500, "GetIssueByIndex", err)
  37. return
  38. }
  39. comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
  40. if err != nil {
  41. ctx.Error(500, "CreateIssueComment", err)
  42. return
  43. }
  44. ctx.JSON(201, comment.APIFormat())
  45. }
  46. func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) {
  47. comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
  48. if err != nil {
  49. if models.IsErrCommentNotExist(err) {
  50. ctx.Error(404, "GetCommentByID", err)
  51. } else {
  52. ctx.Error(500, "GetCommentByID", err)
  53. }
  54. return
  55. }
  56. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  57. ctx.Status(403)
  58. return
  59. } else if comment.Type != models.COMMENT_TYPE_COMMENT {
  60. ctx.Status(204)
  61. return
  62. }
  63. comment.Content = form.Body
  64. if err := models.UpdateComment(comment); err != nil {
  65. ctx.Error(500, "UpdateComment", err)
  66. return
  67. }
  68. ctx.JSON(200, comment.APIFormat())
  69. }