diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-07-10 12:47:27 +0200 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-08-09 12:23:47 +0200 |
commit | b023f93deb57693eb1d11131adfe388644d77a1e (patch) | |
tree | d020ebf943b3048955bb43fcf84939e0fc09c68a | |
parent | b00831783d97906f0077f49b9e70c5d37b63fb62 (diff) |
More testing now mention put post in the timeline
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
-rw-r--r-- | lib/Entity/Mention.php | 21 | ||||
-rw-r--r-- | lib/Entity/Status.php | 3 | ||||
-rw-r--r-- | lib/Service/AccountFinder.php | 20 | ||||
-rw-r--r-- | lib/Service/Feed/PostDeliveryService.php | 6 | ||||
-rw-r--r-- | lib/Service/ProcessMentionsService.php | 6 | ||||
-rw-r--r-- | tests/Service/Feed/PostDeliveryServiceTest.php | 9 |
6 files changed, 49 insertions, 16 deletions
diff --git a/lib/Entity/Mention.php b/lib/Entity/Mention.php index 1e20feff..11498aaa 100644 --- a/lib/Entity/Mention.php +++ b/lib/Entity/Mention.php @@ -62,31 +62,44 @@ class Mention { return $this->status; } - public function setStatus(?Status $status): void { + public function setStatus(?Status $status): self { $this->status = $status; + return $this; } public function getAccount(): ?Account { return $this->account; } - public function setAccount(?Account $account): void { + public function setAccount(?Account $account): self { $this->account = $account; + return $this; } public function getCreatedAt() { return $this->createdAt; } - public function setCreatedAt($createdAt): void { + public function setCreatedAt($createdAt): self { $this->createdAt = $createdAt; + return $this; } public function getUpdatedAt() { return $this->updatedAt; } - public function setUpdatedAt($updatedAt): void { + public function setUpdatedAt($updatedAt): self { $this->updatedAt = $updatedAt; + return $this; + } + + public function isSilent(): bool { + return $this->silent; + } + + public function setSilent(bool $silent): self { + $this->silent = $silent; + return $this; } } diff --git a/lib/Entity/Status.php b/lib/Entity/Status.php index db1f6bab..f6914e2e 100644 --- a/lib/Entity/Status.php +++ b/lib/Entity/Status.php @@ -347,6 +347,9 @@ class Status { return $this->mentions; } + /** + * @return Collection<Mention> + */ public function getActiveMentions(): Collection { $criteria = Criteria::create(); $criteria->where(Criteria::expr()->eq('silent', false)); diff --git a/lib/Service/AccountFinder.php b/lib/Service/AccountFinder.php index 78e63552..49556808 100644 --- a/lib/Service/AccountFinder.php +++ b/lib/Service/AccountFinder.php @@ -9,8 +9,10 @@ namespace OCA\Social\Service; use Doctrine\Common\Collections\Collection; use OCA\Social\Entity\Account; use OCA\Social\Entity\Follow; +use OCA\Social\Entity\Instance; use OCP\DB\ORM\IEntityManager; use OCP\DB\ORM\IEntityRepository; +use OCP\DB\ORM\NoResultException; use OCP\IRequest; use OCP\IUser; @@ -27,10 +29,20 @@ class AccountFinder { } public function findRemote(string $userName, ?string $domain): ?Account { - return $this->repository->findOneBy([ - 'domain' => $domain, - 'userName' => $userName, - ]); + if ($domain !== null) { + $instance = new Instance(); + $instance->setDomain($domain); + return $this->entityManager->createQuery('SELECT a FROM \OCA\Social\Entity\Account a WHERE a.instance = :instance AND a.userName = :userName') + ->setParameters([ + 'instance' => $instance, + 'userName' => $userName, + ])->getOneOrNullResult(); + } else { + return $this->entityManager->createQuery('SELECT a FROM \OCA\Social\Entity\Account a WHERE a.instance is NULL AND a.userName = :userName') + ->setParameters([ + 'userName' => $userName, + ])->getOneOrNullResult(); + } } public function findLocal(string $userName): ?Account { diff --git a/lib/Service/Feed/PostDeliveryService.php b/lib/Service/Feed/PostDeliveryService.php index 5bdd8d28..ff62e122 100644 --- a/lib/Service/Feed/PostDeliveryService.php +++ b/lib/Service/Feed/PostDeliveryService.php @@ -30,14 +30,14 @@ class PostDeliveryService { } // deliver to mentioned accounts - $localFollowers = $this->accountFinder->getLocalFollowersOf($author); - $status->getActiveMentions()->forAll(function (Mention $mention) use ($status): void{ - if ($mention->getAccount()->isLocal()) { + $status->getActiveMentions()->forAll(function ($mention) use ($status): void{ + if ($mention && $mention->getAccount()->isLocal()) { $this->deliverLocalAccount($status, $mention->getAccount()); } }); // deliver to local followers + $localFollowers = $this->accountFinder->getLocalFollowersOf($author); foreach ($localFollowers as $follower) { $this->deliverLocalAccount($status, $follower->getAccount()); }; diff --git a/lib/Service/ProcessMentionsService.php b/lib/Service/ProcessMentionsService.php index 7f83d965..8eea6f15 100644 --- a/lib/Service/ProcessMentionsService.php +++ b/lib/Service/ProcessMentionsService.php @@ -35,12 +35,12 @@ class ProcessMentionsService { $this->previousMentions = $status->getActiveMentions(); $this->currentMentions = new ArrayCollection(); - if (preg_match('/@(([a-z0-9_]([a-z0-9_\.-]+[a-z0-9_]+)+)(@[[:word:]\.\-]+[[:word:]]+)?)/i', $status->getText(), $matches)) { + if (preg_match_all('/@(([a-z0-9_]([a-z0-9_\.-]+[a-z0-9_]+)+)(@[[:word:]\.\-]+[[:word:]]+)?)/i', $status->getText(), $matches)) { $host = $this->request->getServerHost(); for ($i = 0; $i < count($matches[0]); $i++) { $completeMatch = $matches[0][$i]; $userName = $matches[2][$i]; - $domain = $matches[2][$i] === '' ? '' : substr($matches[2][$i], 1); + $domain = $matches[4][$i] === '' ? '' : substr($matches[4][$i], 1); $isLocal = $domain === '' || $host === $domain; if ($isLocal) { @@ -74,5 +74,7 @@ class ProcessMentionsService { str_replace($completeMatch, $mentionnedAccount->getAccountName(), $status->getText()); } } + + $status->setMentions($this->currentMentions); } } diff --git a/tests/Service/Feed/PostDeliveryServiceTest.php b/tests/Service/Feed/PostDeliveryServiceTest.php index d6b87e2e..2559d062 100644 --- a/tests/Service/Feed/PostDeliveryServiceTest.php +++ b/tests/Service/Feed/PostDeliveryServiceTest.php @@ -12,6 +12,7 @@ use OCA\Social\Entity\Status; use OCA\Social\Service\AccountFinder; use OCA\Social\Service\Feed\PostDeliveryService; use OCA\Social\Service\Feed\FeedManager; +use OCA\Social\Service\ProcessMentionsService; use OCP\DB\ORM\IEntityManager; use OCP\Server; use PHPUnit\Framework\MockObject\MockClass; @@ -50,6 +51,7 @@ class PostDeliveryServiceTest extends TestCase { $em = Server::get(IEntityManager::class); $em->remove($this->account1); $em->remove($this->account2); + $em->remove($this->account3); $em->flush(); parent::tearDown(); @@ -74,13 +76,14 @@ class PostDeliveryServiceTest extends TestCase { $status->setAccount($this->account1); $status->setText('Hello world @user3!'); $status->setLocal(true); - \OCP + $mentionService = \OCP\Server::get(ProcessMentionsService::class); + $mentionService->run($status); $this->feedManager->expects($this->exactly(2)) ->method('addToHome') ->withConsecutive( [$this->account1->getId(), $status], // self - [$this->account2->getId(), $status] // follower - [$this->account3->getId(), $status] // follower + [$this->account2->getId(), $status], // follower + [$this->account3->getId(), $status] // mentioned user ); $this->postDeliveryService->run($status); } |