1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PolarAdmin\Polar\Repositories; |
4
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use PolarAdmin\Polar\Models\PolarNotification; |
8
|
|
|
use PolarAdmin\Polar\Events\PolarNotificationCreated; |
9
|
|
|
use PolarAdmin\Polar\Contracts\Repositories\PolarNotificationRepository as PolarNotificationRepositoryContract; |
10
|
|
|
|
11
|
|
|
class PolarNotificationRepository implements PolarNotificationRepositoryContract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function recent($user) : Collection |
17
|
|
|
{ |
18
|
|
|
$notifications = PolarNotification::where('user_id', $user->id) |
19
|
|
|
->where('read', 0) |
20
|
|
|
->orderBy('created_at', 'desc') |
21
|
|
|
->limit(8) |
22
|
|
|
->get(); |
23
|
|
|
|
24
|
|
|
if ($notifications->count() < 8) { |
25
|
|
|
$readNotifications = PolarNotification::where('user_id', $user->id) |
26
|
|
|
->where('read', 1) |
27
|
|
|
->orderBy('created_at', 'desc') |
28
|
|
|
->limit(8 - $notifications->count()) |
29
|
|
|
->get(); |
30
|
|
|
|
31
|
|
|
$notifications = $notifications->merge($readNotifications) |
32
|
|
|
->sortByDesc('created_at'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $notifications->values(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function markAsRead($user, int $notification_id) : bool |
42
|
|
|
{ |
43
|
|
|
return PolarNotification::where('user_id', $user->id) |
44
|
|
|
->where('id', '<=', $notification_id) |
45
|
|
|
->update(['read' => 1]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function create(array $data) : PolarNotification |
52
|
|
|
{ |
53
|
|
|
$notification = PolarNotification::create([ |
54
|
|
|
'user_id' => $data['user_id'], |
55
|
|
|
'body' => $data['body'], |
56
|
|
|
'status' => $data['status'], |
57
|
|
|
'from' => array_get($data, 'from'), |
58
|
|
|
'url' => array_get($data, 'url'), |
59
|
|
|
'extra' => array_get($data, 'extra'), |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
event(new PolarNotificationCreated($notification)); |
63
|
|
|
|
64
|
|
|
return $notification; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|