From d31dcf4e874fffb96d685dc219794f954cb424ed Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 15 Nov 2018 14:26:56 -0100 Subject: [PATCH] storing Actor source Signed-off-by: Maxence Lange --- appinfo/database.xml | 18 ++++++++-- appinfo/info.xml | 2 +- lib/Db/CacheActorsRequest.php | 44 +++++++++++------------ lib/Model/ActivityPub/ACore.php | 33 ++++++++++++++++- lib/Model/ActivityPub/Person.php | 16 ++++----- lib/Service/ActivityPub/PersonService.php | 2 ++ 6 files changed, 77 insertions(+), 38 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 9bb2db8a..1ae367ac 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -207,6 +207,13 @@ 127 + + source + text + 3000 + true + + creation timestamp @@ -236,8 +243,8 @@ local - text - 7 + boolean + false true @@ -318,6 +325,13 @@ true + + source + text + 3000 + true + + creation timestamp diff --git a/appinfo/info.xml b/appinfo/info.xml index 0e284287..25da3d69 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Social 🎉 Nextcloud becomes part of the federated social networks! - 0.0.29 + 0.0.32 agpl Maxence Lange Julius Härtl diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 4cefb756..a693096f 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -61,35 +61,31 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { * @param bool $local * * @return int - * @throws \Exception */ public function save(Person $actor, bool $local = false): int { - try { - $qb = $this->getCacheActorsInsertSql(); - $qb->setValue('id', $qb->createNamedParameter($actor->getId())) - ->setValue('account', $qb->createNamedParameter($actor->getAccount())) - ->setValue('local', $qb->createNamedParameter(($local) ? '1' : '0')) - ->setValue('following', $qb->createNamedParameter($actor->getFollowing())) - ->setValue('followers', $qb->createNamedParameter($actor->getFollowers())) - ->setValue('inbox', $qb->createNamedParameter($actor->getInbox())) - ->setValue('shared_inbox', $qb->createNamedParameter($actor->getSharedInbox())) - ->setValue('outbox', $qb->createNamedParameter($actor->getOutbox())) - ->setValue('featured', $qb->createNamedParameter($actor->getFeatured())) - ->setValue('url', $qb->createNamedParameter($actor->getUrl())) - ->setValue( - 'preferred_username', $qb->createNamedParameter($actor->getPreferredUsername()) - ) - ->setValue('name', $qb->createNamedParameter($actor->getName())) - ->setValue('summary', $qb->createNamedParameter($actor->getSummary())) - ->setValue('public_key', $qb->createNamedParameter($actor->getPublicKey())); + $qb = $this->getCacheActorsInsertSql(); + $qb->setValue('id', $qb->createNamedParameter($actor->getId())) + ->setValue('account', $qb->createNamedParameter($actor->getAccount())) + ->setValue('local', $qb->createNamedParameter(($local) ? '1' : '0')) + ->setValue('following', $qb->createNamedParameter($actor->getFollowing())) + ->setValue('followers', $qb->createNamedParameter($actor->getFollowers())) + ->setValue('inbox', $qb->createNamedParameter($actor->getInbox())) + ->setValue('shared_inbox', $qb->createNamedParameter($actor->getSharedInbox())) + ->setValue('outbox', $qb->createNamedParameter($actor->getOutbox())) + ->setValue('featured', $qb->createNamedParameter($actor->getFeatured())) + ->setValue('url', $qb->createNamedParameter($actor->getUrl())) + ->setValue( + 'preferred_username', $qb->createNamedParameter($actor->getPreferredUsername()) + ) + ->setValue('name', $qb->createNamedParameter($actor->getName())) + ->setValue('summary', $qb->createNamedParameter($actor->getSummary())) + ->setValue('public_key', $qb->createNamedParameter($actor->getPublicKey())) + ->setValue('source', $qb->createNamedParameter($actor->getSource())); - $qb->execute(); + $qb->execute(); - return $qb->getLastInsertId(); - } catch (\Exception $e) { - throw $e; - } + return $qb->getLastInsertId(); } diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index 963a31fa..d9a442c3 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -112,6 +112,10 @@ abstract class ACore implements JsonSerializable { /** @var bool */ private $completeDetails = false; + /** @var string */ + private $source = ''; + + /** * Core constructor. * @@ -155,7 +159,9 @@ abstract class ACore implements JsonSerializable { * @return ACore */ public function setType(string $type): ACore { - $this->type = $type; + if ($type !== '') { + $this->type = $type; + } return $this; } @@ -706,11 +712,31 @@ abstract class ACore implements JsonSerializable { } + /** + * @return string + */ + public function getSource(): string { + return $this->source; + } + + /** + * @param string $source + * + * @return ACore + */ + public function setSource(string $source): ACore { + $this->source = $source; + + return $this; + } + + /** * @param array $data */ public function import(array $data) { $this->setId($this->get('id', $data, '')); + $this->setType($this->get('type', $data, '')); $this->setUrl($this->get('url', $data, '')); $this->setSummary($this->get('summary', $data, '')); $this->setToArray($this->getArray('to', $data, [])); @@ -718,6 +744,7 @@ abstract class ACore implements JsonSerializable { $this->setPublished($this->get('published', $data, '')); $this->setActorId($this->get('actor', $data, '')); $this->setObjectId($this->get('object', $data, '')); + $this->setSource($this->get('source', $data, '')); } @@ -766,6 +793,10 @@ abstract class ACore implements JsonSerializable { $this->addEntry('object', $this->getObjectId()); } + if ($this->isCompleteDetails()) { + $this->addEntry('source', $this->getSource()); + } + return $this->getEntries(); } diff --git a/lib/Model/ActivityPub/Person.php b/lib/Model/ActivityPub/Person.php index 22299ae7..843a1177 100644 --- a/lib/Model/ActivityPub/Person.php +++ b/lib/Model/ActivityPub/Person.php @@ -352,15 +352,11 @@ class Person extends ACore implements JsonSerializable { */ public function setLocal(bool $local): Person { $this->local = $local; - + return $this; } - - - - /** * @param array $data */ @@ -377,12 +373,12 @@ class Person extends ACore implements JsonSerializable { ->setFollowing($this->get('following', $data, '')) ->setSharedInbox($this->get('shared_inbox', $data, '')) ->setFeatured($this->get('featured', $data, '')) - ->setLocal(($this->get('local', $data, '') === '1')) + ->setLocal(($this->getInt('local', $data, 0) === 1)) ->setCreation($this->getInt('creation', $data, 0)); - if ($this->getPreferredUsername() === '') { - $this->setType('Invalid'); - } +// if ($this->getPreferredUsername() === '') { +// $this->setType('Invalid'); +// } } @@ -411,7 +407,7 @@ class Person extends ACore implements JsonSerializable { 'owner' => $this->getId(), 'publicKeyPem' => $this->getPublicKey() ], - 'local' => $this->isLocal() + 'local' => $this->isLocal() ] ); } diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index 665a7211..4fb7d6df 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -92,6 +92,7 @@ class PersonService implements ICoreService { } try { + $actor->setSource(json_encode($actor, JSON_UNESCAPED_SLASHES)); $this->save($actor, true); } catch (Exception $e) { } @@ -125,6 +126,7 @@ class PersonService implements ICoreService { $object = $this->instanceService->retrieveObject($id); $actor = new Person(); $actor->import($object); + $actor->setSource(json_encode($object, JSON_UNESCAPED_SLASHES)); $actor->setPreferredUsername($this->get('preferredUsername', $object, '')); $actor->setPublicKey($this->get('publicKey.publicKeyPem', $object));