From 031b76b94240e09a2fc9185044697e300c3248ee Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 23 Nov 2018 19:52:14 -0100 Subject: [PATCH] adding Icon to Person Signed-off-by: Maxence Lange --- appinfo/database.xml | 7 ++++++ lib/Db/CacheActorsRequest.php | 9 +++++++ lib/Db/CacheActorsRequestBuilder.php | 7 ++++++ lib/Model/ActivityPub/ACore.php | 30 +++++++++++++++++++++++ lib/Service/ActivityPub/PersonService.php | 12 +++++++++ lib/Service/ImportService.php | 11 +++++++++ 6 files changed, 76 insertions(+) diff --git a/appinfo/database.xml b/appinfo/database.xml index 58e8360e..5778099a 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -345,6 +345,13 @@ true + + icon_id + text + 127 + true + + summary text diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index fe722dcc..724abf2d 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -80,6 +80,12 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { ->setValue('public_key', $qb->createNamedParameter($actor->getPublicKey())) ->setValue('source', $qb->createNamedParameter($actor->getSource())); + if ($actor->gotIcon()) { + $iconId = $actor->getIcon() + ->getId(); + $qb->setValue('icon_id', $qb->createNamedParameter($iconId)); + } + $qb->execute(); return $qb->getLastInsertId(); @@ -121,6 +127,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function getFromId(string $id): Person { $qb = $this->getCacheActorsSelectSql(); $this->limitToIdString($qb, $id); + $this->leftJoinCacheDocuments($qb, 'icon_id'); $cursor = $qb->execute(); $data = $cursor->fetch(); @@ -145,6 +152,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function getFromAccount(string $account): Person { $qb = $this->getCacheActorsSelectSql(); $this->limitToAccount($qb, $account); + $this->leftJoinCacheDocuments($qb, 'icon_id'); $cursor = $qb->execute(); $data = $cursor->fetch(); @@ -166,6 +174,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function searchAccounts(string $search): array { $qb = $this->getCacheActorsSelectSql(); $this->searchInAccount($qb, $search); + $this->leftJoinCacheDocuments($qb, 'icon_id'); $accounts = []; $cursor = $qb->execute(); diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php index 4049a274..3d6847d1 100644 --- a/lib/Db/CacheActorsRequestBuilder.php +++ b/lib/Db/CacheActorsRequestBuilder.php @@ -112,6 +112,13 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder { $actor = new Person(); $actor->importFromDatabase($data); + try { + $icon = $this->parseCacheDocumentsLeftJoin($data); + $icon->setParent($actor); + $actor->setIcon($icon); + } catch (InvalidResourceException $e) { + } + return $actor; } diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index 58b40599..a230d40a 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -568,6 +568,36 @@ abstract class ACore implements JsonSerializable { } + /** + * @return bool + */ + public function gotIcon(): bool { + if ($this->icon === null) { + return false; + } + + return true; + } + + /** + * @return Document + */ + public function getIcon(): Document { + return $this->icon; + } + + /** + * @param Document $icon + * + * @return ACore + */ + public function setIcon(Document $icon): ACore { + $this->icon = $icon; + + return $this; + } + + /** * @return bool */ diff --git a/lib/Service/ActivityPub/PersonService.php b/lib/Service/ActivityPub/PersonService.php index a4d4eee1..23bb6bea 100644 --- a/lib/Service/ActivityPub/PersonService.php +++ b/lib/Service/ActivityPub/PersonService.php @@ -216,6 +216,18 @@ class PersonService implements ICoreService { return; } + if ($person->gotIcon()) { + try { + $icon = $this->cacheDocumentsRequest->getFromSource( + $person->getIcon() + ->getUrl() + ); + $person->setIcon($icon); + } catch (CacheDocumentDoesNotExistException $e) { + $this->cacheDocumentsRequest->save($person->getIcon()); + } + } + $this->cacheActorsRequest->save($person); } diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index ae7a96c0..95fae20a 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -133,6 +133,10 @@ class ImportService { $item = new Note($root); break; + case Image::TYPE: + $item = new Image($root); + break; + case Follow::TYPE: $item = new Follow($root); break; @@ -163,6 +167,13 @@ class ImportService { } catch (UnknownItemException $e) { } + try { + /** @var Document $icon */ + $icon = $this->createItem($this->getArray('icon', $data, []), $item); + $item->setIcon($icon); + } catch (UnknownItemException $e) { + } + return $item; }