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