Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for liking a hashtag matched post #1364

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/MessageHandler/ActivityPub/Outbox/AnnounceLikeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ public function doWork(MessageInterface $message): void
$likeActivity
);

$inboxes = array_filter(array_unique(array_merge(
$this->magazineRepository->findAudience($object->magazine),
$this->userRepository->findAudience($user),
[$object->user->apInboxUrl]
)));
// send the announcement only to the subscribers of the magazine
$inboxes = array_filter(
$this->magazineRepository->findAudience($object->magazine)
);
$this->deliverManager->deliver($inboxes, $activity);
}
}
61 changes: 55 additions & 6 deletions tests/Unit/ActivityPub/TagMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace App\Tests\Unit\ActivityPub;

use App\ActivityPub\JsonRd;
use App\Entity\Magazine;
use App\Entity\User;
use App\Event\ActivityPub\WebfingerResponseEvent;
use App\Message\ActivityPub\Inbox\CreateMessage;
use App\Message\ActivityPub\Inbox\LikeMessage;
use App\Service\ActivityPub\Webfinger\WebFingerFactory;
use App\Tests\WebTestCase;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -26,8 +29,10 @@ class TagMatchTest extends WebTestCase
'mbin10.tld',
];

/** @var User[] */
private array $remoteUsers = [];

/** @var Magazine[] */
private array $remoteMagazines = [];

/**
Expand Down Expand Up @@ -123,19 +128,63 @@ public function testMatching(): void
self::assertEquals(\sizeof($this->domains), \sizeof(array_filter($this->remoteMagazines)));
self::assertEquals(\sizeof($this->domains), \sizeof(array_filter($this->remoteUsers)));

// pull in the 10 prepared remote entries
$this->pullInRemoteEntries();
$this->pullInMastodonPost();

$postedObjects = $this->testingApHttpClient->getPostedObjects();
$postedAnnounces = array_filter($postedObjects, fn ($item) => 'Announce' === $item['payload']['type']);
$targetInboxes = array_map(fn ($item) => parse_url($item['inboxUrl'], PHP_URL_HOST), $postedAnnounces);
sort($targetInboxes);
self::assertArrayIsEqualToArrayIgnoringListOfKeys($this->domains, $targetInboxes, []);
}

public function testMatchingLikeAnnouncing(): void
{
self::assertEquals(\sizeof($this->domains), \sizeof(array_filter($this->remoteMagazines)));
self::assertEquals(\sizeof($this->domains), \sizeof(array_filter($this->remoteUsers)));

$this->pullInRemoteEntries();
$this->pullInMastodonPost();

$mastodonPost = $this->postRepository->findOneBy(['apId' => $this->mastodonPost['id']]);
$user = $this->getUserByUsername('user');
$this->favouriteManager->toggle($user, $mastodonPost);

$postedObjects = $this->testingApHttpClient->getPostedObjects();
$postedLikes = array_filter($postedObjects, fn ($item) => 'Like' === $item['payload']['type']);
$targetInboxes2 = array_map(fn ($item) => parse_url($item['inboxUrl'], PHP_URL_HOST), $postedLikes);
sort($targetInboxes2);

// the pure like activity is expected to be sent to the author of the post
$expectedInboxes = [...$this->domains, parse_url($mastodonPost->user->apInboxUrl, PHP_URL_HOST)];
sort($expectedInboxes);
self::assertArrayIsEqualToArrayIgnoringListOfKeys($expectedInboxes, $targetInboxes2, []);

// dispatch a remote like message, so we trigger the announcement of it
$this->bus->dispatch(new LikeMessage($this->likeWrapper->build($this->remoteUsers[0]->apProfileId, $this->mastodonPost)));

$postedObjects = $this->testingApHttpClient->getPostedObjects();
$postedLikeAnnounces = array_filter($postedObjects, fn ($item) => 'Announce' === $item['payload']['type'] && 'Like' === $item['payload']['object']['type']);
$targetInboxes3 = array_map(fn ($item) => parse_url($item['inboxUrl'], PHP_URL_HOST), $postedLikeAnnounces);
sort($targetInboxes3);

// the announcement of the like is expected to be delivered only to the subscribers of the magazine,
// because we expect the pure like activity to already be sent to the author of the post by the remote server
self::assertArrayIsEqualToArrayIgnoringListOfKeys($this->domains, $targetInboxes3, []);
}

private function pullInRemoteEntries(): void
{
foreach (array_filter($this->testingApHttpClient->activityObjects, fn ($item) => 'Page' === $item['type']) as $apObject) {
$this->bus->dispatch(new CreateMessage($apObject));
$entry = $this->entryRepository->findOneBy(['apId' => $apObject['id']]);
self::assertNotNull($entry);
}
}

private function pullInMastodonPost(): void
{
$this->bus->dispatch(new CreateMessage($this->mastodonPost));
$postedObjects = $this->testingApHttpClient->getPostedObjects();
$postedAnnounces = array_filter($postedObjects, fn ($item) => 'Announce' === $item['payload']['type']);
$targetInboxes = array_map(fn ($item) => parse_url($item['inboxUrl'], PHP_URL_HOST), $postedAnnounces);
sort($targetInboxes);
self::assertArrayIsEqualToArrayIgnoringListOfKeys($this->domains, $targetInboxes, []);
}

private array $mastodonUser = [
Expand Down
3 changes: 3 additions & 0 deletions tests/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Repository\UserRepository;
use App\Service\ActivityPub\ApHttpClientInterface;
use App\Service\ActivityPub\Wrapper\CreateWrapper;
use App\Service\ActivityPub\Wrapper\LikeWrapper;
use App\Service\ActivityPubManager;
use App\Service\BadgeManager;
use App\Service\DomainManager;
Expand Down Expand Up @@ -130,6 +131,7 @@ abstract class WebTestCase extends BaseWebTestCase
protected TestingApHttpClient $testingApHttpClient;

protected CreateWrapper $createWrapper;
protected LikeWrapper $likeWrapper;

protected UrlGeneratorInterface $urlGenerator;
protected TranslatorInterface $translator;
Expand Down Expand Up @@ -194,6 +196,7 @@ public function setUp(): void
$this->pageFactory = $this->getService(EntryPageFactory::class);

$this->createWrapper = $this->getService(CreateWrapper::class);
$this->likeWrapper = $this->getService(LikeWrapper::class);

$this->urlGenerator = $this->getService(UrlGeneratorInterface::class);
$this->translator = $this->getService(TranslatorInterface::class);
Expand Down
Loading