import * as validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../../initializers'
import { exists } from '../misc'
+import { truncate } from 'lodash'
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
+import { isHostValid } from '../servers'
function isActorEndpointsObjectValid (endpointObject: any) {
return isActivityPubUrlValid(endpointObject.sharedInbox)
validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
}
-const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
+const actorNameRegExp = new RegExp('^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\\-_]+$')
function isActorPreferredUsernameValid (preferredUsername: string) {
return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
}
isActorPublicKeyObjectValid(actor.publicKey) &&
isActorEndpointsObjectValid(actor.endpoints) &&
setValidAttributedTo(actor) &&
+
// If this is not an account, it should be attributed to an account
// In PeerTube we use this to attach a video channel to a specific account
(actor.type === 'Person' || actor.attributedTo.length !== 0)
return isBaseActivityValid(activity, 'Accept')
}
+function isActorRejectActivityValid (activity: any) {
+ return isBaseActivityValid(activity, 'Reject')
+}
+
function isActorUpdateActivityValid (activity: any) {
+ normalizeActor(activity.object)
+
return isBaseActivityValid(activity, 'Update') &&
isActorObjectValid(activity.object)
}
+function normalizeActor (actor: any) {
+ if (!actor || !actor.url) return
+
+ if (typeof actor.url !== 'string') {
+ actor.url = actor.url.href || actor.url.url
+ }
+
+ if (actor.summary && typeof actor.summary === 'string') {
+ actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
+
+ if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
+ actor.summary = null
+ }
+ }
+
+ return
+}
+
+function isValidActorHandle (handle: string) {
+ if (!exists(handle)) return false
+
+ const parts = handle.split('@')
+ if (parts.length !== 2) return false
+
+ return isHostValid(parts[1])
+}
+
// ---------------------------------------------------------------------------
export {
+ normalizeActor,
isActorEndpointsObjectValid,
isActorPublicKeyObjectValid,
isActorTypeValid,
isActorFollowersCountValid,
isActorFollowActivityValid,
isActorAcceptActivityValid,
+ isActorRejectActivityValid,
isActorDeleteActivityValid,
- isActorUpdateActivityValid
+ isActorUpdateActivityValid,
+ isValidActorHandle
}