]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/misc.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / misc.ts
CommitLineData
e34c85e5 1import * as validator from 'validator'
3fd3ab2d 2import { CONSTRAINTS_FIELDS } from '../../../initializers'
e34c85e5 3import { isTestInstance } from '../../core-utils'
3fd3ab2d 4import { exists } from '../misc'
e4f97bab 5
c48e82b5 6function isUrlValid (url: string) {
e4f97bab
C
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
c48e82b5
C
20 return exists(url) && validator.isURL('' + url, isURLOptions)
21}
22
23function isActivityPubUrlValid (url: string) {
24 return isUrlValid(url) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
e4f97bab
C
25}
26
0d0e8dd0 27function isBaseActivityValid (activity: any, type: string) {
20494f12 28 return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
0d0e8dd0 29 activity.type === type &&
350e31d6 30 isActivityPubUrlValid(activity.id) &&
848f499d
C
31 isObjectValid(activity.actor) &&
32 isUrlCollectionValid(activity.to) &&
33 isUrlCollectionValid(activity.cc)
34}
35
36function isUrlCollectionValid (collection: any) {
37 return collection === undefined ||
38 (Array.isArray(collection) && collection.every(t => isActivityPubUrlValid(t)))
39}
40
41function isObjectValid (object: any) {
42 return exists(object) &&
9a27cdc2 43 (
848f499d 44 isActivityPubUrlValid(object) || isActivityPubUrlValid(object.id)
350e31d6 45 )
0d0e8dd0
C
46}
47
50d6de9c
C
48function setValidAttributedTo (obj: any) {
49 if (Array.isArray(obj.attributedTo) === false) {
50 obj.attributedTo = []
51 return true
52 }
53
8b604880 54 obj.attributedTo = obj.attributedTo.filter(a => {
50d6de9c
C
55 return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
56 })
57
50d6de9c
C
58 return true
59}
60
e4f97bab 61export {
c48e82b5 62 isUrlValid,
0d0e8dd0 63 isActivityPubUrlValid,
50d6de9c 64 isBaseActivityValid,
848f499d
C
65 setValidAttributedTo,
66 isObjectValid
e4f97bab 67}