You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_comment.go 2.3 kB

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