2
1

user-subscriptions.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import { body, param, query } from 'express-validator/check'
  4. import { logger } from '../../helpers/logger'
  5. import { areValidationErrors } from './utils'
  6. import { ActorFollowModel } from '../../models/activitypub/actor-follow'
  7. import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
  8. import { toArray } from '../../helpers/custom-validators/misc'
  9. import { WEBSERVER } from '../../initializers/constants'
  10. const userSubscriptionAddValidator = [
  11. body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),
  12. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  13. logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body })
  14. if (areValidationErrors(req, res)) return
  15. return next()
  16. }
  17. ]
  18. const areSubscriptionsExistValidator = [
  19. query('uris')
  20. .customSanitizer(toArray)
  21. .custom(areValidActorHandles).withMessage('Should have a valid uri array'),
  22. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  23. logger.debug('Checking areSubscriptionsExistValidator parameters', { parameters: req.query })
  24. if (areValidationErrors(req, res)) return
  25. return next()
  26. }
  27. ]
  28. const userSubscriptionGetValidator = [
  29. param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'),
  30. async (req: express.Request, res: express.Response, next: express.NextFunction) => {
  31. logger.debug('Checking userSubscriptionGetValidator parameters', { parameters: req.params })
  32. if (areValidationErrors(req, res)) return
  33. let [ name, host ] = req.params.uri.split('@')
  34. if (host === WEBSERVER.HOST) host = null
  35. const user = res.locals.oauth.token.User
  36. const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI(user.Account.Actor.id, name, host)
  37. if (!subscription || !subscription.ActorFollowing.VideoChannel) {
  38. return res
  39. .status(404)
  40. .json({
  41. error: `Subscription ${req.params.uri} not found.`
  42. })
  43. .end()
  44. }
  45. res.locals.subscription = subscription
  46. return next()
  47. }
  48. ]
  49. // ---------------------------------------------------------------------------
  50. export {
  51. areSubscriptionsExistValidator,
  52. userSubscriptionAddValidator,
  53. userSubscriptionGetValidator
  54. }