gitea源码

threads.go 3.0KB

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