From 3198de4b9b26b5c3fd1e8e44d4a4f70937e3e909 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Tue, 8 Dec 2020 11:41:05 -0500 Subject: [PATCH] Use `Formatter` extender for rendering callbacks --- extensions/mentions/extend.php | 10 ++--- .../src/Formatter/FormatPostMentions.php | 40 +++++++++++++++++++ .../src/Formatter/FormatUserMentions.php | 40 +++++++++++++++++++ .../src/Listener/FormatPostMentions.php | 30 -------------- .../src/Listener/FormatUserMentions.php | 31 -------------- 5 files changed, 84 insertions(+), 67 deletions(-) create mode 100644 extensions/mentions/src/Formatter/FormatPostMentions.php create mode 100644 extensions/mentions/src/Formatter/FormatUserMentions.php delete mode 100755 extensions/mentions/src/Listener/FormatPostMentions.php delete mode 100755 extensions/mentions/src/Listener/FormatUserMentions.php diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index 24667bd49..a66a2f008 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -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), ]; diff --git a/extensions/mentions/src/Formatter/FormatPostMentions.php b/extensions/mentions/src/Formatter/FormatPostMentions.php new file mode 100644 index 000000000..d4b47b8fc --- /dev/null +++ b/extensions/mentions/src/Formatter/FormatPostMentions.php @@ -0,0 +1,40 @@ +mentionsPosts->find($attributes['id']); + if ($post && $post->user) { + $attributes['displayname'] = $post->user->display_name; + } + + return $attributes; + }); + } +} diff --git a/extensions/mentions/src/Formatter/FormatUserMentions.php b/extensions/mentions/src/Formatter/FormatUserMentions.php new file mode 100644 index 000000000..7d160aae5 --- /dev/null +++ b/extensions/mentions/src/Formatter/FormatUserMentions.php @@ -0,0 +1,40 @@ +mentionsUsers->find($attributes['id']); + if ($user) { + $attributes['username'] = $user->username; + $attributes['displayname'] = $user->display_name; + } + + return $attributes; + }); + } +} diff --git a/extensions/mentions/src/Listener/FormatPostMentions.php b/extensions/mentions/src/Listener/FormatPostMentions.php deleted file mode 100755 index 0d1290a31..000000000 --- a/extensions/mentions/src/Listener/FormatPostMentions.php +++ /dev/null @@ -1,30 +0,0 @@ -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; - }); - } -} diff --git a/extensions/mentions/src/Listener/FormatUserMentions.php b/extensions/mentions/src/Listener/FormatUserMentions.php deleted file mode 100755 index 6f738744d..000000000 --- a/extensions/mentions/src/Listener/FormatUserMentions.php +++ /dev/null @@ -1,31 +0,0 @@ -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; - }); - } -}