fix?: Allow mentions from non-post objects (#79)

This commit is contained in:
Ian Morland 2021-11-28 08:52:29 +00:00 committed by GitHub
parent e8cb1559cf
commit 7d3bc4a092
2 changed files with 10 additions and 8 deletions

View File

@ -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;

View File

@ -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;