From 0da568f56bc26a0e48021fa265de715c274b66cc Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 22 Aug 2020 01:41:38 -0100 Subject: [PATCH] fix mention based on content Signed-off-by: Maxence Lange --- lib/Model/ActivityPub/Item.php | 4 +++- lib/Model/ActivityPub/Object/Note.php | 2 +- lib/Model/Post.php | 11 +++++++++- lib/Service/PostService.php | 29 ++++++++++++++++++++++----- lib/Service/StreamService.php | 4 ++-- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/lib/Model/ActivityPub/Item.php b/lib/Model/ActivityPub/Item.php index 94e7b067..4f9578e7 100644 --- a/lib/Model/ActivityPub/Item.php +++ b/lib/Model/ActivityPub/Item.php @@ -411,7 +411,9 @@ class Item { * @return Item */ public function addToArray(string $to): Item { - $this->toArray[] = $to; + if (!in_array($to, $this->toArray)) { + $this->toArray[] = $to; + } return $this; } diff --git a/lib/Model/ActivityPub/Object/Note.php b/lib/Model/ActivityPub/Object/Note.php index 0cbc5238..7026e36a 100644 --- a/lib/Model/ActivityPub/Object/Note.php +++ b/lib/Model/ActivityPub/Object/Note.php @@ -113,7 +113,7 @@ class Note extends Stream implements JsonSerializable { if (substr($hashtag, 0, 1) === '#') { $hashtag = substr($hashtag, 1); } - $hashtags[] = $hashtag; + $hashtags[] = trim($hashtag); } $this->setHashtags($hashtags); diff --git a/lib/Model/Post.php b/lib/Model/Post.php index 55b02453..0fb87f96 100644 --- a/lib/Model/Post.php +++ b/lib/Model/Post.php @@ -94,7 +94,8 @@ class Post implements JsonSerializable { * @return Post */ public function addTo(string $to): Post { - if ($to !== '') { + $to = trim($to); + if ($to !== '' && !in_array($to, $this->to)) { $this->to[] = $to; } @@ -175,6 +176,14 @@ class Post implements JsonSerializable { return $this; } + public function addHashtag(string $hashtag): Post { + $hashtag = trim($hashtag); + if ($hashtag !== '' && !in_array($hashtag, $this->hashtags)) { + $this->hashtags[] = $hashtag; + } + + return $this; + } /** * @return string[] diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index abd1e01d..e78e777b 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -31,6 +31,11 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\CacheContentMimeTypeException; @@ -38,11 +43,6 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use daita\MySmallPhpTools\Exceptions\RequestContentException; -use daita\MySmallPhpTools\Exceptions\RequestNetworkException; -use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; -use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; -use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; @@ -119,6 +119,8 @@ class PostService { * @throws UnauthorizedFediverseException */ public function createPost(Post $post, &$token = ''): ACore { + $this->fixRecipientAndHashtags($post); + $note = new Note(); $actor = $post->getActor(); $this->streamService->assignItem($note, $actor, $post->getType()); @@ -192,5 +194,22 @@ class PostService { } + /** + * @param Post $post + */ + public function fixRecipientAndHashtags(Post $post) { + preg_match_all('/(?!\b)@([^\s]+)/', $post->getContent(), $matchesTo); + preg_match_all('/(?!\b)#([^\s]+)/', $post->getContent(), $matchesHash); + + foreach ($matchesTo[1] as $to) { + $post->addTo($to); + } + + foreach ($matchesHash[1] as $hash) { + $post->addHashtag($hash); + } + + } + } diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index 8c77e589..d3e0da89 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -242,7 +242,7 @@ class StreamService { } try { - $actor = $this->cacheActorService->getFromAccount($account); + $actor = $this->cacheActorService->getFromAccount($account, true); } catch (Exception $e) { return; } @@ -253,7 +253,7 @@ class StreamService { if ($type === Stream::TYPE_DIRECT) { $instancePath->setPriority(InstancePath::PRIORITY_HIGH); $stream->addToArray($actor->getId()); - $stream->setFilterDuplicate(true); + $stream->setFilterDuplicate(true); // TODO: really needed ? } else { $stream->addCc($actor->getId()); }