From 349a0fc215ab968557fc32b572e9229b27237e8c Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 18 May 2015 14:35:22 +0930 Subject: [PATCH] Sync mention metadata when a post is edited/deleted --- .../Handlers/PostMentionsMetadataUpdater.php | 23 +++++++++++++++---- .../Handlers/UserMentionsMetadataUpdater.php | 22 +++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/extensions/mentions/src/Handlers/PostMentionsMetadataUpdater.php b/extensions/mentions/src/Handlers/PostMentionsMetadataUpdater.php index 57f0c3554..56ee6e267 100755 --- a/extensions/mentions/src/Handlers/PostMentionsMetadataUpdater.php +++ b/extensions/mentions/src/Handlers/PostMentionsMetadataUpdater.php @@ -3,7 +3,9 @@ use Flarum\Mentions\PostMentionsParser; use Flarum\Mentions\PostMentionedNotification; use Flarum\Core\Events\PostWasPosted; -use Flarum\Core\Models\User; +use Flarum\Core\Events\PostWasRevised; +use Flarum\Core\Events\PostWasDeleted; +use Flarum\Core\Models\Post; use Flarum\Core\Notifications\Notifier; use Illuminate\Contracts\Events\Dispatcher; @@ -22,14 +24,27 @@ class PostMentionsMetadataUpdater public function subscribe(Dispatcher $events) { $events->listen('Flarum\Core\Events\PostWasPosted', __CLASS__.'@whenPostWasPosted'); - - // @todo listen for post edit/delete events and sync mentions as appropriate + $events->listen('Flarum\Core\Events\PostWasRevised', __CLASS__.'@whenPostWasRevised'); + $events->listen('Flarum\Core\Events\PostWasDeleted', __CLASS__.'@whenPostWasDeleted'); } public function whenPostWasPosted(PostWasPosted $event) { - $reply = $event->post; + $this->syncMentions($event->post); + } + public function whenPostWasRevised(PostWasRevised $event) + { + $this->syncMentions($event->post); + } + + public function whenPostWasDeleted(PostWasDeleted $event) + { + $event->post->mentionsPosts()->sync([]); + } + + protected function syncMentions(Post $reply) + { $matches = $this->parser->match($reply->content); $mentioned = $reply->discussion->posts()->with('user')->whereIn('number', array_filter($matches['number']))->get(); diff --git a/extensions/mentions/src/Handlers/UserMentionsMetadataUpdater.php b/extensions/mentions/src/Handlers/UserMentionsMetadataUpdater.php index 829985fd7..e191b560c 100755 --- a/extensions/mentions/src/Handlers/UserMentionsMetadataUpdater.php +++ b/extensions/mentions/src/Handlers/UserMentionsMetadataUpdater.php @@ -3,6 +3,9 @@ use Flarum\Mentions\UserMentionsParser; use Flarum\Mentions\UserMentionedNotification; use Flarum\Core\Events\PostWasPosted; +use Flarum\Core\Events\PostWasRevised; +use Flarum\Core\Events\PostWasDeleted; +use Flarum\Core\Models\Post; use Flarum\Core\Models\User; use Flarum\Core\Notifications\Notifier; use Illuminate\Contracts\Events\Dispatcher; @@ -22,14 +25,27 @@ class UserMentionsMetadataUpdater public function subscribe(Dispatcher $events) { $events->listen('Flarum\Core\Events\PostWasPosted', __CLASS__.'@whenPostWasPosted'); - - // @todo listen for post edit/delete events and sync mentions as appropriate + $events->listen('Flarum\Core\Events\PostWasRevised', __CLASS__.'@whenPostWasRevised'); + $events->listen('Flarum\Core\Events\PostWasDeleted', __CLASS__.'@whenPostWasDeleted'); } public function whenPostWasPosted(PostWasPosted $event) { - $post = $event->post; + $this->syncMentions($event->post); + } + public function whenPostWasRevised(PostWasRevised $event) + { + $this->syncMentions($event->post); + } + + public function whenPostWasDeleted(PostWasDeleted $event) + { + $event->post->mentionsUsers()->sync([]); + } + + public function syncMentions(Post $post) + { $matches = $this->parser->match($post->content); $mentioned = User::whereIn('username', array_filter($matches['username']))->get();