]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/activitypub/misc.ts
Fetch remote AP objects
[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
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
01de67b9 20 return exists(url) && validator.isURL('' + url, isURLOptions) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.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)))
9a27cdc2
C
31 ) &&
32 (
33 activity.cc === undefined ||
34 (Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t)))
350e31d6 35 )
0d0e8dd0
C
36}
37
50d6de9c
C
38function setValidAttributedTo (obj: any) {
39 if (Array.isArray(obj.attributedTo) === false) {
40 obj.attributedTo = []
41 return true
42 }
43
44 const newAttributesTo = obj.attributedTo.filter(a => {
45 return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
46 })
47
48 obj.attributedTo = newAttributesTo
49
50 return true
51}
52
e4f97bab 53export {
0d0e8dd0 54 isActivityPubUrlValid,
50d6de9c
C
55 isBaseActivityValid,
56 setValidAttributedTo
e4f97bab 57}