]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Comment.php
Notifications: Switched testing from string to reference levels
[bookstack] / app / Activity / Models / Comment.php
1 <?php
2
3 namespace BookStack\Activity\Models;
4
5 use BookStack\App\Model;
6 use BookStack\Users\Models\HasCreatorAndUpdater;
7 use Illuminate\Database\Eloquent\Factories\HasFactory;
8 use Illuminate\Database\Eloquent\Relations\BelongsTo;
9 use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11 /**
12  * @property int      $id
13  * @property string   $text
14  * @property string   $html
15  * @property int|null $parent_id
16  * @property int      $local_id
17  * @property string   $entity_type
18  * @property int      $entity_id
19  */
20 class Comment extends Model implements Loggable
21 {
22     use HasFactory;
23     use HasCreatorAndUpdater;
24
25     protected $fillable = ['text', 'parent_id'];
26     protected $appends = ['created', 'updated'];
27
28     /**
29      * Get the entity that this comment belongs to.
30      */
31     public function entity(): MorphTo
32     {
33         return $this->morphTo('entity');
34     }
35
36     /**
37      * Get the parent comment this is in reply to (if existing).
38      */
39     public function parent(): BelongsTo
40     {
41         return $this->belongsTo(Comment::class);
42     }
43
44     /**
45      * Check if a comment has been updated since creation.
46      */
47     public function isUpdated(): bool
48     {
49         return $this->updated_at->timestamp > $this->created_at->timestamp;
50     }
51
52     /**
53      * Get created date as a relative diff.
54      */
55     public function getCreatedAttribute(): string
56     {
57         return $this->created_at->diffForHumans();
58     }
59
60     /**
61      * Get updated date as a relative diff.
62      */
63     public function getUpdatedAttribute(): string
64     {
65         return $this->updated_at->diffForHumans();
66     }
67
68     public function logDescriptor(): string
69     {
70         return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->entity_type} (ID: {$this->entity_id})";
71     }
72 }