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 isUrlValid (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)
23 function isActivityPubUrlValid (url: string) {
24 return isUrlValid(url) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
27 function isBaseActivityValid (activity: any, type: string) {
28 return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
29 activity.type === type &&
30 isActivityPubUrlValid(activity.id) &&
31 exists(activity.actor) &&
32 (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id)) &&
34 activity.to === undefined ||
35 (Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))
38 activity.cc === undefined ||
39 (Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t)))
43 function setValidAttributedTo (obj: any) {
44 if (Array.isArray(obj.attributedTo) === false) {
49 obj.attributedTo = obj.attributedTo.filter(a => {
50 return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
58 isActivityPubUrlValid,