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

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