From a552b957ba487ea96822b5fe1e33adb5154db9b9 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Fri, 24 Apr 2020 15:55:50 +0200 Subject: [PATCH] Use new Model extender --- extensions/mentions/extend.php | 7 ++++++ .../AddPostMentionedByRelationship.php | 23 ------------------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index 880afe77f..f444ba1c3 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -22,6 +22,8 @@ use Flarum\Post\Event\Hidden; use Flarum\Post\Event\Posted; use Flarum\Post\Event\Restored; use Flarum\Post\Event\Revised; +use Flarum\Post\Post; +use Flarum\User\User; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\View\Factory; @@ -33,6 +35,11 @@ return [ (new Extend\Formatter) ->configure(ConfigureMentions::class), + (new Extend\Model(Post::class)) + ->belongsToMany('mentionedBy', Post::class, 'post_mentions_post', 'mentions_post_id', 'post_id') + ->belongsToMany('mentionsPosts', Post::class, 'post_mentions_post', 'post_id', 'mentions_post_id') + ->belongsToMany('mentionsUsers', User::class, 'post_mentions_user', 'post_id', 'mentions_user_id'), + function (Dispatcher $events, Factory $views) { $events->listen(WillSerializeData::class, Listener\FilterVisiblePosts::class); $events->subscribe(Listener\AddPostMentionedByRelationship::class); diff --git a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php b/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php index 7c0262da4..7ad4cc50a 100755 --- a/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php +++ b/extensions/mentions/src/Listener/AddPostMentionedByRelationship.php @@ -13,9 +13,6 @@ use Flarum\Api\Controller; use Flarum\Api\Event\WillGetData; use Flarum\Api\Serializer\BasicPostSerializer; use Flarum\Event\GetApiRelationship; -use Flarum\Event\GetModelRelationship; -use Flarum\Post\Post; -use Flarum\User\User; use Illuminate\Contracts\Events\Dispatcher; class AddPostMentionedByRelationship @@ -25,30 +22,10 @@ class AddPostMentionedByRelationship */ public function subscribe(Dispatcher $events) { - $events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']); $events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']); $events->listen(WillGetData::class, [$this, 'includeRelationships']); } - /** - * @param GetModelRelationship $event - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|null - */ - public function getModelRelationship(GetModelRelationship $event) - { - if ($event->isRelationship(Post::class, 'mentionedBy')) { - return $event->model->belongsToMany(Post::class, 'post_mentions_post', 'mentions_post_id', 'post_id', null, null, 'mentionedBy'); - } - - if ($event->isRelationship(Post::class, 'mentionsPosts')) { - return $event->model->belongsToMany(Post::class, 'post_mentions_post', 'post_id', 'mentions_post_id', null, null, 'mentionsPosts'); - } - - if ($event->isRelationship(Post::class, 'mentionsUsers')) { - return $event->model->belongsToMany(User::class, 'post_mentions_user', 'post_id', 'mentions_user_id', null, null, 'mentionsUsers'); - } - } - /** * @param GetApiRelationship $event * @return \Tobscure\JsonApi\Relationship|null