]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Comment.php
Merge branch 'v25-07' into development
[bookstack] / app / Activity / Models / Comment.php
1 <?php
2
3 namespace BookStack\Activity\Models;
4
5 use BookStack\App\Model;
6 use BookStack\Users\Models\HasCreatorAndUpdater;
7 use BookStack\Users\Models\OwnableInterface;
8 use BookStack\Users\Models\User;
9 use BookStack\Util\HtmlContentFilter;
10 use Illuminate\Database\Eloquent\Factories\HasFactory;
11 use Illuminate\Database\Eloquent\Relations\BelongsTo;
12 use Illuminate\Database\Eloquent\Relations\MorphTo;
13
14 /**
15  * @property int      $id
16  * @property string   $text - Deprecated & now unused (#4821)
17  * @property string   $html
18  * @property int|null $parent_id  - Relates to local_id, not id
19  * @property int      $local_id
20  * @property string   $entity_type
21  * @property int      $entity_id
22  * @property string   $content_ref
23  * @property bool     $archived
24  */
25 class Comment extends Model implements Loggable, OwnableInterface
26 {
27     use HasFactory;
28     use HasCreatorAndUpdater;
29
30     protected $fillable = ['parent_id'];
31
32     /**
33      * Get the entity that this comment belongs to.
34      */
35     public function entity(): MorphTo
36     {
37         return $this->morphTo('entity');
38     }
39
40     /**
41      * Get the parent comment this is in reply to (if existing).
42      * @return BelongsTo<Comment, $this>
43      */
44     public function parent(): BelongsTo
45     {
46         return $this->belongsTo(Comment::class, 'parent_id', 'local_id', 'parent')
47             ->where('entity_type', '=', $this->entity_type)
48             ->where('entity_id', '=', $this->entity_id);
49     }
50
51     /**
52      * Check if a comment has been updated since creation.
53      */
54     public function isUpdated(): bool
55     {
56         return $this->updated_at->timestamp > $this->created_at->timestamp;
57     }
58
59     public function logDescriptor(): string
60     {
61         return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->entity_type} (ID: {$this->entity_id})";
62     }
63
64     public function safeHtml(): string
65     {
66         return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
67     }
68 }