]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/activitypub/actor.ts
Add ability to search video channels
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
index df0edc30e5bde124a49f9900674f2123fe8fd0da..6958b2b00568b6094f9b7dd042a54ea2bf14e1e7 100644 (file)
@@ -1,7 +1,9 @@
 import * as validator from 'validator'
 import { CONSTRAINTS_FIELDS } from '../../../initializers'
-import { exists } from '../misc'
+import { exists, isArray } from '../misc'
+import { truncate } from 'lodash'
 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
+import { isHostValid } from '../servers'
 
 function isActorEndpointsObjectValid (endpointObject: any) {
   return isActivityPubUrlValid(endpointObject.sharedInbox)
@@ -25,7 +27,7 @@ function isActorPublicKeyValid (publicKey: string) {
     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)
 }
@@ -52,6 +54,7 @@ function isActorObjectValid (actor: any) {
     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)
@@ -83,13 +86,48 @@ function isActorRejectActivityValid (activity: any) {
 }
 
 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])
+}
+
+function areValidActorHandles (handles: string[]) {
+  return isArray(handles) && handles.every(h => isValidActorHandle(h))
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  normalizeActor,
+  areValidActorHandles,
   isActorEndpointsObjectValid,
   isActorPublicKeyObjectValid,
   isActorTypeValid,
@@ -103,5 +141,6 @@ export {
   isActorAcceptActivityValid,
   isActorRejectActivityValid,
   isActorDeleteActivityValid,
-  isActorUpdateActivityValid
+  isActorUpdateActivityValid,
+  isValidActorHandle
 }