mirror of
https://github.com/flarum/framework.git
synced 2025-02-28 05:15:25 +08:00
Update tag metadata when posts are altered
This commit is contained in:
parent
d91f208b1e
commit
37c0b91614
@ -5,6 +5,11 @@ use Flarum\Tags\Events\DiscussionWasTagged;
|
||||
use Flarum\Core\Events\DiscussionWasStarted;
|
||||
use Flarum\Core\Events\DiscussionWasDeleted;
|
||||
use Flarum\Core\Models\Discussion;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Events\PostWasPosted;
|
||||
use Flarum\Core\Events\PostWasDeleted;
|
||||
use Flarum\Core\Events\PostWasHidden;
|
||||
use Flarum\Core\Events\PostWasRestored;
|
||||
|
||||
class TagMetadataUpdater
|
||||
{
|
||||
@ -13,43 +18,66 @@ class TagMetadataUpdater
|
||||
$events->listen('Flarum\Core\Events\DiscussionWasStarted', __CLASS__.'@whenDiscussionWasStarted');
|
||||
$events->listen('Flarum\Tags\Events\DiscussionWasTagged', __CLASS__.'@whenDiscussionWasTagged');
|
||||
$events->listen('Flarum\Core\Events\DiscussionWasDeleted', __CLASS__.'@whenDiscussionWasDeleted');
|
||||
|
||||
$events->listen('Flarum\Core\Events\PostWasPosted', __CLASS__.'@whenPostWasPosted');
|
||||
$events->listen('Flarum\Core\Events\PostWasDeleted', __CLASS__.'@whenPostWasDeleted');
|
||||
$events->listen('Flarum\Core\Events\PostWasHidden', __CLASS__.'@whenPostWasHidden');
|
||||
$events->listen('Flarum\Core\Events\PostWasRestored', __CLASS__.'@whenPostWasRestored');
|
||||
}
|
||||
|
||||
public function whenDiscussionWasStarted(DiscussionWasStarted $event)
|
||||
{
|
||||
$tags = $event->discussion->tags();
|
||||
|
||||
$this->updateTags($tags, 1, $event->discussion);
|
||||
$this->updateTags($event->discussion, 1);
|
||||
}
|
||||
|
||||
public function whenDiscussionWasTagged(DiscussionWasTagged $event)
|
||||
{
|
||||
$oldTags = Tag::whereIn('id', array_pluck($event->oldTags, 'id'));
|
||||
|
||||
$this->updateTags($oldTags, -1, $event->discussion);
|
||||
$this->updateTags($event->discussion, -1, $oldTags);
|
||||
|
||||
$newTags = $event->discussion->tags();
|
||||
|
||||
$this->updateTags($newTags, 1, $event->discussion);
|
||||
$this->updateTags($event->discussion, 1);
|
||||
}
|
||||
|
||||
public function whenDiscussionWasDeleted(DiscussionWasDeleted $event)
|
||||
{
|
||||
$tags = $event->discussion->tags();
|
||||
$this->updateTags($event->discussion, -1);
|
||||
|
||||
$this->updateTags($tags, -1, $event->discussion);
|
||||
|
||||
$tags->detach();
|
||||
$event->discussion->tags()->detach();
|
||||
}
|
||||
|
||||
protected function updateTags($query, $delta, $discussion)
|
||||
public function whenPostWasPosted(PostWasPosted $event)
|
||||
{
|
||||
foreach ($query->get() as $tag) {
|
||||
$this->updateTags($event->post->discussion);
|
||||
}
|
||||
|
||||
public function whenPostWasDeleted(PostWasDeleted $event)
|
||||
{
|
||||
$this->updateTags($event->post->discussion);
|
||||
}
|
||||
|
||||
public function whenPostWasHidden(PostWasHidden $event)
|
||||
{
|
||||
$this->updateTags($event->post->discussion);
|
||||
}
|
||||
|
||||
public function whenPostWasRestored(PostWasRestored $event)
|
||||
{
|
||||
$this->updateTags($event->post->discussion);
|
||||
}
|
||||
|
||||
protected function updateTags($discussion, $delta = 0, $tags = null)
|
||||
{
|
||||
if (! $tags) {
|
||||
$tags = $discussion->getRelation('tags');
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
$tag->discussions_count += $delta;
|
||||
|
||||
if ($delta > 0 && max($discussion->start_time, $discussion->last_time) > $tag->last_time) {
|
||||
if ($discussion->last_time > $tag->last_time) {
|
||||
$tag->setLastDiscussion($discussion);
|
||||
} elseif ($delta < 0 && $discussion->id == $tag->last_discussion_id) {
|
||||
} elseif ($discussion->id == $tag->last_discussion_id) {
|
||||
$tag->refreshLastDiscussion();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user