From 47fa5d08f30836c5ddeb4a5952effb1f16df6fca Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 4 Jan 2019 18:34:19 -0100 Subject: save hashtags and get from database Signed-off-by: Maxence Lange --- lib/Model/ActivityPub/ACore.php | 2 +- lib/Model/ActivityPub/Item.php | 21 ++++++++++++-- lib/Model/ActivityPub/Object/Note.php | 53 ++++++++++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 7 deletions(-) (limited to 'lib/Model') diff --git a/lib/Model/ActivityPub/ACore.php b/lib/Model/ActivityPub/ACore.php index b11cec61..4f981840 100644 --- a/lib/Model/ActivityPub/ACore.php +++ b/lib/Model/ActivityPub/ACore.php @@ -559,7 +559,7 @@ class ACore extends Item implements JsonSerializable { $this->setPublished($this->validate(self::AS_DATE, 'published', $data, '')); $this->setActorId($this->validate(self::AS_ID, 'actor', $data, '')); $this->setObjectId($this->validate(self::AS_ID, 'object', $data, '')); - $this->setTags($this->validateArray(self::AS_TAGS, 'tags', $data, [])); + $this->setTags($this->validateArray(self::AS_TAGS, 'tag', $data, [])); } diff --git a/lib/Model/ActivityPub/Item.php b/lib/Model/ActivityPub/Item.php index 2cc0beb5..f6f04af1 100644 --- a/lib/Model/ActivityPub/Item.php +++ b/lib/Model/ActivityPub/Item.php @@ -30,6 +30,7 @@ declare(strict_types=1); namespace OCA\Social\Model\ActivityPub; +use daita\MySmallPhpTools\Traits\TArrayTools; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\InstancePath; @@ -37,6 +38,9 @@ use OCA\Social\Model\InstancePath; class Item { + use TArrayTools; + + /** @var string */ private $urlSocial = ''; @@ -502,10 +506,23 @@ class Item { } /** + * @param string $type + * * @return array */ - public function getTags(): array { - return $this->tags; + public function getTags(string $type = ''): array { + if ($type === '') { + return $this->tags; + } + + $result = []; + foreach ($this->tags as $tag) { + if ($this->get('type', $tag, '') === $type) { + $result[] = $tag; + } + } + + return $result; } /** diff --git a/lib/Model/ActivityPub/Object/Note.php b/lib/Model/ActivityPub/Object/Note.php index 0efb3ac4..71501f26 100644 --- a/lib/Model/ActivityPub/Object/Note.php +++ b/lib/Model/ActivityPub/Object/Note.php @@ -49,6 +49,9 @@ class Note extends ACore implements JsonSerializable { /** @var string */ private $content = ''; + /** @var array */ + private $hashtags = []; + /** @var string */ private $attributedTo = ''; @@ -96,6 +99,25 @@ class Note extends ACore implements JsonSerializable { } + /** + * @return array + */ + public function getHashtags(): array { + return $this->hashtags; + } + + /** + * @param array $hashtags + * + * @return Note + */ + public function setHashtags(array $hashtags): Note { + $this->hashtags = $hashtags; + + return $this; + } + + /** * @return string */ @@ -198,6 +220,20 @@ class Note extends ACore implements JsonSerializable { } + /** + * + */ + public function fillHashtags() { + $tags = $this->getTags('Hashtag'); + $hashtags = []; + foreach ($tags as $tag) { + $hashtags[] = $tag['name']; + } + + $this->setHashtags($hashtags); + } + + /** * @param array $data */ @@ -210,6 +246,8 @@ class Note extends ACore implements JsonSerializable { $this->setConversation($this->validate(ACore::AS_ID, 'conversation', $data, '')); $this->setContent($this->get('content', $data, '')); $this->convertPublished(); + + $this->fillHashtags(); } @@ -226,6 +264,7 @@ class Note extends ACore implements JsonSerializable { $this->setPublishedTime($dTime->getTimestamp()); $this->setAttributedTo($this->validate(self::AS_ID, 'attributed_to', $data, '')); $this->setInReplyTo($this->validate(self::AS_ID, 'in_reply_to', $data)); + $this->setHashtags($this->getArray('hashtags', $data, [])); } @@ -235,16 +274,22 @@ class Note extends ACore implements JsonSerializable { public function jsonSerialize(): array { $this->addEntryInt('publishedTime', $this->getPublishedTime()); - return array_merge( + $result = array_merge( parent::jsonSerialize(), [ - 'content' => $this->getContent(), + 'content' => $this->getContent(), 'attributedTo' => $this->getUrlSocial() . $this->getAttributedTo(), - 'inReplyTo' => $this->getInReplyTo(), - 'sensitive' => $this->isSensitive(), + 'inReplyTo' => $this->getInReplyTo(), + 'sensitive' => $this->isSensitive(), 'conversation' => $this->getConversation() ] ); + + if ($this->isCompleteDetails()) { + $result['hashtags'] = $this->getHashtags(); + } + + return $result; } } -- cgit v1.2.3