aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/user-subscriptions.ts
blob: 68d83add528e5c81929b301016d3d403ced3c1eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import express from 'express'
import { body, param, query } from 'express-validator'
import { arrayify } from '@shared/core-utils'
import { FollowState } from '@shared/models'
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor'
import { WEBSERVER } from '../../initializers/constants'
import { ActorFollowModel } from '../../models/actor/actor-follow'
import { areValidationErrors } from './shared'

const userSubscriptionListValidator = [
  query('search')
    .optional()
    .not().isEmpty(),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    return next()
  }
]

const userSubscriptionAddValidator = [
  body('uri')
    .custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    return next()
  }
]

const areSubscriptionsExistValidator = [
  query('uris')
    .customSanitizer(arrayify)
    .custom(areValidActorHandles).withMessage('Should have a valid array of URIs'),

  (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return

    return next()
  }
]

const userSubscriptionGetValidator = [
  param('uri')
    .custom(isValidActorHandle),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return
    if (!await doesSubscriptionExist({ uri: req.params.uri, res, state: 'accepted' })) return

    return next()
  }
]

const userSubscriptionDeleteValidator = [
  param('uri')
    .custom(isValidActorHandle),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (areValidationErrors(req, res)) return
    if (!await doesSubscriptionExist({ uri: req.params.uri, res })) return

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  areSubscriptionsExistValidator,
  userSubscriptionListValidator,
  userSubscriptionAddValidator,
  userSubscriptionGetValidator,
  userSubscriptionDeleteValidator
}

// ---------------------------------------------------------------------------

async function doesSubscriptionExist (options: {
  uri: string
  res: express.Response
  state?: FollowState
}) {
  const { uri, res, state } = options

  let [ name, host ] = uri.split('@')
  if (host === WEBSERVER.HOST) host = null

  const user = res.locals.oauth.token.User
  const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHostForAPI({
    actorId: user.Account.Actor.id,
    targetName: name,
    targetHost: host,
    state
  })

  if (!subscription?.ActorFollowing.VideoChannel) {
    res.fail({
      status: HttpStatusCode.NOT_FOUND_404,
      message: `Subscription ${uri} not found.`
    })
    return false
  }

  res.locals.subscription = subscription

  return true
}