-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
CommentPost.php
140 lines (110 loc) · 3.56 KB
/
CommentPost.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Post;
use Carbon\Carbon;
use Flarum\Formatter\Formatter;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
use Flarum\User\User;
use Psr\Http\Message\ServerRequestInterface;
/**
* A standard comment in a discussion.
*
* @property string $parsed_content
*/
class CommentPost extends Post
{
public static string $type = 'comment';
protected static Formatter $formatter;
protected $observables = ['hidden'];
public static function reply(int $discussionId, string $content, int $userId, ?string $ipAddress, ?User $actor = null): static
{
$post = new static;
$post->created_at = Carbon::now();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
$post->ip_address = $ipAddress;
// Set content last, as the parsing may rely on other post attributes.
$post->setContentAttribute($content, $actor);
$post->raise(new Posted($post));
return $post;
}
public function revise(string $content, User $actor): static
{
if ($this->content !== $content) {
$oldContent = $this->content;
$this->setContentAttribute($content, $actor);
$this->edited_at = Carbon::now();
$this->edited_user_id = $actor->id;
$this->raise(new Revised($this, $actor, $oldContent));
}
return $this;
}
public function hide(?User $actor = null): static
{
if (! $this->hidden_at) {
$this->hidden_at = Carbon::now();
$this->hidden_user_id = $actor?->id;
$this->raise(new Hidden($this));
$this->saved(function (self $model) {
if ($model === $this) {
$model->fireModelEvent('hidden', false);
}
});
}
return $this;
}
public function restore(): static
{
if ($this->hidden_at !== null) {
$this->hidden_at = null;
$this->hidden_user_id = null;
$this->raise(new Restored($this));
$this->saved(function (self $model) {
if ($model === $this) {
$model->fireModelEvent('restored', false);
}
});
}
return $this;
}
public function getContentAttribute(string $value): string
{
return static::$formatter->unparse($value, $this);
}
public function getParsedContentAttribute(): string
{
return $this->attributes['content'];
}
public function setContentAttribute(string $value, ?User $actor = null): void
{
$this->attributes['content'] = $value ? static::$formatter->parse($value, $this, $actor ?? $this->user) : null;
}
public function setParsedContentAttribute(string $value): void
{
$this->attributes['content'] = $value;
}
/**
* Get the content rendered as HTML.
*/
public function formatContent(?ServerRequestInterface $request = null): string
{
return static::$formatter->render($this->attributes['content'], $this, $request);
}
public static function getFormatter(): Formatter
{
return static::$formatter;
}
public static function setFormatter(Formatter $formatter): void
{
static::$formatter = $formatter;
}
}