]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/misc.ts
Fix video full description
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / misc.ts
CommitLineData
e34c85e5 1import * as validator from 'validator'
e4f97bab 2import { exists } from '../misc'
e34c85e5
C
3import { isTestInstance } from '../../core-utils'
4import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
e4f97bab
C
5
6function isActivityPubUrlValid (url: string) {
7 const isURLOptions = {
8 require_host: true,
9 require_tld: true,
10 require_protocol: true,
11 require_valid_protocol: true,
12 protocols: [ 'http', 'https' ]
13 }
14
e34c85e5
C
15 // We validate 'localhost', so we don't have the top level domain
16 if (isTestInstance()) {
17 isURLOptions.require_tld = false
18 }
19
20 return exists(url) && validator.isURL(url, isURLOptions) && validator.isLength(url, CONSTRAINTS_FIELDS.ACCOUNTS.URL)
e4f97bab
C
21}
22
0d0e8dd0 23function isBaseActivityValid (activity: any, type: string) {
20494f12 24 return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
0d0e8dd0 25 activity.type === type &&
350e31d6
C
26 isActivityPubUrlValid(activity.id) &&
27 isActivityPubUrlValid(activity.actor) &&
28 (
29 activity.to === undefined ||
30 (Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))
31 )
0d0e8dd0
C
32}
33
e4f97bab 34export {
0d0e8dd0
C
35 isActivityPubUrlValid,
36 isBaseActivityValid
e4f97bab 37}