1 import * as validator from 'validator'
2 import { exists } from '../misc'
3 import { isTestInstance } from '../../core-utils'
4 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
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.ACCOUNTS.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)))
35 isActivityPubUrlValid,