2017-11-14 04:57:56 -05:00
|
|
|
import * as validator from 'validator'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { CONSTRAINTS_FIELDS } from '../../../initializers'
|
2017-11-14 04:57:56 -05:00
|
|
|
import { isTestInstance } from '../../core-utils'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { exists } from '../misc'
|
2017-11-09 11:51:58 -05: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 04:57:56 -05:00
|
|
|
// We validate 'localhost', so we don't have the top level domain
|
|
|
|
if (isTestInstance()) {
|
|
|
|
isURLOptions.require_tld = false
|
|
|
|
}
|
|
|
|
|
2018-01-03 05:10:40 -05:00
|
|
|
return exists(url) && validator.isURL('' + url, isURLOptions) && validator.isLength('' + url, CONSTRAINTS_FIELDS.ACTORS.URL)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
2017-11-10 08:34:45 -05:00
|
|
|
function isBaseActivityValid (activity: any, type: string) {
|
2017-11-16 09:22:39 -05:00
|
|
|
return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
|
2017-11-10 08:34:45 -05:00
|
|
|
activity.type === type &&
|
2017-11-14 11:31:26 -05:00
|
|
|
isActivityPubUrlValid(activity.id) &&
|
2018-03-22 13:40:33 -04:00
|
|
|
(isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id)) &&
|
2017-11-14 11:31:26 -05:00
|
|
|
(
|
|
|
|
activity.to === undefined ||
|
|
|
|
(Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))
|
2017-11-17 09:20:42 -05:00
|
|
|
) &&
|
|
|
|
(
|
|
|
|
activity.cc === undefined ||
|
|
|
|
(Array.isArray(activity.cc) && activity.cc.every(t => isActivityPubUrlValid(t)))
|
2017-11-14 11:31:26 -05:00
|
|
|
)
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
function setValidAttributedTo (obj: any) {
|
|
|
|
if (Array.isArray(obj.attributedTo) === false) {
|
|
|
|
obj.attributedTo = []
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const newAttributesTo = obj.attributedTo.filter(a => {
|
|
|
|
return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id)
|
|
|
|
})
|
|
|
|
|
|
|
|
obj.attributedTo = newAttributesTo
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:51:58 -05:00
|
|
|
export {
|
2017-11-10 08:34:45 -05:00
|
|
|
isActivityPubUrlValid,
|
2017-12-14 11:38:41 -05:00
|
|
|
isBaseActivityValid,
|
|
|
|
setValidAttributedTo
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|