cache on key retrieving (when receiving post)

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/24/head
Maxence Lange 2018-11-14 09:38:34 -01:00
rodzic dbf808bfb6
commit af92d6e292
4 zmienionych plików z 63 dodań i 8 usunięć

Wyświetl plik

@ -0,0 +1,8 @@
<?php
namespace OCA\Social\Exceptions;
class InvalidResourceException extends \Exception {
}

Wyświetl plik

@ -35,10 +35,10 @@ use daita\MySmallPhpTools\Traits\TArrayTools;
use Exception;
use OCA\Social\Db\CacheActorsRequest;
use OCA\Social\Exceptions\CacheActorDoesNotExistException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\RequestException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Person;
use OCA\Social\Model\InstancePath;
use OCA\Social\Service\ICoreService;
use OCA\Social\Service\InstanceService;
use OCA\Social\Service\MiscService;
@ -82,6 +82,43 @@ class PersonService implements ICoreService {
}
/**
* @param string $id
*
* @return Person
* @throws RequestException
* @throws Exception
*/
public function getFromId(string $id): Person {
$posAnchor = strpos($id, '#');
if ($posAnchor !== false) {
$id = substr($id, 0, $posAnchor);
}
try {
$actor = $this->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));

Wyświetl plik

@ -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();
}

Wyświetl plik

@ -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;