PeerTube/server/helpers/custom-validators/activitypub/misc.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-11-14 03:57:56 -06:00
import * as validator from 'validator'
2017-11-09 10:51:58 -06:00
import { exists } from '../misc'
2017-11-14 03:57:56 -06:00
import { isTestInstance } from '../../core-utils'
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
2017-11-09 10:51:58 -06:00
function isActivityPubUrlValid (url: string) {
const isURLOptions = {
require_host: true,
require_tld: true,
require_protocol: true,
require_valid_protocol: true,
protocols: [ 'http', 'https' ]
}
2017-11-14 03:57:56 -06:00
// We validate 'localhost', so we don't have the top level domain
if (isTestInstance()) {
isURLOptions.require_tld = false
}
return exists(url) && validator.isURL(url, isURLOptions) && validator.isLength(url, CONSTRAINTS_FIELDS.ACCOUNTS.URL)
2017-11-09 10:51:58 -06:00
}
2017-11-10 07:34:45 -06:00
function isBaseActivityValid (activity: any, type: string) {
2017-11-16 08:22:39 -06:00
return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
2017-11-10 07:34:45 -06:00
activity.type === type &&
2017-11-14 10:31:26 -06:00
isActivityPubUrlValid(activity.id) &&
isActivityPubUrlValid(activity.actor) &&
(
activity.to === undefined ||
(Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))
2017-11-17 08:20:42 -06:00
) &&
(
activity.cc === undefined ||
(Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t)))
2017-11-14 10:31:26 -06:00
)
2017-11-10 07:34:45 -06:00
}
2017-11-09 10:51:58 -06:00
export {
2017-11-10 07:34:45 -06:00
isActivityPubUrlValid,
isBaseActivityValid
2017-11-09 10:51:58 -06:00
}