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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "github.com/gogits/gogs/modules/log"
  11. )
  12. const (
  13. ISO8601Format = "2006-01-02T15:04:05Z"
  14. )
  15. // ListIssueComments list comments on an issue
  16. func ListIssueComments(ctx *context.APIContext) {
  17. var since time.Time
  18. var withSince bool
  19. // we get the issue instead of comments directly
  20. // because to get comments we need to gets issue first,
  21. // and there is already comments in the issue
  22. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  23. if err != nil {
  24. ctx.Error(500, "Comments", err)
  25. return
  26. }
  27. // parse `since`, by default we don't use `since`
  28. if len(ctx.Query("since")) > 0 {
  29. var err error
  30. since, err = time.Parse(ISO8601Format, ctx.Query("since"))
  31. if err == nil {
  32. withSince = true
  33. }
  34. }
  35. apiComments := []*api.Comment{}
  36. for _, comment := range issue.Comments {
  37. if withSince && !comment.Created.After(since) {
  38. continue
  39. }
  40. apiComments = append(apiComments, comment.APIFormat())
  41. }
  42. ctx.JSON(200, &apiComments)
  43. }
  44. // CreateIssueComment create comment on an issue
  45. func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) {
  46. // check issue
  47. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
  48. if err != nil {
  49. ctx.Error(500, "Comments", err)
  50. return
  51. }
  52. comment := &models.Comment{
  53. Content: form.Body,
  54. }
  55. if len(form.Body) == 0 {
  56. ctx.Handle(400, "CreateIssueComment:empty content", err)
  57. return
  58. }
  59. // create comment
  60. comment, err = models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, []string{})
  61. if err != nil {
  62. ctx.Handle(500, "CreateIssueComment", err)
  63. return
  64. }
  65. log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
  66. // Refetch from database to assign some automatic values
  67. comment, err = models.GetCommentByID(comment.ID)
  68. if err != nil {
  69. log.Info("Failed to refetch comment:%v", err)
  70. }
  71. ctx.JSON(201, comment.APIFormat())
  72. }
  73. // EditIssueComment edits an issue comment
  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.Handle(500, "GetCommentByID", err)
  81. }
  82. return
  83. }
  84. if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
  85. ctx.Error(403, "edit comment", err)
  86. return
  87. } else if comment.Type != models.COMMENT_TYPE_COMMENT {
  88. ctx.Error(204, "edit comment", err)
  89. return
  90. }
  91. comment.Content = form.Body
  92. if len(comment.Content) == 0 {
  93. ctx.JSON(200, map[string]interface{}{
  94. "content": "",
  95. })
  96. return
  97. }
  98. if err := models.UpdateComment(comment); err != nil {
  99. ctx.Handle(500, "UpdateComment", err)
  100. return
  101. }
  102. ctx.JSON(200, comment.APIFormat())
  103. }