Use Formatter extender for rendering callbacks

This commit is contained in:
Alexander Skvortsov 2020-12-08 11:41:05 -05:00
parent 256e821289
commit 3198de4b9b
5 changed files with 84 additions and 67 deletions

View File

@ -12,9 +12,9 @@ use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\ConfigurePostsQuery;
use Flarum\Extend;
use Flarum\Formatter\Event\Rendering;
use Flarum\Mentions\ConfigureMentions;
use Flarum\Mentions\FilterVisiblePosts;
use Flarum\Mentions\Formatter;
use Flarum\Mentions\Listener;
use Flarum\Mentions\Notification\PostMentionedBlueprint;
use Flarum\Mentions\Notification\UserMentionedBlueprint;
@ -25,7 +25,6 @@ use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
return [
(new Extend\Frontend('forum'))
@ -77,8 +76,7 @@ return [
->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
->listen(ConfigurePostsQuery::class, Listener\AddFilterByMentions::class),
function (Dispatcher $events) {
$events->listen(Rendering::class, Listener\FormatPostMentions::class);
$events->listen(Rendering::class, Listener\FormatUserMentions::class);
},
(new Extend\Formatter())
->render(Formatter\FormatPostMentions::class)
->render(Formatter\FormatUserMentions::class),
];

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Formatter;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
/**
* Configure rendering for post mentions.
*
* @param s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @param string|null $xml
* @param Psr\Http\Message\ServerRequestInterface $request
* @return void
*/
public function __invoke(Renderer $renderer, $context, $xml, Request $request)
{
$post = $context;
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;
}
return $attributes;
});
}
}

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Formatter;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Renderer;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
/**
* Configure rendering for user mentions.
*
* @param s9e\TextFormatter\Renderer $renderer
* @param mixed $context
* @param string|null $xml
* @param Psr\Http\Message\ServerRequestInterface $request
*/
public function __invoke(Renderer $renderer, $context, $xml, Request $request)
{
$post = $context;
return Utils::replaceAttributes($xml, 'USERMENTION', function ($attributes) use ($post) {
$user = $post->mentionsUsers->find($attributes['id']);
if ($user) {
$attributes['username'] = $user->username;
$attributes['displayname'] = $user->display_name;
}
return $attributes;
});
}
}

View File

@ -1,30 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Rendering;
use s9e\TextFormatter\Utils;
class FormatPostMentions
{
public function handle(Rendering $event)
{
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'POSTMENTION', function ($attributes) use ($post) {
$post = $post->mentionsPosts->find($attributes['id']);
if ($post && $post->user) {
$attributes['displayname'] = $post->user->display_name;
}
return $attributes;
});
}
}

View File

@ -1,31 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Mentions\Listener;
use Flarum\Formatter\Event\Rendering;
use s9e\TextFormatter\Utils;
class FormatUserMentions
{
public function handle(Rendering $event)
{
$post = $event->context;
$event->xml = Utils::replaceAttributes($event->xml, 'USERMENTION', function ($attributes) use ($post) {
$user = $post->mentionsUsers->find($attributes['id']);
if ($user) {
$attributes['username'] = $user->username;
$attributes['displayname'] = $user->display_name;
}
return $attributes;
});
}
}