From a0e3ae9970825e1249f5d4717c0e080fe4ede1e9 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Wed, 10 Jan 2024 11:10:35 +0100 Subject: [PATCH 1/4] fix(Mentions): allow renderer to be used without context --- .../src/Formatter/FormatPostMentions.php | 19 +++++++++++++++---- .../src/Formatter/UnparsePostMentions.php | 8 +++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/extensions/mentions/src/Formatter/FormatPostMentions.php b/extensions/mentions/src/Formatter/FormatPostMentions.php index 992dca1507..35759f55d2 100644 --- a/extensions/mentions/src/Formatter/FormatPostMentions.php +++ b/extensions/mentions/src/Formatter/FormatPostMentions.php @@ -12,6 +12,7 @@ use Flarum\Discussion\Discussion; use Flarum\Http\SlugManager; use Flarum\Locale\TranslatorInterface; +use Flarum\Post\Post; use Psr\Http\Message\ServerRequestInterface as Request; use s9e\TextFormatter\Renderer; use s9e\TextFormatter\Utils; @@ -24,12 +25,22 @@ public function __construct( ) { } - public function __invoke(Renderer $renderer, mixed $context, ?string $xml, Request $request = null): string + /** + * Configure rendering for post mentions. + * + * @param \s9e\TextFormatter\Renderer $renderer + * @param mixed $context + * @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 0a5499caf6..fd470c10ad 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 Flarum\Locale\TranslatorInterface; use s9e\TextFormatter\Utils; @@ -31,10 +32,11 @@ public function __invoke(mixed $context, string $xml): string */ protected function updatePostMentionTags(mixed $context, string $xml): string { - $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; } From 01283b3fe2d61a543c6ff4c394258ed6741fd49f Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Wed, 10 Jan 2024 11:10:35 +0100 Subject: [PATCH 2/4] test(Mentions): implement test for rendering post without context --- .../integration/api/PostMentionsTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/extensions/mentions/tests/integration/api/PostMentionsTest.php b/extensions/mentions/tests/integration/api/PostMentionsTest.php index 9bb48d8c0c..93ff3dd5f2 100644 --- a/extensions/mentions/tests/integration/api/PostMentionsTest.php +++ b/extensions/mentions/tests/integration/api/PostMentionsTest.php @@ -11,6 +11,8 @@ use Carbon\Carbon; use Flarum\Extend; +use Flarum\Formatter\Formatter; +use Flarum\Post\Post; use Flarum\Post\CommentPost; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; @@ -538,6 +540,40 @@ public function editing_a_post_with_a_mention_of_a_post_with_deleted_author_work $this->assertStringContainsString('PostMention', $response['data']['attributes']['contentHtml']); $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsPosts->find(11)); } + + /** + * @test + */ + public function rendering_post_mention_with_a_post_context_works() + { + /** @var Formatter $formatter */ + $formatter = $this->app()->getContainer()->make(Formatter::class); + + $post = Post::find(4); + $user = User::find(1); + + $xml = $formatter->parse($post->content, $post, $user); + $renderedHtml = $formatter->render($xml, $post); + + $this->assertStringContainsString('TOBY$', $renderedHtml); + } + + /** + * @test + */ + public function rendering_post_mention_without_a_context_works() + { + /** @var Formatter $formatter */ + $formatter = $this->app()->getContainer()->make(Formatter::class); + + $post = Post::find(4); + $user = User::find(1); + + $xml = $formatter->parse($post->content, null, $user); + $renderedHtml = $formatter->render($xml); + + $this->assertStringContainsString('TOBY$', $renderedHtml); + } } class CustomOtherDisplayNameDriver implements DriverInterface From 841e3a4dc06d22561f6931ae0de0c6a3c6f3102b Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:02:46 +0000 Subject: [PATCH 3/4] Update UnparsePostMentions.php --- extensions/mentions/src/Formatter/UnparsePostMentions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/mentions/src/Formatter/UnparsePostMentions.php b/extensions/mentions/src/Formatter/UnparsePostMentions.php index fd470c10ad..2b4b8a062e 100644 --- a/extensions/mentions/src/Formatter/UnparsePostMentions.php +++ b/extensions/mentions/src/Formatter/UnparsePostMentions.php @@ -9,8 +9,8 @@ namespace Flarum\Mentions\Formatter; -use Flarum\Post\Post; use Flarum\Locale\TranslatorInterface; +use Flarum\Post\Post; use s9e\TextFormatter\Utils; class UnparsePostMentions From 589b4edac5c6f6f6181004df947230e8ac19cef2 Mon Sep 17 00:00:00 2001 From: IanM <16573496+imorland@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:03:17 +0000 Subject: [PATCH 4/4] Update PostMentionsTest.php --- extensions/mentions/tests/integration/api/PostMentionsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/mentions/tests/integration/api/PostMentionsTest.php b/extensions/mentions/tests/integration/api/PostMentionsTest.php index 93ff3dd5f2..25af7f255d 100644 --- a/extensions/mentions/tests/integration/api/PostMentionsTest.php +++ b/extensions/mentions/tests/integration/api/PostMentionsTest.php @@ -12,8 +12,8 @@ use Carbon\Carbon; use Flarum\Extend; use Flarum\Formatter\Formatter; -use Flarum\Post\Post; use Flarum\Post\CommentPost; +use Flarum\Post\Post; use Flarum\Testing\integration\RetrievesAuthorizedUsers; use Flarum\Testing\integration\TestCase; use Flarum\User\DisplayName\DriverInterface;