]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Comment.php
Merge pull request #5917 from BookStackApp/copy_references
[bookstack] / app / Activity / Models / Comment.php
1 <?php
2
3 namespace BookStack\Activity\Models;
4
5 use BookStack\App\Model;
6 use BookStack\Permissions\Models\JointPermission;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Users\Models\HasCreatorAndUpdater;
9 use BookStack\Users\Models\OwnableInterface;
10 use BookStack\Util\HtmlContentFilter;
11 use Illuminate\Database\Eloquent\Builder;
12 use Illuminate\Database\Eloquent\Factories\HasFactory;
13 use Illuminate\Database\Eloquent\Relations\BelongsTo;
14 use Illuminate\Database\Eloquent\Relations\HasMany;
15 use Illuminate\Database\Eloquent\Relations\MorphTo;
16
17 /**
18  * @property int      $id
19  * @property string   $html
20  * @property int|null $parent_id  - Relates to local_id, not id
21  * @property int      $local_id
22  * @property string   $commentable_type
23  * @property int      $commentable_id
24  * @property string   $content_ref
25  * @property bool     $archived
26  */
27 class Comment extends Model implements Loggable, OwnableInterface
28 {
29     use HasFactory;
30     use HasCreatorAndUpdater;
31
32     protected $fillable = ['parent_id'];
33     protected $hidden = ['html'];
34
35     protected $casts = [
36         'archived' => 'boolean',
37     ];
38
39     /**
40      * Get the entity that this comment belongs to.
41      */
42     public function entity(): MorphTo
43     {
44         return $this->morphTo('commentable');
45     }
46
47     /**
48      * Get the parent comment this is in reply to (if existing).
49      * @return BelongsTo<Comment, $this>
50      */
51     public function parent(): BelongsTo
52     {
53         return $this->belongsTo(Comment::class, 'parent_id', 'local_id', 'parent')
54             ->where('commentable_type', '=', $this->commentable_type)
55             ->where('commentable_id', '=', $this->commentable_id);
56     }
57
58     /**
59      * Check if a comment has been updated since creation.
60      */
61     public function isUpdated(): bool
62     {
63         return $this->updated_at->timestamp > $this->created_at->timestamp;
64     }
65
66     public function logDescriptor(): string
67     {
68         return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->commentable_type} (ID: {$this->commentable_id})";
69     }
70
71     public function safeHtml(): string
72     {
73         return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
74     }
75
76     public function jointPermissions(): HasMany
77     {
78         return $this->hasMany(JointPermission::class, 'entity_id', 'commentable_id')
79             ->whereColumn('joint_permissions.entity_type', '=', 'comments.commentable_type');
80     }
81
82     /**
83      * Scope the query to just the comments visible to the user based upon the
84      * user visibility of what has been commented on.
85      */
86     public function scopeVisible(Builder $query): Builder
87     {
88         return app()->make(PermissionApplicator::class)
89             ->restrictEntityRelationQuery($query, 'comments', 'commentable_id', 'commentable_type');
90     }
91 }