1 import * as validator from 'validator'
2 import { CONSTRAINTS_FIELDS } from '../../../initializers'
3 import { isTestInstance } from '../../core-utils'
4 import { exists } from '../misc'
6 function isActivityPubUrlValid (url: string) {
10 require_protocol: true,
11 require_valid_protocol: true,
12 protocols: [ 'http', 'https' ]
15 // We validate 'localhost', so we don't have the top level domain
16 if (isTestInstance()) {
17 isURLOptions.require_tld = false
20 return exists(url) && validator.isURL('' + url, isURLOptions) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTOR.URL)
23 function isBaseActivityValid (activity: any, type: string) {
24 return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
25 activity.type === type &&
26 isActivityPubUrlValid(activity.id) &&
27 isActivityPubUrlValid(activity.actor) &&
29 activity.to === undefined ||
30 (Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))
33 activity.cc === undefined ||
34 (Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t)))
38 function setValidAttributedTo (obj: any) {
39 if (Array.isArray(obj.attributedTo) === false) {
44 const newAttributesTo = obj.attributedTo.filter(a => {
45 return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
48 obj.attributedTo = newAttributesTo
54 isActivityPubUrlValid,