-import * as validator from 'validator'
+import validator from 'validator'
import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
-import { exists, isArray } from '../misc'
+import { exists, isArray, isDateValid } from '../misc'
import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
import { isHostValid } from '../servers'
import { peertubeTruncate } from '@server/helpers/core-utils'
function isActorEndpointsObjectValid (endpointObject: any) {
- return isActivityPubUrlValid(endpointObject.sharedInbox)
+ if (endpointObject?.sharedInbox) {
+ return isActivityPubUrlValid(endpointObject.sharedInbox)
+ }
+
+ // Shared inbox is optional
+ return true
}
function isActorPublicKeyObjectValid (publicKeyObject: any) {
}
function isActorTypeValid (type: string) {
- return type === 'Person' || type === 'Application' || type === 'Group'
+ return type === 'Person' || type === 'Application' || type === 'Group' || type === 'Service' || type === 'Organization'
}
function isActorPublicKeyValid (publicKey: string) {
return exists(publicKey) &&
typeof publicKey === 'string' &&
publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
- publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 &&
+ publicKey.includes('-----END PUBLIC KEY-----') &&
validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
}
function isActorPrivateKeyValid (privateKey: string) {
return exists(privateKey) &&
typeof privateKey === 'string' &&
- privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
+ (privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') || privateKey.startsWith('-----BEGIN PRIVATE KEY-----')) &&
// Sometimes there is a \n at the end, so just assert the string contains the end mark
- privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
+ (privateKey.includes('-----END RSA PRIVATE KEY-----') || privateKey.includes('-----END PRIVATE KEY-----')) &&
validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
}
-function isActorObjectValid (actor: any) {
+function isActorFollowingCountValid (value: string) {
+ return exists(value) && validator.isInt('' + value, { min: 0 })
+}
+
+function isActorFollowersCountValid (value: string) {
+ return exists(value) && validator.isInt('' + value, { min: 0 })
+}
+
+function isActorDeleteActivityValid (activity: any) {
+ return isBaseActivityValid(activity, 'Delete')
+}
+
+function sanitizeAndCheckActorObject (actor: any) {
+ if (!isActorTypeValid(actor.type)) return false
+
+ normalizeActor(actor)
+
return exists(actor) &&
isActivityPubUrlValid(actor.id) &&
- isActorTypeValid(actor.type) &&
isActivityPubUrlValid(actor.inbox) &&
isActorPreferredUsernameValid(actor.preferredUsername) &&
isActivityPubUrlValid(actor.url) &&
(!actor.followers || isActivityPubUrlValid(actor.followers)) &&
setValidAttributedTo(actor) &&
+ setValidDescription(actor) &&
// If this is a group (a channel), it should be attributed to an account
// In PeerTube we use this to attach a video channel to a specific account
(actor.type !== 'Group' || actor.attributedTo.length !== 0)
}
-function isActorFollowingCountValid (value: string) {
- return exists(value) && validator.isInt('' + value, { min: 0 })
-}
-
-function isActorFollowersCountValid (value: string) {
- return exists(value) && validator.isInt('' + value, { min: 0 })
-}
-
-function isActorDeleteActivityValid (activity: any) {
- return isBaseActivityValid(activity, 'Delete')
-}
-
-function sanitizeAndCheckActorObject (object: any) {
- normalizeActor(object)
-
- return isActorObjectValid(object)
-}
-
function normalizeActor (actor: any) {
- if (!actor || !actor.url) return
+ if (!actor) return
- if (typeof actor.url !== 'string') {
+ if (!actor.url) {
+ actor.url = actor.id
+ } else if (typeof actor.url !== 'string') {
actor.url = actor.url.href || actor.url.url
}
+ if (!isDateValid(actor.published)) actor.published = undefined
+
if (actor.summary && typeof actor.summary === 'string') {
actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
actor.summary = null
}
}
-
- return
}
function isValidActorHandle (handle: string) {
return isArray(handles) && handles.every(h => isValidActorHandle(h))
}
+function setValidDescription (obj: any) {
+ if (!obj.summary) obj.summary = null
+
+ return true
+}
+
// ---------------------------------------------------------------------------
export {
isActorPublicKeyValid,
isActorPreferredUsernameValid,
isActorPrivateKeyValid,
- isActorObjectValid,
isActorFollowingCountValid,
isActorFollowersCountValid,
isActorDeleteActivityValid,