From af92d6e2928ae29d63f85123208c8165ad7ed099 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 14 Nov 2018 09:38:34 -0100 Subject: [PATCH] cache on key retrieving (when receiving post) Signed-off-by: Maxence Lange --- lib/Exceptions/InvalidResourceException.php | 8 ++++ lib/Service/ActivityPub/PersonService.php | 43 ++++++++++++++++++++- lib/Service/ActivityService.php | 12 ++++-- lib/Service/InstanceService.php | 8 ++-- 4 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 lib/Exceptions/InvalidResourceException.php diff --git a/lib/Exceptions/InvalidResourceException.php b/lib/Exceptions/InvalidResourceException.php new file mode 100644 index 00000000..1aa1d6f4 --- /dev/null +++ b/lib/Exceptions/InvalidResourceException.php @@ -0,0 +1,8 @@ +cacheActorsRequest->getFromId($id); + } catch (CacheActorDoesNotExistException $e) { + $object = $this->instanceService->retrieveObject($id); + $actor = new Person(); + $actor->import($object); + + if ($actor->getType() !== 'Person') { + throw new InvalidResourceException(); + } + + $actor->setPreferredUsername($this->get('preferredUsername', $object, '')); + $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object)); + $actor->setSharedInbox($this->get('endpoints.sharedInbox', $object)); + $actor->setAccount($actor->getPreferredUsername() . '@' . $this->get('_host', $object)); + + $this->save($actor); + } + + return $actor; + } + + /** * @param string $account * @@ -98,6 +135,10 @@ class PersonService implements ICoreService { $actor = new Person(); $actor->import($object); + if ($actor->getType() !== 'Person') { + throw new InvalidResourceException(); + } + $actor->setAccount($account); $actor->setPreferredUsername($this->get('preferredUsername', $object, '')); $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object)); diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 45e8e489..1564065f 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -390,13 +390,17 @@ class ActivityService { /** * @param $keyId * - * @return array + * @return string * @throws RequestException */ - private function retrieveKey($keyId) { - $actor = $this->instanceService->retrieveObject($keyId); + private function retrieveKey($keyId): string { + //check cache here - return $actor['publicKey']['publicKeyPem']; + $actor = $this->personService->getFromId($keyId); + +// $actor = $this->instanceService->retrieveObject($keyId); + + return $actor->getPublicKey(); } diff --git a/lib/Service/InstanceService.php b/lib/Service/InstanceService.php index 6d91e7cb..c691c5ae 100644 --- a/lib/Service/InstanceService.php +++ b/lib/Service/InstanceService.php @@ -109,9 +109,12 @@ class InstanceService { $request = new Request($url['path'], Request::TYPE_GET); $request->setAddress($url['host']); -// $key = $url['fragment']; + $result = $this->curlService->request($request); + if (is_array($result)) { + $result['_host'] = $url['host']; + } - return $this->curlService->request($request); + return $result; } @@ -125,7 +128,6 @@ class InstanceService { foreach ($activity->getInstancePaths() as $instancePath) { $this->addInstances($instancePath, $instances); -// $uriIds[] = $to; } return $instances;