user-subscriptions.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import express from 'express'
  2. import { body, param, query } from 'express-validator'
  3. import { arrayify } from '@peertube/peertube-core-utils'
  4. import { FollowState, HttpStatusCode } from '@peertube/peertube-models'
  5. import { areValidActorHandles, isValidActorHandle } from '../../../helpers/custom-validators/activitypub/actor.js'
  6. import { WEBSERVER } from '../../../initializers/constants.js'
  7. import { ActorFollowModel } from '../../../models/actor/actor-follow.js'
  8. import { areValidationErrors } from '../shared/index.js'
  9. const userSubscriptionListValidator = [
  10. query('search')
  11. .optional()
  12. .not().isEmpty(),
  13. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  14. if (areValidationErrors(req, res)) return
  15. return next()
  16. }
  17. ]
  18. const userSubscriptionAddValidator = [
  19. body('uri')
  20. .custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
  21. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  22. if (areValidationErrors(req, res)) return
  23. return next()
  24. }
  25. ]
  26. const areSubscriptionsExistValidator = [
  27. query('uris')
  28. .customSanitizer(arrayify)
  29. .custom(areValidActorHandles).withMessage('Should have a valid array of URIs'),
  30. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  31. if (areValidationErrors(req, res)) return
  32. return next()
  33. }
  34. ]
  35. const userSubscriptionGetValidator = [
  36. param('uri')
  37. .custom(isValidActorHandle),
  38. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  39. if (areValidationErrors(req, res)) return
  40. if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return
  41. return next()
  42. }
  43. ]
  44. const userSubscriptionDeleteValidator = [
  45. param('uri')
  46. .custom(isValidActorHandle),
  47. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  48. if (areValidationErrors(req, res)) return
  49. if (!await doesSubscriptionExist({ uri: req.params.uri, res })) return
  50. return next()
  51. }
  52. ]
  53. // ---------------------------------------------------------------------------
  54. export {
  55. areSubscriptionsExistValidator,
  56. userSubscriptionListValidator,
  57. userSubscriptionAddValidator,
  58. userSubscriptionGetValidator,
  59. userSubscriptionDeleteValidator
  60. }
  61. // ---------------------------------------------------------------------------
  62. async function doesSubscriptionExist (options: {
  63. uri: string
  64. res: express.Response
  65. state?: FollowState
  66. }) {
  67. const { uri, res, state } = options
  68. let [ name, host ] = uri.split('@')
  69. if (host === WEBSERVER.HOST) host = null
  70. const user = res.locals.oauth.token.User
  71. const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({
  72. actorId: user.Account.Actor.id,
  73. targetName: name,
  74. targetHost: host,
  75. state
  76. })
  77. if (!subscription?.ActorFollowing.VideoChannel) {
  78. res.fail({
  79. status: HttpStatusCode.NOT_FOUND_404,
  80. message: `Subscription ${uri} not found.`
  81. })
  82. return false
  83. }
  84. res.locals.subscription = subscription
  85. return true
  86. }