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.

threads.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 The Gitea 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 notify
  5. import (
  6. "fmt"
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. )
  11. // GetThread get notification by ID
  12. func GetThread(ctx *context.APIContext) {
  13. // swagger:operation GET /notifications/threads/{id} notification notifyGetThread
  14. // ---
  15. // summary: Get notification thread by ID
  16. // consumes:
  17. // - application/json
  18. // produces:
  19. // - application/json
  20. // parameters:
  21. // - name: id
  22. // in: path
  23. // description: id of notification thread
  24. // type: string
  25. // required: true
  26. // responses:
  27. // "200":
  28. // "$ref": "#/responses/NotificationThread"
  29. // "403":
  30. // "$ref": "#/responses/forbidden"
  31. // "404":
  32. // "$ref": "#/responses/notFound"
  33. n := getThread(ctx)
  34. if n == nil {
  35. return
  36. }
  37. if err := n.LoadAttributes(); err != nil {
  38. ctx.InternalServerError(err)
  39. return
  40. }
  41. ctx.JSON(http.StatusOK, n.APIFormat())
  42. }
  43. // ReadThread mark notification as read by ID
  44. func ReadThread(ctx *context.APIContext) {
  45. // swagger:operation PATCH /notifications/threads/{id} notification notifyReadThread
  46. // ---
  47. // summary: Mark notification thread as read by ID
  48. // consumes:
  49. // - application/json
  50. // produces:
  51. // - application/json
  52. // parameters:
  53. // - name: id
  54. // in: path
  55. // description: id of notification thread
  56. // type: string
  57. // required: true
  58. // responses:
  59. // "205":
  60. // "$ref": "#/responses/empty"
  61. // "403":
  62. // "$ref": "#/responses/forbidden"
  63. // "404":
  64. // "$ref": "#/responses/notFound"
  65. n := getThread(ctx)
  66. if n == nil {
  67. return
  68. }
  69. err := models.SetNotificationStatus(n.ID, ctx.User, models.NotificationStatusRead)
  70. if err != nil {
  71. ctx.InternalServerError(err)
  72. return
  73. }
  74. ctx.Status(http.StatusResetContent)
  75. }
  76. func getThread(ctx *context.APIContext) *models.Notification {
  77. n, err := models.GetNotificationByID(ctx.ParamsInt64(":id"))
  78. if err != nil {
  79. if models.IsErrNotExist(err) {
  80. ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
  81. } else {
  82. ctx.InternalServerError(err)
  83. }
  84. return nil
  85. }
  86. if n.UserID != ctx.User.ID && !ctx.User.IsAdmin {
  87. ctx.Error(http.StatusForbidden, "GetNotificationByID", fmt.Errorf("only user itself and admin are allowed to read/change this thread %d", n.ID))
  88. return nil
  89. }
  90. return n
  91. }