2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 17:56:55 +01:00
|
|
|
namespace BookStack\Activity\Models;
|
2018-09-25 12:30:50 +01:00
|
|
|
|
2023-05-17 17:56:55 +01:00
|
|
|
use BookStack\App\Model;
|
|
|
|
use BookStack\Users\Models\HasCreatorAndUpdater;
|
2024-01-31 16:20:22 +00:00
|
|
|
use BookStack\Util\HtmlContentFilter;
|
2021-10-30 21:29:59 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2023-08-15 14:39:39 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2020-12-30 18:25:35 +00:00
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
2017-01-13 21:45:48 +05:30
|
|
|
|
2020-05-01 23:24:11 +01:00
|
|
|
/**
|
2021-09-18 21:21:44 +01:00
|
|
|
* @property int $id
|
2024-01-31 16:35:58 +00:00
|
|
|
* @property string $text - Deprecated & now unused (#4821)
|
2021-09-18 21:21:44 +01:00
|
|
|
* @property string $html
|
2023-09-15 13:38:02 +01:00
|
|
|
* @property int|null $parent_id - Relates to local_id, not id
|
2021-09-18 21:21:44 +01:00
|
|
|
* @property int $local_id
|
2023-07-18 15:07:31 +01:00
|
|
|
* @property string $entity_type
|
|
|
|
* @property int $entity_id
|
2023-09-15 13:38:02 +01:00
|
|
|
* @property int $created_by
|
|
|
|
* @property int $updated_by
|
2020-05-01 23:24:11 +01:00
|
|
|
*/
|
2023-07-18 15:07:31 +01:00
|
|
|
class Comment extends Model implements Loggable
|
2017-01-13 21:45:48 +05:30
|
|
|
{
|
2021-10-30 21:29:59 +01:00
|
|
|
use HasFactory;
|
2020-12-30 18:25:35 +00:00
|
|
|
use HasCreatorAndUpdater;
|
|
|
|
|
2024-01-31 16:35:58 +00:00
|
|
|
protected $fillable = ['parent_id'];
|
2017-09-03 16:37:51 +01:00
|
|
|
protected $appends = ['created', 'updated'];
|
|
|
|
|
2017-01-13 21:45:48 +05:30
|
|
|
/**
|
2021-06-26 15:23:15 +00:00
|
|
|
* Get the entity that this comment belongs to.
|
2017-01-13 21:45:48 +05:30
|
|
|
*/
|
2020-12-30 18:25:35 +00:00
|
|
|
public function entity(): MorphTo
|
2017-01-13 21:45:48 +05:30
|
|
|
{
|
|
|
|
return $this->morphTo('entity');
|
|
|
|
}
|
2017-05-16 00:40:14 +05:30
|
|
|
|
2023-08-05 14:19:23 +01:00
|
|
|
/**
|
|
|
|
* Get the parent comment this is in reply to (if existing).
|
|
|
|
*/
|
2023-08-15 14:39:39 +01:00
|
|
|
public function parent(): BelongsTo
|
2023-08-05 14:19:23 +01:00
|
|
|
{
|
2023-09-15 13:38:02 +01:00
|
|
|
return $this->belongsTo(Comment::class, 'parent_id', 'local_id', 'parent')
|
|
|
|
->where('entity_type', '=', $this->entity_type)
|
|
|
|
->where('entity_id', '=', $this->entity_id);
|
2023-08-05 14:19:23 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 21:45:48 +05:30
|
|
|
/**
|
2017-09-03 16:37:51 +01:00
|
|
|
* Check if a comment has been updated since creation.
|
2017-01-13 21:45:48 +05:30
|
|
|
*/
|
2020-12-30 18:25:35 +00:00
|
|
|
public function isUpdated(): bool
|
2017-01-13 21:45:48 +05:30
|
|
|
{
|
2017-09-03 16:37:51 +01:00
|
|
|
return $this->updated_at->timestamp > $this->created_at->timestamp;
|
2017-01-13 21:45:48 +05:30
|
|
|
}
|
2017-05-16 00:40:14 +05:30
|
|
|
|
2017-01-13 21:45:48 +05:30
|
|
|
/**
|
2017-09-03 16:37:51 +01:00
|
|
|
* Get created date as a relative diff.
|
2017-01-13 21:45:48 +05:30
|
|
|
*/
|
2023-08-15 14:39:39 +01:00
|
|
|
public function getCreatedAttribute(): string
|
2017-01-13 21:45:48 +05:30
|
|
|
{
|
2017-09-03 16:37:51 +01:00
|
|
|
return $this->created_at->diffForHumans();
|
2017-01-13 21:45:48 +05:30
|
|
|
}
|
2017-05-16 00:40:14 +05:30
|
|
|
|
2017-09-03 16:37:51 +01:00
|
|
|
/**
|
|
|
|
* Get updated date as a relative diff.
|
2017-06-06 01:46:59 +05:30
|
|
|
*/
|
2023-08-15 14:39:39 +01:00
|
|
|
public function getUpdatedAttribute(): string
|
2017-09-03 16:37:51 +01:00
|
|
|
{
|
|
|
|
return $this->updated_at->diffForHumans();
|
2017-05-30 09:02:47 +05:30
|
|
|
}
|
2023-07-18 15:07:31 +01:00
|
|
|
|
|
|
|
public function logDescriptor(): string
|
|
|
|
{
|
|
|
|
return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->entity_type} (ID: {$this->entity_id})";
|
|
|
|
}
|
2024-01-31 16:20:22 +00:00
|
|
|
|
|
|
|
public function safeHtml(): string
|
|
|
|
{
|
|
|
|
return HtmlContentFilter::removeScriptsFromHtmlString($this->html ?? '');
|
|
|
|
}
|
2017-01-13 21:45:48 +05:30
|
|
|
}
|