-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
DiscussionMetadataUpdater.php
executable file
·97 lines (84 loc) · 2.52 KB
/
DiscussionMetadataUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Core\Discussions\Listeners;
use Flarum\Core\Posts\Post;
use Flarum\Events\PostWasPosted;
use Flarum\Events\PostWasDeleted;
use Flarum\Events\PostWasHidden;
use Flarum\Events\PostWasRestored;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionMetadataUpdater
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(PostWasPosted::class, [$this, 'whenPostWasPosted']);
$events->listen(PostWasDeleted::class, [$this, 'whenPostWasDeleted']);
$events->listen(PostWasHidden::class, [$this, 'whenPostWasHidden']);
$events->listen(PostWasRestored::class, [$this, 'whenPostWasRestored']);
}
/**
* @param PostWasPosted $event
*/
public function whenPostWasPosted(PostWasPosted $event)
{
$discussion = $event->post->discussion;
if ($discussion && $discussion->exists) {
$discussion->comments_count++;
$discussion->setLastPost($event->post);
$discussion->refreshParticipantsCount();
$discussion->save();
}
}
/**
* @param \Flarum\Events\PostWasDeleted $event
*/
public function whenPostWasDeleted(PostWasDeleted $event)
{
$this->removePost($event->post);
}
/**
* @param PostWasHidden $event
*/
public function whenPostWasHidden(PostWasHidden $event)
{
$this->removePost($event->post);
}
/**
* @param PostWasRestored $event
*/
public function whenPostWasRestored(PostWasRestored $event)
{
$discussion = $event->post->discussion;
if ($discussion && $discussion->exists) {
$discussion->refreshCommentsCount();
$discussion->refreshParticipantsCount();
$discussion->refreshLastPost();
$discussion->save();
}
}
/**
* @param Post $post
*/
protected function removePost(Post $post)
{
$discussion = $post->discussion;
if ($discussion && $discussion->exists) {
$discussion->refreshCommentsCount();
$discussion->refreshParticipantsCount();
if ($discussion->last_post_id == $post->id) {
$discussion->refreshLastPost();
}
$discussion->save();
}
}
}