Skip to content
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
1 change: 1 addition & 0 deletions src/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function findObjectsByAp(string $urlOrHandle): array

foreach ($result['errors'] as $error) {
/** @var \Throwable $error */
$this->logger->warning('SearchController: exception occurred while searching with ActivityPub: {e}', ['e' => $error]);
$this->addFlash('error', $error->getMessage());
}

Expand Down
2 changes: 1 addition & 1 deletion src/Markdown/CommonMark/MentionLinkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function parse(InlineParserContext $ctx): bool
$data->apPublicUrl,
'@'.$username,
'@'.$data->apId,
'@'.$data->apId,
$data->username,
MentionType::RemoteUser,
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Service\ActivityPub\Note;
use App\Service\ActivityPub\Page;
use App\Service\SettingsManager;
use App\Utils\JsonldUtils;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -119,7 +120,7 @@ private function retrieveObject(string $apUrl): Entry|EntryComment|Post|PostComm
}

if (\array_key_exists('inReplyTo', $object) && null !== $object['inReplyTo']) {
$parentUrl = \is_string($object['inReplyTo']) ? $object['inReplyTo'] : $object['inReplyTo']['id'];
$parentUrl = JsonldUtils::getApId($object['inReplyTo']);
$meta = $this->repository->findByObjectId($parentUrl);
if (!$meta) {
$this->retrieveObject($parentUrl);
Expand Down
3 changes: 2 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use App\Service\ActivityPub\Note;
use App\Service\ActivityPub\Page;
use App\Service\MessageManager;
use App\Utils\JsonldUtils;
use App\Utils\UrlUtils;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -134,7 +135,7 @@ public function doWork(MessageInterface $message): void
private function handleChain(array $object, bool $stickyIt, ?array $fullCreatePayload): void
{
if (isset($object['inReplyTo']) && $object['inReplyTo']) {
$existed = $this->repository->findByObjectId($object['inReplyTo']);
$existed = $this->repository->findByObjectId(JsonldUtils::getApId($object['inReplyTo']));
if (!$existed) {
$this->bus->dispatch(new ChainActivityMessage([$object]));

Expand Down
14 changes: 12 additions & 2 deletions src/Service/ActivityPub/ApHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ enum ApRequestType

class ApHttpClient implements ApHttpClientInterface
{
public const TIMEOUT = 8;
public const MAX_DURATION = 15;
public const int TIMEOUT = 8;
public const int MAX_DURATION = 15;

/**
* useful when running a local dev instance as it can be treated as invalid.
*/
private const bool SKIP_HTTP_SIGNATURE = false;

public function __construct(
private readonly string $kbinDomain,
Expand Down Expand Up @@ -699,6 +704,11 @@ private function getInstanceHeaders(string $url, ?array $body = null, string $me
$headers['User-Agent'] = $this->projectInfo->getUserAgent();
$headers = array_merge($headers, $this->getFetchAcceptHeaders($requestType));

if (self::SKIP_HTTP_SIGNATURE) {
unset($headers['Signature']);
unset($headers['Date']);
}

return $headers;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Service/ActivityPub/ApObjectExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Service\ActivityPub;

use App\DTO\ImageDto;
use App\Service\ActivityPubManager;

class ApObjectExtractor
Expand Down Expand Up @@ -42,14 +43,14 @@ public function getMarkdownBody(array $object): ?string
return '';
}

public function getExternalMediaBody(array $object): ?string
public function getExternalMediaBody(array $object, ?ImageDto $consumedImage): ?string
{
$body = null;

if (isset($object['attachment'])) {
$attachments = $object['attachment'];

if ($images = $this->activityPubManager->handleExternalImages($attachments)) {
if ($images = $this->activityPubManager->handleExternalImages($attachments, $consumedImage)) {
$body .= "\n\n".implode(
" \n",
array_map(
Expand Down
8 changes: 5 additions & 3 deletions src/Service/ActivityPub/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use App\Service\PostCommentManager;
use App\Service\PostManager;
use App\Service\SettingsManager;
use App\Utils\JsonldUtils;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
Expand Down Expand Up @@ -79,6 +80,7 @@ public function create(array $object, ?array $root = null, bool $stickyIt = fals

if (isset($object['inReplyTo']) && $replyTo = $object['inReplyTo']) {
// Create post or entry comment
$replyTo = JsonldUtils::getApId($replyTo);
$parentObjectId = $this->repository->findByObjectId($replyTo);
$parent = $this->entityManager->getRepository($parentObjectId['type'])->find((int) $parentObjectId['id']);

Expand Down Expand Up @@ -140,7 +142,7 @@ private function createEntryComment(array $object, ActivityPubActivityInterface
throw new UserDeletedException();
}
$dto->body = $this->objectExtractor->getMarkdownBody($object);
if ($media = $this->objectExtractor->getExternalMediaBody($object)) {
if ($media = $this->objectExtractor->getExternalMediaBody($object, $dto->image)) {
$dto->body .= $media;
}

Expand Down Expand Up @@ -196,7 +198,7 @@ private function createPost(array $object, bool $stickyIt = false): Post
}

$dto->body = $this->objectExtractor->getMarkdownBody($object);
if ($media = $this->objectExtractor->getExternalMediaBody($object)) {
if ($media = $this->objectExtractor->getExternalMediaBody($object, $dto->image)) {
$dto->body .= $media;
}

Expand Down Expand Up @@ -261,7 +263,7 @@ private function createPostComment(array $object, ActivityPubActivityInterface $
throw new UserDeletedException();
}
$dto->body = $this->objectExtractor->getMarkdownBody($object);
if ($media = $this->objectExtractor->getExternalMediaBody($object)) {
if ($media = $this->objectExtractor->getExternalMediaBody($object, $dto->image)) {
$dto->body .= $media;
}

Expand Down
45 changes: 34 additions & 11 deletions src/Service/ActivityPubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,21 +507,31 @@ private function updateUser(string $actorUrl): ?User

public function handleImages(array|string $attachment): ?Image
{
if (\is_string($attachment) && filter_var($attachment, FILTER_VALIDATE_URL)) {
if (\is_string($attachment)) {
if (!filter_var($attachment, FILTER_VALIDATE_URL)) {
return null;
}

$path = parse_url($attachment, PHP_URL_PATH);
$query = parse_url($attachment, PHP_URL_QUERY);
$query = parse_url($attachment, PHP_URL_QUERY) ?? '';
$attachment = [
[
'url' => $attachment,
'type' => 'Image',
],
];

if (null === $path) {
return null;
}

if (str_contains($path, 'jpg') || str_contains($path, 'jpeg') || str_contains($query, 'jpg') || str_contains($query, 'jpeg')) {
$attachment[0]['mediaType'] = 'image/jpeg';
} elseif (str_contains($path, 'png') || str_contains($query, 'png')) {
$attachment[0]['mediaType'] = 'image/png';
}
}

$images = array_filter(
$attachment,
fn ($val) => $this->isImageAttachment($val)
Expand All @@ -531,9 +541,9 @@ public function handleImages(array|string $attachment): ?Image
try {
$imageObject = $images[array_key_first($images)];
if (isset($imageObject['height'])) {
// determine the highest resolution image
// determine the highest resolution image for the same image (equality is ducktyped by comparing the alt text)
foreach ($images as $i) {
if (isset($i['height']) && $i['height'] ?? 0 > $imageObject['height'] ?? 0) {
if (isset($i['height']) && $i['height'] > ($imageObject['height'] ?? 0) && ($i['name'] ?? '') === ($imageObject['name'] ?? '')) {
$imageObject = $i;
}
}
Expand Down Expand Up @@ -965,15 +975,13 @@ public function handleVideos(array $attachment): ?VideoDto
return null;
}

public function handleExternalImages(array $attachment): ?array
public function handleExternalImages(array $attachment, ?\App\DTO\ImageDto $consumedImage): ?array
{
$images = array_filter(
$attachment,
fn ($val) => $this->isImageAttachment($val)
fn ($val) => $this->isImageAttachment($val) && !$this->describeSameImage($val, $consumedImage)
);

array_shift($images);

if (\count($images)) {
return array_map(fn ($val) => (new ImageDto())->create(
$val['url'],
Expand All @@ -989,7 +997,7 @@ public function handleExternalVideos(array $attachment): ?array
{
$videos = array_filter(
$attachment,
fn ($val) => \in_array($val['type'], ['Document', 'Video']) && VideoManager::isVideoUrl($val['url'])
fn ($val) => \in_array($val['type'], ['Document', 'Video']) && (VideoManager::isSupportedVideoMimeType($val['mediaType']) || VideoManager::isVideoUrl($val['url']))
);

if (\count($videos)) {
Expand Down Expand Up @@ -1080,7 +1088,7 @@ public static function getReceivers(array $object): array
private function isImageAttachment(array $object): bool
{
// attachment object has acceptable object type
if (!\in_array($object['type'], ['Document', 'Image'])) {
if (!\in_array($object['type'], ['Document', 'Image']) || !isset($object['url']) || !\is_string($object['url'])) {
return false;
}

Expand All @@ -1091,6 +1099,21 @@ private function isImageAttachment(array $object): bool
|| ImageManager::isImageUrl($object['url']);
}

private function describeSameImage(array $imgA, ?\App\DTO\ImageDto $imgB): bool
{
if (null === $imgB) {
return false;
}

if ($imgA['url'] === $imgB->sourceUrl) {
return true;
}

$altTextA = $imgA['name'] ?? null;
$altTextB = $imgB->altText ?? null;
return null !== $altTextA && '' !== $altTextA && $altTextA === $altTextB;
}

/**
* @param string|array $apObject the object that should be like, so a post of any kind in its AP array representation or a URL
* @param array $fullPayload the full message payload, only used to log it
Expand Down Expand Up @@ -1225,7 +1248,7 @@ public function getActorFromAttributedTo(string|array|null $attributedTo, bool $
} elseif (\is_array($attributedTo)) {
$actors = array_filter($attributedTo, fn ($item) => \is_string($item) || (\is_array($item) && !empty($item['type']) && (!$filterForPerson || 'Person' === $item['type'])));

return array_map(fn ($item) => $item['id'], $actors);
return array_map(fn ($item) => \is_string($item) ? $item : $item['id'], $actors);
}

return [];
Expand Down
7 changes: 3 additions & 4 deletions src/Service/VideoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class VideoManager
{
public const VIDEO_MIMETYPES = ['video/mp4', 'video/webm'];
public const array VIDEO_MIMETYPES = ['video/mp4', 'video/webm', 'video/quicktime', 'video/ogg'];
public const array VIDEO_FILE_EXTENSIONS = ['mp4', 'webm', 'mov', 'ogv', 'ogg'];

public static function isVideoUrl(string $url): bool
{
Expand All @@ -17,9 +18,7 @@ public static function isVideoUrl(string $url): bool
$path = (string) parse_url($url, PHP_URL_PATH);
$urlExt = strtolower(pathinfo($path, PATHINFO_EXTENSION));

$types = array_map(fn ($type) => str_replace('video/', '', $type), self::VIDEO_MIMETYPES);

return \in_array($urlExt, $types, false);
return \in_array($urlExt, self::VIDEO_FILE_EXTENSIONS, false);
}

public static function isSupportedVideoMimeType(?string $mimeType): bool
Expand Down
15 changes: 15 additions & 0 deletions src/Utils/JsonldUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ public static function getArrayValue(array $object, string $key): array

return [$object[$key]];
}

public static function getApId(string|array $value): string
{
if (\is_string($value)) {
return $value;
} elseif (\is_array($value)) {
$value = $value['id'] ?? null;
if (!\is_string($value)) {
throw new \LogicException('JsonldUtils::getApId(): value is array but did not contain valid `id` element');
}
return $value;
} else {
throw new \LogicException('JsonldUtils::getApId(): value is neither a string nor an array');
}
}
}
Loading
Loading