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