|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Repositories\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Coyote\Notification; |
|
7
|
|
|
use Coyote\Notification\Setting; |
|
8
|
|
|
use Coyote\Repositories\Contracts\NotificationRepositoryInterface; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @package Coyote\Repositories\Eloquent |
|
13
|
|
|
*/ |
|
14
|
|
|
class NotificationRepository extends Repository implements NotificationRepositoryInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
public function model() |
|
20
|
|
|
{ |
|
21
|
|
|
return Notification::class; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $userId |
|
26
|
|
|
* @param int $perPage |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
public function lengthAwarePaginate($userId, $perPage = 20) |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->prepare($userId)->paginate($perPage); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param int $userId |
|
36
|
|
|
* @param int $limit |
|
37
|
|
|
* @param int $offset |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function takeForUser($userId, $limit = 10, $offset = 0) |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->prepare($userId)->take($limit)->skip($offset)->get(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Mark notifications as read |
|
47
|
|
|
* |
|
48
|
|
|
* @use Coyote\Http\Controllers\User\NotificationsController |
|
49
|
|
|
* @param array $id |
|
50
|
|
|
*/ |
|
51
|
|
|
public function markAsRead(array $id) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->model->whereIn('id', $id)->update(['read_at' => Carbon::now()]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Find notification by url and mark it as read |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $userId |
|
60
|
|
|
* @param mixed $model |
|
61
|
|
|
*/ |
|
62
|
|
|
public function markAsReadByModel($userId, $model) |
|
63
|
|
|
{ |
|
64
|
|
|
$this |
|
65
|
|
|
->model |
|
66
|
|
|
->where('user_id', $userId) |
|
67
|
|
|
->whereNull('read_at') |
|
68
|
|
|
->where('content_id', $model->id) |
|
69
|
|
|
->where('content_type', class_basename($model)) |
|
70
|
|
|
->update(['read_at' => Carbon::now()]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Build query for repository |
|
75
|
|
|
* |
|
76
|
|
|
* @param int $userId |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
private function prepare($userId) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this |
|
82
|
|
|
->model |
|
83
|
|
|
->select(['notifications.*', 'notification_types.headline']) |
|
84
|
|
|
->where('user_id', $userId) |
|
85
|
|
|
->with(['senders' => function (HasMany $sql) { |
|
86
|
|
|
$sql |
|
87
|
|
|
->select([ |
|
88
|
|
|
'notification_id', |
|
89
|
|
|
'user_id', |
|
90
|
|
|
$this->raw('COALESCE(users.name, notification_senders.name) AS name'), |
|
91
|
|
|
'photo', |
|
92
|
|
|
'is_blocked', |
|
93
|
|
|
$this->raw('users.deleted_at IS NULL AS is_active') |
|
94
|
|
|
]) |
|
95
|
|
|
->orderBy('notification_senders.id'); |
|
96
|
|
|
}]) |
|
97
|
|
|
->join('notification_types', 'notification_types.id', '=', 'type_id') |
|
98
|
|
|
->orderBy('notifications.created_at', 'DESC'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets public notification types |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
public function notificationTypes() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this |
|
109
|
|
|
->app |
|
110
|
|
|
->make(Notification\Type::class) |
|
111
|
|
|
->select() |
|
112
|
|
|
->where('is_public', 1) |
|
113
|
|
|
->orderBy('notification_types.id') |
|
114
|
|
|
->get(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param int $userId |
|
119
|
|
|
* @param array $data |
|
120
|
|
|
*/ |
|
121
|
|
|
public function updateSettings($userId, array $data) |
|
122
|
|
|
{ |
|
123
|
|
|
$model = $this->app->make(Setting::class); |
|
124
|
|
|
|
|
125
|
|
|
foreach ($data as $id => $value) { |
|
126
|
|
|
$model->where('user_id', $userId)->where('id', $id)->update(['is_enabled' => $value]); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|