]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Comment.php
API: Started building comments API endpoints
[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   $text - Deprecated & now unused (#4821)
20  * @property string   $html
21  * @property int|null $parent_id  - Relates to local_id, not id
22  * @property int      $local_id
23  * @property string   $commentable_type
24  * @property int      $commentable_id
25  * @property string   $content_ref
26  * @property bool     $archived
27  */
28 class Comment extends Model implements Loggable, OwnableInterface
29 {
30     use HasFactory;
31     use HasCreatorAndUpdater;
32
33     protected $fillable = ['parent_id'];
34
35     /**
36      * Get the entity that this comment belongs to.
37      */
38     public function entity(): MorphTo
39     {
40         return $this->morphTo('entity');
41     }
42
43     /**
44      * Get the parent comment this is in reply to (if existing).
45      * @return BelongsTo<Comment, $this>
46      */
47     public function parent(): BelongsTo
48     {
49         return $this->belongsTo(Comment::class, 'parent_id', 'local_id', 'parent')
50             ->where('commentable_type', '=', $this->commentable_type)
51             ->where('commentable_id', '=', $this->commentable_id);
52     }
53
54     /**
55      * Check if a comment has been updated since creation.
56      */
57     public function isUpdated(): bool
58     {
59         return $this->updated_at->timestamp > $this->created_at->timestamp;
60     }
61
62     public function logDescriptor(): string
63     {
64         return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->commentable_type} (ID: {$this->commentable_id})";
65     }
66
67     public function safeHtml(): string
68     {
69         return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
70     }
71
72     public function jointPermissions(): HasMany
73     {
74         return $this->hasMany(JointPermission::class, 'entity_id', 'commentable_id')
75             ->whereColumn('joint_permissions.entity_type', '=', 'comments.commentable_type');
76     }
77
78     /**
79      * Scope the query to just the comments visible to the user based upon the
80      * user visibility of what has been commented on.
81      */
82     public function scopeVisible(Builder $query): Builder
83     {
84         return app()->make(PermissionApplicator::class)
85             ->restrictEntityRelationQuery($query, 'comments', 'commentable_id', 'commentable_type');
86     }
87 }