From 7d3bc4a092a445cc69350ab85c465b4f19f8cd7a Mon Sep 17 00:00:00 2001 From: Ian Morland <16573496+imorland@users.noreply.github.com> Date: Sun, 28 Nov 2021 08:52:29 +0000 Subject: [PATCH] fix?: Allow mentions from non-post objects (#79) --- extensions/mentions/src/Formatter/FormatUserMentions.php | 9 +++++---- .../mentions/src/Formatter/UnparseUserMentions.php | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/extensions/mentions/src/Formatter/FormatUserMentions.php b/extensions/mentions/src/Formatter/FormatUserMentions.php index b52324195..fab597af1 100644 --- a/extensions/mentions/src/Formatter/FormatUserMentions.php +++ b/extensions/mentions/src/Formatter/FormatUserMentions.php @@ -10,6 +10,7 @@ namespace Flarum\Mentions\Formatter; use Flarum\Http\SlugManager; +use Flarum\Post\Post; use Flarum\User\User; use s9e\TextFormatter\Renderer; use s9e\TextFormatter\Utils; @@ -43,10 +44,10 @@ class FormatUserMentions */ public function __invoke(Renderer $renderer, $context, string $xml) { - $post = $context; - - return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($post) { - $user = $post->mentionsUsers->find($attributes['id']); + return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) { + $user = (isset($context->getRelations()['mentionsUsers']) || $context instanceof Post) + ? $context->mentionsUsers->find($attributes['id']) + : User::find($attributes['id']); $attributes['deleted'] = false; diff --git a/extensions/mentions/src/Formatter/UnparseUserMentions.php b/extensions/mentions/src/Formatter/UnparseUserMentions.php index 694a2d5ed..4f114636a 100644 --- a/extensions/mentions/src/Formatter/UnparseUserMentions.php +++ b/extensions/mentions/src/Formatter/UnparseUserMentions.php @@ -9,6 +9,7 @@ namespace Flarum\Mentions\Formatter; +use Flarum\Post\Post; use Flarum\User\User; use s9e\TextFormatter\Utils; use Symfony\Contracts\Translation\TranslatorInterface; @@ -49,10 +50,10 @@ class UnparseUserMentions */ protected function updateUserMentionTags($context, string $xml): string { - $post = $context; - - return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($post) { - $user = $post->mentionsUsers->find($attributes['id']); + return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($context) { + $user = (isset($context->getRelations()['mentionsUsers']) || $context instanceof Post) + ? $context->mentionsUsers->find($attributes['id']) + : User::find($attributes['id']); if ($user) { $attributes['displayname'] = $user->display_name;