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