Skip to content

Commit

Permalink
Merge branch 'main' into update_php_deps
Browse files Browse the repository at this point in the history
  • Loading branch information
melroy89 authored Sep 12, 2024
2 parents 45a7260 + b12adeb commit 0a9e7e1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
20 changes: 15 additions & 5 deletions src/MessageHandler/ActivityPub/Inbox/ChainActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use App\Service\ActivityPub\ApHttpClient;
use App\Service\ActivityPub\Note;
use App\Service\ActivityPub\Page;
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
Expand All @@ -38,7 +39,8 @@ public function __construct(
private readonly MessageBusInterface $bus,
private readonly ApActivityRepository $repository,
private readonly Note $note,
private readonly Page $page
private readonly Page $page,
private readonly SettingsManager $settingsManager,
) {
parent::__construct($this->entityManager);
}
Expand All @@ -64,11 +66,16 @@ public function doWork(MessageInterface $message): void

return;
}
try {
$entity = $this->retrieveObject($object['id']);
} catch (InstanceBannedException) {
$this->logger->info('the instance is banned, url: {url}', ['url' => $object['id']]);

$entity = $this->retrieveObject($object['id']);
return;
}

if (!$entity) {
$this->logger->error('could not retrieve all the dependencies of {o}', ['o' => $object]);
$this->logger->error('could not retrieve all the dependencies of {o}', ['o' => $object['id']]);

return;
}
Expand All @@ -91,6 +98,9 @@ public function doWork(MessageInterface $message): void
*/
private function retrieveObject(string $apUrl): Entry|EntryComment|Post|PostComment|null
{
if ($this->settingsManager->isBannedInstance($apUrl)) {
throw new InstanceBannedException();
}
try {
$object = $this->client->getActivityObject($apUrl);
if (!$object) {
Expand Down Expand Up @@ -141,8 +151,8 @@ private function retrieveObject(string $apUrl): Entry|EntryComment|Post|PostComm
$this->logger->info('one of the used tags is banned, url: {url}', ['url' => $apUrl]);
} catch (EntityNotFoundException $e) {
$this->logger->error('There was an exception while getting {url}: {ex} - {m}. {o}', ['url' => $apUrl, 'ex' => \get_class($e), 'm' => $e->getMessage(), 'o' => $e]);
} catch (InstanceBannedException $e) {
$this->logger->info('the user\'s instance is banned, url: {url}', ['url' => $apUrl]);
} catch (InstanceBannedException) {
$this->logger->info('the instance is banned, url: {url}', ['url' => $apUrl]);
}

return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Service/ActivityPub/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public function __construct(
*/
public function create(array $object, bool $stickyIt = false): Entry
{
$current = $this->repository->findByObjectId($object['id']);
if ($current) {
return $this->entityManager->getRepository($current['type'])->find((int) $current['id']);
}
$actorUrl = $this->activityPubManager->getSingleActorFromAttributedTo($object['attributedTo']);
if ($this->settingsManager->isBannedInstance($actorUrl)) {
throw new InstanceBannedException();
Expand Down
6 changes: 5 additions & 1 deletion src/Service/ActivityPub/SignatureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public function validate(array $request, array $headers, string $body): void

$user = $this->activityPubManager->findActorOrCreate($actorUrl);
if (!empty($user)) {
$pkey = openssl_pkey_get_public($this->client->getActorObject($user->apProfileId)['publicKey']['publicKeyPem']);
$pem = $this->client->getActorObject($user->apProfileId)['publicKey']['publicKeyPem'] ?? null;
if (null === $pem) {
throw new InvalidUserPublicKeyException($user->apProfileId);
}
$pkey = openssl_pkey_get_public($pem);

if (false === $pkey) {
throw new InvalidUserPublicKeyException($user->apProfileId);
Expand Down
10 changes: 7 additions & 3 deletions src/Service/ActivityPubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,14 @@ public function buildHandle(string $id): string
$port = !\is_null(parse_url($id, PHP_URL_PORT))
? ':'.parse_url($id, PHP_URL_PORT)
: '';
$apObj = $this->apHttpClient->getActorObject($id);
if (!isset($apObj['preferredUsername'])) {
throw new \InvalidArgumentException("webfinger from $id does not supply a valid user object");
}

return \sprintf(
'%s@%s%s',
$this->apHttpClient->getActorObject($id)['preferredUsername'],
$apObj['preferredUsername'],
parse_url($id, PHP_URL_HOST),
$port
);
Expand Down Expand Up @@ -551,7 +555,7 @@ public function updateMagazine(string $actorUrl): ?Magazine
$magazine->apInboxUrl = $actor['endpoints']['sharedInbox'] ?? $actor['inbox'];
$magazine->apDomain = parse_url($actor['id'], PHP_URL_HOST);
$magazine->apFollowersUrl = $actor['followers'] ?? null;
$magazine->apAttributedToUrl = \is_string($actor['attributedTo']) ? $actor['attributedTo'] : null;
$magazine->apAttributedToUrl = isset($actor['attributedTo']) && \is_string($actor['attributedTo']) ? $actor['attributedTo'] : null;
$magazine->apFeaturedUrl = $actor['featured'] ?? null;
$magazine->apPreferredUsername = $actor['preferredUsername'] ?? null;
$magazine->apDiscoverable = $actor['discoverable'] ?? true;
Expand Down Expand Up @@ -579,7 +583,7 @@ public function updateMagazine(string $actorUrl): ?Magazine
$this->handleModeratorCollection($actorUrl, $magazine);
} catch (InvalidArgumentException $ignored) {
}
} elseif (\is_array($actor['attributedTo'])) {
} elseif (isset($actor['attributedTo']) && \is_array($actor['attributedTo'])) {
$this->handleModeratorArray($magazine, $this->getActorFromAttributedTo($actor['attributedTo']));
}

Expand Down
2 changes: 1 addition & 1 deletion templates/notifications/_blocks.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
{% elseif notification.report.postComment is defined and notification.report.postComment is not same as null %}
{% set postComment = notification.report.postComment %}
<a href="{{ path('post_single', { 'magazine_name': postComment.post.magazine.name, 'post_id': postComment.post.id, 'slug': postComment.post.slug }) }}#post-comment-{{ postComment.id }}">
{{ post.getShortTitle() }}
{{ postComment.getShortTitle() }}
</a>
{% endif %}
{% endblock %}
Expand Down

0 comments on commit 0a9e7e1

Please sign in to comment.