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 $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
28 class Comment extends Model implements Loggable, OwnableInterface
31 use HasCreatorAndUpdater;
33 protected $fillable = ['parent_id'];
36 * Get the entity that this comment belongs to.
38 public function entity(): MorphTo
40 return $this->morphTo('entity');
44 * Get the parent comment this is in reply to (if existing).
45 * @return BelongsTo<Comment, $this>
47 public function parent(): BelongsTo
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);
55 * Check if a comment has been updated since creation.
57 public function isUpdated(): bool
59 return $this->updated_at->timestamp > $this->created_at->timestamp;
62 public function logDescriptor(): string
64 return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->commentable_type} (ID: {$this->commentable_id})";
67 public function safeHtml(): string
69 return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
72 public function jointPermissions(): HasMany
74 return $this->hasMany(JointPermission::class, 'entity_id', 'commentable_id')
75 ->whereColumn('joint_permissions.entity_type', '=', 'comments.commentable_type');
79 * Scope the query to just the comments visible to the user based upon the
80 * user visibility of what has been commented on.
82 public function scopeVisible(Builder $query): Builder
84 return app()->make(PermissionApplicator::class)
85 ->restrictEntityRelationQuery($query, 'comments', 'commentable_id', 'commentable_type');