PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.3.2
Fluent Support – Helpdesk & Customer Support Ticket System v2.3.2
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Services / Notifications / NotificationService.php

NotificationService.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.3.2, at app/Services/Notifications/NotificationService.php

153 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\Notifications;
4
5 use FluentSupport\App\Models\Notification;
6 use FluentSupport\App\Models\NotificationUser;
7 use FluentSupport\Framework\Support\Arr;
8 use InvalidArgumentException;
9
10 class NotificationService
11 {
12 public function createNotification(array $data, array $recipientPersonIds = [])
13 {
14 $settings = new NotificationSettings();
15 $recipientPersonIds = $this->normalizeRecipientPersonIds($recipientPersonIds);
16
17 if (!$recipientPersonIds) {
18 return null;
19 }
20
21 if (!$settings->canUseNotificationTables(true)) {
22 return null;
23 }
24
25 $eventType = NotificationEventMap::normalizeEventType(Arr::get($data, 'event_type'));
26 $expectedCategory = NotificationEventMap::resolveCategory($eventType);
27 $category = Arr::get($data, 'category');
28
29 if ($category && $category !== $expectedCategory) {
30 throw new InvalidArgumentException('Notification category must match the event_type.');
31 }
32
33 $category = $expectedCategory;
34
35 if (!$settings->isEnabled() || !$settings->isEventEnabled($eventType)) {
36 return null;
37 }
38
39 $actorId = (int) Arr::get($data, 'actor_id');
40 if (!$settings->allowsSelfNotifications() && $actorId) {
41 $recipientPersonIds = array_values(array_diff($recipientPersonIds, [$actorId]));
42 }
43
44 if (!$recipientPersonIds) {
45 return null;
46 }
47
48 $notification = (new Notification())->getConnection()->transaction(function () use ($data, $recipientPersonIds, $eventType, $category) {
49 $notification = Notification::create([
50 'actor_id' => Arr::get($data, 'actor_id'),
51 'ticket_id' => Arr::get($data, 'ticket_id'),
52 'conversation_id' => Arr::get($data, 'conversation_id'),
53 'event_type' => $eventType,
54 'category' => $category,
55 'payload' => Arr::get($data, 'payload', [])
56 ]);
57
58 $this->attachRecipients($notification->id, $recipientPersonIds, Arr::get($data, 'channel', 'web'));
59
60 return $notification;
61 });
62
63 return $notification;
64 }
65
66 public function attachRecipients($notificationId, array $recipientPersonIds, $channel = 'web')
67 {
68 if (!(new NotificationSettings())->canUseNotificationTables()) {
69 return [];
70 }
71
72 $recipientPersonIds = $this->normalizeRecipientPersonIds($recipientPersonIds);
73
74 if (!$recipientPersonIds) {
75 return [];
76 }
77
78 $now = current_time('mysql');
79 $rows = [];
80
81 foreach ($recipientPersonIds as $recipientPersonId) {
82 $rows[] = [
83 'notification_id' => $notificationId,
84 'user_id' => $recipientPersonId,
85 'channel' => $channel ?: 'web',
86 'is_read' => 0,
87 'created_at' => $now,
88 'updated_at' => $now
89 ];
90 }
91
92 if ($rows) {
93 NotificationUser::insertOrIgnore($rows);
94 }
95
96 return $rows;
97 }
98
99 protected function normalizeRecipientPersonIds(array $recipientPersonIds)
100 {
101 return array_values(array_unique(array_map('intval', array_filter($recipientPersonIds))));
102 }
103
104 public function markAsRead($notificationId, $personId)
105 {
106 if (!(new NotificationSettings())->canUseNotificationTables()) {
107 return 0;
108 }
109
110 $now = current_time('mysql');
111
112 return NotificationUser::where('notification_id', $notificationId)
113 ->where('user_id', $personId)
114 ->update([
115 'is_read' => 1,
116 'read_at' => $now,
117 'updated_at' => $now
118 ]);
119 }
120
121 public function markAllAsRead($personId)
122 {
123 if (!(new NotificationSettings())->canUseNotificationTables()) {
124 return 0;
125 }
126
127 $now = current_time('mysql');
128
129 return NotificationUser::where('user_id', $personId)
130 ->where('is_read', 0)
131 ->update([
132 'is_read' => 1,
133 'read_at' => $now,
134 'updated_at' => $now
135 ]);
136 }
137
138 public function getUnreadCount($personId, array $filters = [])
139 {
140 return (new NotificationQueryService())->getUnreadCount($personId, $filters);
141 }
142
143 public function paginateForPerson($personId, array $filters = [])
144 {
145 return (new NotificationQueryService())->paginateForPerson($personId, $filters);
146 }
147
148 public function getRecentForPerson($personId, array $filters = [])
149 {
150 return (new NotificationQueryService())->getRecentForPerson($personId, $filters);
151 }
152 }
153