3 namespace BookStack\Activity\Models;
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;
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
27 class Comment extends Model implements Loggable, OwnableInterface
30 use HasCreatorAndUpdater;
32 protected $fillable = ['parent_id'];
33 protected $hidden = ['html'];
36 'archived' => 'boolean',
40 * Get the entity that this comment belongs to.
42 public function entity(): MorphTo
44 return $this->morphTo('commentable');
48 * Get the parent comment this is in reply to (if existing).
49 * @return BelongsTo<Comment, $this>
51 public function parent(): BelongsTo
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);
59 * Check if a comment has been updated since creation.
61 public function isUpdated(): bool
63 return $this->updated_at->timestamp > $this->created_at->timestamp;
66 public function logDescriptor(): string
68 return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->commentable_type} (ID: {$this->commentable_id})";
71 public function safeHtml(): string
73 return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
76 public function jointPermissions(): HasMany
78 return $this->hasMany(JointPermission::class, 'entity_id', 'commentable_id')
79 ->whereColumn('joint_permissions.entity_type', '=', 'comments.commentable_type');
83 * Scope the query to just the comments visible to the user based upon the
84 * user visibility of what has been commented on.
86 public function scopeVisible(Builder $query): Builder
88 return app()->make(PermissionApplicator::class)
89 ->restrictEntityRelationQuery($query, 'comments', 'commentable_id', 'commentable_type');