Sync mention metadata when a post is edited/deleted

This commit is contained in:
Toby Zerner 2015-05-18 14:35:22 +09:30
parent 547b2b1304
commit 349a0fc215
2 changed files with 38 additions and 7 deletions

View File

@ -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();

View File

@ -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();