From 00166cb2681e76c7e605af7c19d6242226c720b0 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 9 Jan 2024 11:48:25 +0100 Subject: [PATCH] fix(Mentions): allow renderer to be used without context --- .../mentions/src/Formatter/FormatPostMentions.php | 14 ++++++++------ .../mentions/src/Formatter/UnparsePostMentions.php | 8 ++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/extensions/mentions/src/Formatter/FormatPostMentions.php b/extensions/mentions/src/Formatter/FormatPostMentions.php index a13266c74a..48e1ccc37a 100644 --- a/extensions/mentions/src/Formatter/FormatPostMentions.php +++ b/extensions/mentions/src/Formatter/FormatPostMentions.php @@ -11,6 +11,7 @@ use Flarum\Discussion\Discussion; use Flarum\Http\SlugManager; +use Flarum\Post\Post; use Psr\Http\Message\ServerRequestInterface as Request; use s9e\TextFormatter\Renderer; use s9e\TextFormatter\Utils; @@ -39,16 +40,17 @@ public function __construct(TranslatorInterface $translator, SlugManager $slugMa * * @param \s9e\TextFormatter\Renderer $renderer * @param mixed $context - * @param string|null $xml - * @param \Psr\Http\Message\ServerRequestInterface $request - * @return string + * @param string $xml + * @param \Psr\Http\Message\ServerRequestInterface|null $request + * @return string $xml to be rendered */ public function __invoke(Renderer $renderer, $context, $xml, Request $request = null) { - $post = $context; + return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) { + $post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post) + ? $context->mentionsPosts->find($attributes['id']) + : Post::find($attributes['id']); - return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) { - $post = $post->mentionsPosts->find($attributes['id']); if ($post && $post->user) { $attributes['displayname'] = $post->user->display_name; } diff --git a/extensions/mentions/src/Formatter/UnparsePostMentions.php b/extensions/mentions/src/Formatter/UnparsePostMentions.php index 4f8d016148..f223dd638f 100644 --- a/extensions/mentions/src/Formatter/UnparsePostMentions.php +++ b/extensions/mentions/src/Formatter/UnparsePostMentions.php @@ -9,6 +9,7 @@ namespace Flarum\Mentions\Formatter; +use Flarum\Post\Post; use s9e\TextFormatter\Utils; use Symfony\Contracts\Translation\TranslatorInterface; @@ -50,8 +51,11 @@ protected function updatePostMentionTags($context, string $xml): string { $post = $context; - return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($post) { - $post = $post->mentionsPosts->find($attributes['id']); + return Utils::replaceAttributes($xml, 'POSTMENTION', function ($attributes) use ($context) { + $post = (($context && isset($context->getRelations()['mentionsPosts'])) || $context instanceof Post) + ? $context->mentionsPosts->find($attributes['id']) + : Post::find($attributes['id']); + if ($post && $post->user) { $attributes['displayname'] = $post->user->display_name; }