]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Watch.php
Notifications: Started back-end for watch system
[bookstack] / app / Activity / Models / Watch.php
1 <?php
2
3 namespace BookStack\Activity\Models;
4
5 use BookStack\Permissions\Models\JointPermission;
6 use Carbon\Carbon;
7 use Illuminate\Database\Eloquent\Model;
8 use Illuminate\Database\Eloquent\Relations\HasMany;
9
10 /**
11  * @property int $id
12  * @property int $user_id
13  * @property int $watchable_id
14  * @property string $watchable_type
15  * @property int $level
16  * @property Carbon $created_at
17  * @property Carbon $updated_at
18  */
19 class Watch extends Model
20 {
21     protected static array $levelByOption = [
22         'default' => -1,
23         'ignore' => 0,
24         'new' => 1,
25         'updates' => 2,
26         'comments' => 3,
27     ];
28
29     public function watchable()
30     {
31         $this->morphTo();
32     }
33
34     public function jointPermissions(): HasMany
35     {
36         return $this->hasMany(JointPermission::class, 'entity_id', 'watchable_id')
37             ->whereColumn('favourites.watchable_type', '=', 'joint_permissions.entity_type');
38     }
39
40     /**
41      * @return string[]
42      */
43     public static function getAvailableOptionNames(): array
44     {
45         return array_keys(static::$levelByOption);
46     }
47
48     public static function optionNameToLevel(string $option): int
49     {
50         return static::$levelByOption[$option] ?? -1;
51     }
52 }