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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // - name: to-status
  59. // in: query
  60. // description: Status to mark notifications as
  61. // type: string
  62. // default: read
  63. // required: false
  64. // responses:
  65. // "205":
  66. // "$ref": "#/responses/empty"
  67. // "403":
  68. // "$ref": "#/responses/forbidden"
  69. // "404":
  70. // "$ref": "#/responses/notFound"
  71. n := getThread(ctx)
  72. if n == nil {
  73. return
  74. }
  75. targetStatus := statusStringToNotificationStatus(ctx.Query("to-status"))
  76. if targetStatus == 0 {
  77. targetStatus = models.NotificationStatusRead
  78. }
  79. err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
  80. if err != nil {
  81. ctx.InternalServerError(err)
  82. return
  83. }
  84. ctx.Status(http.StatusResetContent)
  85. }
  86. func getThread(ctx *context.APIContext) *models.Notification {
  87. n, err := models.GetNotificationByID(ctx.ParamsInt64(":id"))
  88. if err != nil {
  89. if models.IsErrNotExist(err) {
  90. ctx.Error(http.StatusNotFound, "GetNotificationByID", err)
  91. } else {
  92. ctx.InternalServerError(err)
  93. }
  94. return nil
  95. }
  96. if n.UserID != ctx.User.ID && !ctx.User.IsAdmin {
  97. ctx.Error(http.StatusForbidden, "GetNotificationByID", fmt.Errorf("only user itself and admin are allowed to read/change this thread %d", n.ID))
  98. return nil
  99. }
  100. return n
  101. }