]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/custom-validators/activitypub/actor.ts
Add links to comment mentions
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / actor.ts
index bf42757c59005d95bce30a7934f2d1e8bdd30c06..df0edc30e5bde124a49f9900674f2123fe8fd0da 100644 (file)
@@ -1,11 +1,6 @@
-import * as Bluebird from 'bluebird'
-import { Response } from 'express'
 import * as validator from 'validator'
 import { CONSTRAINTS_FIELDS } from '../../../initializers'
-import { ActorModel } from '../../../models/activitypub/actor'
-import { isAccountNameValid } from '../accounts'
-import { exists, isUUIDValid } from '../misc'
-import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels'
+import { exists } from '../misc'
 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
 
 function isActorEndpointsObjectValid (endpointObject: any) {
@@ -26,45 +21,40 @@ function isActorPublicKeyValid (publicKey: string) {
   return exists(publicKey) &&
     typeof publicKey === 'string' &&
     publicKey.startsWith('-----BEGIN PUBLIC KEY-----') &&
-    publicKey.endsWith('-----END PUBLIC KEY-----') &&
-    validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY)
-}
-
-function isActorPreferredUsernameValid (preferredUsername: string) {
-  return isAccountNameValid(preferredUsername) || isVideoChannelNameValid(preferredUsername)
+    publicKey.indexOf('-----END PUBLIC KEY-----') !== -1 &&
+    validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY)
 }
 
 const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+')
-function isActorNameValid (name: string) {
-  return exists(name) && validator.matches(name, actorNameRegExp)
+function isActorPreferredUsernameValid (preferredUsername: string) {
+  return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp)
 }
 
 function isActorPrivateKeyValid (privateKey: string) {
   return exists(privateKey) &&
     typeof privateKey === 'string' &&
     privateKey.startsWith('-----BEGIN RSA PRIVATE KEY-----') &&
-    privateKey.endsWith('-----END RSA PRIVATE KEY-----') &&
-    validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY)
-}
-
-function isRemoteActorValid (remoteActor: any) {
-  return isActivityPubUrlValid(remoteActor.id) &&
-    isUUIDValid(remoteActor.uuid) &&
-    isActorTypeValid(remoteActor.type) &&
-    isActivityPubUrlValid(remoteActor.following) &&
-    isActivityPubUrlValid(remoteActor.followers) &&
-    isActivityPubUrlValid(remoteActor.inbox) &&
-    isActivityPubUrlValid(remoteActor.outbox) &&
-    isActorNameValid(remoteActor.name) &&
-    isActorPreferredUsernameValid(remoteActor.preferredUsername) &&
-    isActivityPubUrlValid(remoteActor.url) &&
-    isActorPublicKeyObjectValid(remoteActor.publicKey) &&
-    isActorEndpointsObjectValid(remoteActor.endpoints) &&
-    (!remoteActor.summary || isVideoChannelDescriptionValid(remoteActor.summary)) &&
-    setValidAttributedTo(remoteActor) &&
+    // Sometimes there is a \n at the end, so just assert the string contains the end mark
+    privateKey.indexOf('-----END RSA PRIVATE KEY-----') !== -1 &&
+    validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY)
+}
+
+function isActorObjectValid (actor: any) {
+  return exists(actor) &&
+    isActivityPubUrlValid(actor.id) &&
+    isActorTypeValid(actor.type) &&
+    isActivityPubUrlValid(actor.following) &&
+    isActivityPubUrlValid(actor.followers) &&
+    isActivityPubUrlValid(actor.inbox) &&
+    isActivityPubUrlValid(actor.outbox) &&
+    isActorPreferredUsernameValid(actor.preferredUsername) &&
+    isActivityPubUrlValid(actor.url) &&
+    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
-    (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0)
+    (actor.type === 'Person' || actor.attributedTo.length !== 0)
 }
 
 function isActorFollowingCountValid (value: string) {
@@ -88,38 +78,13 @@ function isActorAcceptActivityValid (activity: any) {
   return isBaseActivityValid(activity, 'Accept')
 }
 
-function isActorIdExist (id: number | string, res: Response) {
-  let promise: Bluebird<ActorModel>
-
-  if (validator.isInt('' + id)) {
-    promise = ActorModel.load(+id)
-  } else { // UUID
-    promise = ActorModel.loadByUUID('' + id)
-  }
-
-  return isActorExist(promise, res)
+function isActorRejectActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Reject')
 }
 
-function isLocalActorNameExist (name: string, res: Response) {
-  const promise = ActorModel.loadLocalByName(name)
-
-  return isActorExist(promise, res)
-}
-
-async function isActorExist (p: Bluebird<ActorModel>, res: Response) {
-  const actor = await p
-
-  if (!actor) {
-    res.status(404)
-      .send({ error: 'Actor not found' })
-      .end()
-
-    return false
-  }
-
-  res.locals.actor = actor
-
-  return true
+function isActorUpdateActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Update') &&
+    isActorObjectValid(activity.object)
 }
 
 // ---------------------------------------------------------------------------
@@ -131,14 +96,12 @@ export {
   isActorPublicKeyValid,
   isActorPreferredUsernameValid,
   isActorPrivateKeyValid,
-  isRemoteActorValid,
+  isActorObjectValid,
   isActorFollowingCountValid,
   isActorFollowersCountValid,
   isActorFollowActivityValid,
   isActorAcceptActivityValid,
+  isActorRejectActivityValid,
   isActorDeleteActivityValid,
-  isActorIdExist,
-  isLocalActorNameExist,
-  isActorNameValid,
-  isActorExist
+  isActorUpdateActivityValid
 }