-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDiscussion.php
148 lines (126 loc) · 3.5 KB
/
Discussion.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
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace App\Models;
use App\Contracts\ReactableInterface;
use App\Contracts\ReplyInterface;
use App\Contracts\SpamReportableContract;
use App\Contracts\SubscribeInterface;
use App\Models\Builders\DiscussionQueryBuilder;
use App\Models\Traits\HasAuthor;
use App\Models\Traits\HasLocaleScope;
use App\Models\Traits\HasReplies;
use App\Models\Traits\HasSlug;
use App\Traits\HasSpamReports;
use App\Traits\HasSubscribers;
use App\Traits\HasTags;
use App\Traits\Reactable;
use App\Traits\RecordsActivity;
use Carbon\Carbon;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
/**
* @property-read int $id
* @property string $title
* @property string $slug
* @property string $body
* @property bool $locked
* @property bool $is_pinned
* @property string | null $locale
* @property int $user_id
* @property-read int $count_all_replies_with_child
* @property Carbon $created_at
* @property Carbon $updated_at
* @property User $user
* @property Collection | SpamReport[] $spamReports
*/
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
use HasFactory;
use HasLocaleScope;
use HasReplies;
use HasSlug;
use HasSpamReports;
use HasSubscribers;
use HasTags;
use InteractsWithViews;
use Reactable;
use RecordsActivity;
protected $fillable = [
'title',
'body',
'slug',
'user_id',
'is_pinned',
'locked',
'locale',
];
protected $casts = [
'locked' => 'boolean',
'is_pinned' => 'boolean',
];
protected $appends = [
'count_all_replies_with_child', // @phpstan-ignore-line
];
protected bool $removeViewsOnDelete = true;
public function newEloquentBuilder($query): DiscussionQueryBuilder
{
return new DiscussionQueryBuilder($query);
}
public function getRouteKeyName(): string
{
return 'slug';
}
public function subject(): string
{
return $this->title;
}
public function replyAbleSubject(): string
{
return $this->title;
}
public function getPathUrl(): string
{
return route('discussions.show', $this);
}
public function excerpt(int $limit = 110): string
{
return Str::limit(strip_tags((string) md_to_html($this->body)), $limit);
}
public function isPinned(): bool
{
return $this->is_pinned;
}
public function isLocked(): bool
{
return $this->locked;
}
public function countAllRepliesWithChild(): Attribute
{
return Attribute::make(
get: function () {
$count = $this->replies->count();
foreach ($this->replies()->withCount('allChildReplies')->get() as $reply) {
/** @var Reply $reply */
$count += $reply->all_child_replies_count;
}
return $count;
}
);
}
public function lockedDiscussion(): void
{
$this->update(['locked' => true]);
}
public function delete(): ?bool
{
$this->removeTags();
$this->deleteReplies();
return parent::delete();
}
}