diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php
index 5f52d4798..c4f388bdf 100644
--- a/extensions/mentions/extend.php
+++ b/extensions/mentions/extend.php
@@ -7,15 +7,12 @@
* LICENSE file that was distributed with this source code.
*/
+namespace Flarum\Mentions;
+
use Flarum\Api\Controller;
use Flarum\Api\Serializer\BasicPostSerializer;
use Flarum\Api\Serializer\PostSerializer;
-use Flarum\Event\ConfigurePostsQuery;
use Flarum\Extend;
-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;
use Flarum\Post\Event\Deleted;
@@ -23,6 +20,7 @@ use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
+use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Post;
use Flarum\User\User;
@@ -83,6 +81,8 @@ return [
->listen(Restored::class, Listener\UpdateMentionsMetadataWhenVisible::class)
->listen(Revised::class, Listener\UpdateMentionsMetadataWhenVisible::class)
->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
- ->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
- ->listen(ConfigurePostsQuery::class, Listener\AddFilterByMentions::class),
+ ->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class),
+
+ (new Extend\Filter(PostFilterer::class))
+ ->addFilter(Filter\MentionedFilter::class),
];
diff --git a/extensions/mentions/src/Filter/MentionedFilter.php b/extensions/mentions/src/Filter/MentionedFilter.php
new file mode 100644
index 000000000..de7edd58b
--- /dev/null
+++ b/extensions/mentions/src/Filter/MentionedFilter.php
@@ -0,0 +1,31 @@
+getQuery()
+ ->join('post_mentions_user', 'posts.id', '=', 'post_mentions_user.post_id')
+ ->where('post_mentions_user.mentions_user_id', $negate ? '!=' : '=', $mentionedId);
+ }
+}
diff --git a/extensions/mentions/src/Listener/AddFilterByMentions.php b/extensions/mentions/src/Listener/AddFilterByMentions.php
deleted file mode 100755
index e25106d20..000000000
--- a/extensions/mentions/src/Listener/AddFilterByMentions.php
+++ /dev/null
@@ -1,24 +0,0 @@
-filter, 'mentioned')) {
- $event->query->join('post_mentions_user', 'posts.id', '=', 'post_mentions_user.post_id')
- ->where('post_mentions_user.mentions_user_id', '=', $mentionedId);
- }
- }
-}
diff --git a/extensions/mentions/tests/integration/api/ListPostsTest.php b/extensions/mentions/tests/integration/api/ListPostsTest.php
new file mode 100644
index 000000000..5ee3e7e43
--- /dev/null
+++ b/extensions/mentions/tests/integration/api/ListPostsTest.php
@@ -0,0 +1,88 @@
+extension('flarum-mentions');
+
+ $this->prepareDatabase([
+ 'discussions' => [
+ ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1],
+ ],
+ 'posts' => [
+ ['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 1, 'type' => 'comment', 'content' => 'text
'],
+ ['id' => 2, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 2, 'type' => 'comment', 'content' => 'text
'],
+ ['id' => 3, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 2, 'type' => 'comment', 'content' => 'text
'],
+ ['id' => 4, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 1, 'type' => 'comment', 'content' => 'text
'],
+ ],
+ 'post_mentions_user' => [
+ ['post_id' => 2, 'mentions_user_id' => 1],
+ ['post_id' => 3, 'mentions_user_id' => 1],
+ ['post_id' => 4, 'mentions_user_id' => 2]
+ ],
+ 'users' => [
+ $this->normalUser(),
+ ]
+ ]);
+ }
+
+ /**
+ * @test
+ */
+ public function mentioned_filter_works()
+ {
+ $response = $this->send(
+ $this->request('GET', '/api/posts')
+ ->withQueryParams([
+ 'filter' => ['mentioned' => 1],
+ ])
+ );
+
+ $data = json_decode($response->getBody()->getContents(), true)['data'];
+
+ // Order-independent comparison
+ $ids = Arr::pluck($data, 'id');
+ $this->assertEqualsCanonicalizing(['2', '3'], $ids, 'IDs do not match');
+ }
+
+ /**
+ * @test
+ */
+ public function mentioned_filter_works_negated()
+ {
+ $response = $this->send(
+ $this->request('GET', '/api/posts')
+ ->withQueryParams([
+ 'filter' => ['-mentioned' => 1],
+ ])
+ );
+
+ $data = json_decode($response->getBody()->getContents(), true)['data'];
+
+ // Order-independent comparison
+ $ids = Arr::pluck($data, 'id');
+ $this->assertEqualsCanonicalizing(['4'], $ids, 'IDs do not match');
+ }
+}