Skip to content

Commit

Permalink
Implement rules federation, fix markdown federation (#694)
Browse files Browse the repository at this point in the history
Co-authored-by: e-five <[email protected]>
  • Loading branch information
BentiGorlich and e-five256 authored Apr 28, 2024
1 parent f6b661c commit 8b24c69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/Factory/ActivityPub/GroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Factory\ActivityPub;

use App\Entity\Magazine;
use App\Markdown\MarkdownConverter;
use App\Markdown\RenderTarget;
use App\Service\ActivityPub\ContextsProvider;
use App\Service\ImageManager;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -13,13 +15,25 @@ class GroupFactory
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly MarkdownConverter $markdownConverter,
private readonly ContextsProvider $contextProvider,
private readonly ImageManager $imageManager
) {
}

public function create(Magazine $magazine): array
{
$markdownSummary = $magazine->description;

if (!empty($magazine->rules)) {
$markdownSummary .= "\r\n\r\n### Rules\r\n\r\n".$magazine->rules;
}

$summary = $this->markdownConverter->convertToHtml(
$markdownSummary,
[MarkdownConverter::RENDER_TARGET => RenderTarget::ActivityPub],
);

$group = [
'type' => 'Group',
'@context' => $this->contextProvider->referencedContexts(),
Expand Down Expand Up @@ -47,7 +61,11 @@ public function create(Magazine $magazine): array
'id' => $this->getActivityPubId($magazine).'#main-key',
'publicKeyPem' => $magazine->publicKey,
],
'summary' => $magazine->description,
'summary' => $summary,
'source' => $markdownSummary ? [
'content' => $markdownSummary,
'mediaType' => 'text/markdown',
] : null,
'sensitive' => $magazine->isAdult,
'attributedTo' => $this->urlGenerator->generate(
'ap_magazine_moderators',
Expand Down
15 changes: 13 additions & 2 deletions src/Service/ActivityPubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use App\Repository\MagazineRepository;
use App\Repository\UserRepository;
use App\Service\ActivityPub\ApHttpClient;
use App\Service\ActivityPub\ApObjectExtractor;
use App\Service\ActivityPub\Webfinger\WebFinger;
use App\Service\ActivityPub\Webfinger\WebFingerFactory;
use Doctrine\Common\Collections\Criteria;
Expand Down Expand Up @@ -441,8 +442,7 @@ public function updateMagazine(string $actorUrl): ?Magazine

if (isset($actor['endpoints']['sharedInbox']) || isset($actor['inbox'])) {
if (isset($actor['summary'])) {
$converter = new HtmlConverter(['strip_tags' => true]);
$magazine->description = stripslashes($converter->convert($actor['summary']));
$magazine->description = $this->extractMarkdownSummary($actor);
}

if (isset($actor['icon'])) {
Expand Down Expand Up @@ -789,4 +789,15 @@ public function getEntityObject(string|array $apObject, array $fullPayload, call

return $this->entityManager->getRepository($activity['type'])->find((int) $activity['id']);
}

public function extractMarkdownSummary(array $apObject): ?string
{
if (isset($apObject['source']) && isset($apObject['source']['mediaType']) && isset($apObject['source']['content']) && ApObjectExtractor::MARKDOWN_TYPE === $apObject['source']['mediaType']) {
return $apObject['source']['content'];
} else {
$converter = new HtmlConverter(['strip_tags' => true]);

return stripslashes($converter->convert($apObject['summary']));
}
}
}

0 comments on commit 8b24c69

Please sign in to comment.