|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Models\Concerns; |
| 4 | + |
| 5 | +use App\Enums\NotificationTarget; |
| 6 | +use App\Models\Notification; |
| 7 | +use App\Services\Notifications\NotificationCollection; |
| 8 | +use Illuminate\Database\Eloquent\Builder; |
| 9 | + |
| 10 | +/** |
| 11 | + * @mixin \App\Models\User |
| 12 | + */ |
| 13 | +trait HasNotifications |
| 14 | +{ |
| 15 | + public const RECENT_NOTIFICATIONS_COUNT = 4; |
| 16 | + |
| 17 | + /** |
| 18 | + * Query builder for all notifications visible to this user (private, project, and global). |
| 19 | + */ |
| 20 | + public function notifications(): Builder |
| 21 | + { |
| 22 | + return Notification::forUser($this); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Fetch recent notifications for this user. |
| 27 | + */ |
| 28 | + public function recentNotifications(int $limit = self::RECENT_NOTIFICATIONS_COUNT): NotificationCollection |
| 29 | + { |
| 30 | + return $this->notifications()->limit($limit)->get(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Private-channel notifications for this user. |
| 35 | + */ |
| 36 | + public function privateNotifications(): Builder |
| 37 | + { |
| 38 | + return Notification::forUser($this)->whereTarget(NotificationTarget::PRIVATE); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Query builder for all notifications visible to this user (private, project, and global). |
| 43 | + */ |
| 44 | + public function projectNotifications(): Builder |
| 45 | + { |
| 46 | + return Notification::forUser($this)->whereTarget(NotificationTarget::PROJECT); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Private-channel notifications for this user. |
| 51 | + */ |
| 52 | + public function globalNotifications(): Builder |
| 53 | + { |
| 54 | + return Notification::forUser($this)->whereTarget(NotificationTarget::GLOBAL); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Query builder for notifications read by this user. |
| 59 | + */ |
| 60 | + public function readNotifications(): Builder |
| 61 | + { |
| 62 | + return $this->notifications()->readBy($this); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Query builder for notifications NOT read by this user. |
| 67 | + */ |
| 68 | + public function unreadNotifications(): Builder |
| 69 | + { |
| 70 | + return $this->notifications()->unreadBy($this); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Pseudo-attributees to make the Builder functions act more like Relations. |
| 75 | + */ |
| 76 | + |
| 77 | + public function getNotificationsAttribute(): NotificationCollection |
| 78 | + { |
| 79 | + return $this->notifications()->get(); |
| 80 | + } |
| 81 | + |
| 82 | + public function getPrivateNotificationsAttribute(): NotificationCollection |
| 83 | + { |
| 84 | + return $this->privateNotifications()->get(); |
| 85 | + } |
| 86 | + |
| 87 | + public function getProjectNotificationsAttribute(): NotificationCollection |
| 88 | + { |
| 89 | + return $this->projectNotifications()->get(); |
| 90 | + } |
| 91 | + |
| 92 | + public function getGlobalNotificationsAttribute(): NotificationCollection |
| 93 | + { |
| 94 | + return $this->globalNotifications()->get(); |
| 95 | + } |
| 96 | + |
| 97 | + public function getReadNotificationsAttribute(): NotificationCollection |
| 98 | + { |
| 99 | + return $this->readNotifications()->get(); |
| 100 | + } |
| 101 | + |
| 102 | + public function getUnreadNotificationsAttribute(): NotificationCollection |
| 103 | + { |
| 104 | + return $this->unreadNotifications()->get(); |
| 105 | + } |
| 106 | +} |
0 commit comments