PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.95
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.95
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Services / NotificationService.php

NotificationService.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.95, at app/Services/NotificationService.php

277 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Services;
4
5 use FluentBoards\App\Models\Meta;
6 use FluentBoards\App\Models\Notification;
7 use FluentBoards\App\Models\NotificationUser;
8 use FluentBoards\App\Models\Relation;
9 use FluentBoards\App\Models\Task;
10 use FluentBoards\App\Models\User;
11
12 class NotificationService
13 {
14 public function getAllNotifications($per_page, $page, $action = 'all')
15 {
16 $user = wp_get_current_user();
17
18 $query = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
19 ->whereHas('users', function($q) use ($user) {
20 $q->where('user_id', $user->ID);
21 });
22
23 // Add action filter if not 'all'
24 if ($action !== 'all') {
25 $query->where('action', $action);
26 }
27
28 $notifications = $query->with('activitist', 'task')
29 ->orderBy('created_at', 'desc')
30 ->paginate($per_page, ['*'], 'page', $page);
31
32 foreach ($notifications as $notification){
33 $notification->read = $notification->checkReadOrNot();
34 }
35
36 return $notifications;
37 }
38
39 /**
40 * @throws \Exception
41 */
42 public function getAllUnreadNotifications($per_page, $page)
43 {
44 $userId = get_current_user_id();
45 if (!$userId) {
46 throw new \Exception(esc_html__('You are not allowed to do that', 'fluent-boards'), 403);
47 }
48 $unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
49 ->whereHas('users', function($q) use ($userId) {
50 $q->where('user_id', $userId)
51 ->whereNull('marked_read_at');
52 })->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page);
53 return $unreadNotifications;
54 }
55
56 public function markAllRead()
57 {
58 $user = wp_get_current_user();
59
60 $userNotifications = NotificationUser::where('user_id', $user->ID)
61 ->with('notification')
62 ->get();
63
64 foreach ($userNotifications as $data)
65 {
66 $data->marked_read_at = current_time('mysql');
67 $data->save();
68 }
69 }
70
71 public function markNotificationRead($notificationId)
72 {
73 $user = wp_get_current_user();
74
75 $notification = NotificationUser::where('user_id', $user->ID)
76 ->where('notification_id', $notificationId)
77 ->first();
78 if (!$notification) {
79 throw new \Exception(esc_html__('Notification could not be found', 'fluent-boards'), 404);
80 }
81
82 $notification->marked_read_at = current_time('mysql');
83 $notification->save();
84 return $notification;
85 }
86
87 public function newNotificationNumber()
88 {
89 $user = wp_get_current_user();
90
91 $unread_notifications = Notification::query()->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
92 ->whereHas('users', function($q) use ($user) {
93 $q->where('user_id', $user->ID)
94 ->whereNull('marked_read_at');
95 });
96
97 return $unread_notifications->count();
98 }
99
100 public function isCurrentUserObservingTask($task)
101 {
102 $currentUserId = get_current_user_id();
103 $observers = $task->watchers()->get()->pluck('ID');
104
105 foreach ($observers as $id){
106 if($id == $currentUserId)
107 return true;
108 }
109
110 return false;
111 }
112
113 public function getBoardNotificationSettingsOfUser($id, $userId)
114 {
115 $boardSettings = Relation::where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
116 ->where('object_id', $id)
117 ->where('foreign_id', $userId)
118 ->first();
119
120 return $boardSettings;
121 }
122
123 public function getGlobalNotificationSettingsOfUser($userId)
124 {
125 $settings = Meta::where('object_type', Constant::OBJECT_TYPE_USER)
126 ->where('object_id', $userId)
127 ->where('key', Constant::USER_GLOBAL_NOTIFICATIONS)
128 ->first();
129
130 return $settings;
131 }
132
133 public function updateBoardNotificationSettings($newSettings, $id)
134 {
135 $userId = get_current_user_id();
136 $boardSettings = $this->getBoardNotificationSettingsOfUser($id, $userId);
137 if(empty($boardSettings)){
138 return;
139 }
140 foreach ($newSettings as $index => $setting)
141 {
142 $newSettings[$index] = $setting == 'true' ? true : false;
143 }
144 $boardSettings->preferences = $newSettings;
145 $boardSettings->save();
146
147 }
148
149 public function filterAssigneeToSendEmail($taskId, $emailPurpose){
150 $task = Task::findOrFail($taskId);
151 $watchers = $task->watchers;
152 $currentUserId = get_current_user_id();
153
154 $wathersToSendEmail = array();
155
156 foreach ($watchers as $watcher){
157 if($watcher->ID != $currentUserId)
158 {
159 if($this->checkIfEmailEnable($watcher->ID, $emailPurpose, $task->board_id))
160 {
161 $wathersToSendEmail[] = $watcher->user_email;
162 }
163 }
164 }
165
166 return $wathersToSendEmail;
167 }
168
169 public function checkIfEmailEnable($userId, $emailPurpose, $boardId)
170 {
171 if(
172 $this->checkIfEmailEnabled($boardId, $userId, $emailPurpose)
173 ){
174 return true;
175 }else{
176 return false;
177 }
178 }
179
180 public function checkIfEmailEnabled($boardId, $userId, $purpose)
181 {
182 $boardSettings = $this->getBoardNotificationSettingsOfUser($boardId, $userId);
183
184 if($boardSettings){
185 $preferences = maybe_unserialize($boardSettings['preferences']);
186 if(!array_key_exists($purpose, $preferences)){
187 $preferences[$purpose] = true;
188 $boardSettings->preferences = $preferences;
189 $boardSettings->save();
190 return true;
191 }
192 return $preferences[$purpose];
193 }
194
195 return false;
196 }
197
198 public function mentionInComment($comment, $mentionedUserIds)
199 {
200 $uniqueIds = array_unique($mentionedUserIds);
201
202 $uniqueIds = array_filter($uniqueIds, function($value) {
203 return (int)$value !== get_current_user_id();
204 });
205
206 //sending emails to mentioned users
207 $mentionedUserEmails = User::whereIn('ID', $uniqueIds)->pluck('user_email');
208 $this->sendMailAfterMention($comment->id, $mentionedUserEmails);
209
210 //sending desktop notifications
211 do_action('fluent_boards/mention_comment_notification', $comment, $uniqueIds);
212 }
213
214 public function sendMailAfterMention($commentId, $usersToSendEmail)
215 {
216 $current_user_id = get_current_user_id();
217
218 /* this will run in background as soon as possible */
219 /* sending Model or Model Instance won't work here */
220 as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_mention', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards');
221 }
222
223 public function getUnreadNotificationsOfTasks($task)
224 {
225 $userId = get_current_user_id();
226 if (!$userId) {
227 throw new \Exception(esc_html__('You are not allowed to do that', 'fluent-boards'), 403);
228 }
229 $unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
230 ->whereHas('users', function($q) use ($userId) {
231 $q->where('user_id', $userId)
232 ->whereNull('marked_read_at');
233 })->whereHas('task', function($q) use ($task) {
234 $q->where('id', $task->id);
235 })
236 ->with('activitist')->orderBy('created_at', 'desc')->count();
237 return $unreadNotifications;
238 }
239
240 public function getUnreadNotificationCountsByTaskIds($taskIds)
241 {
242 $userId = get_current_user_id();
243 if (!$userId) {
244 throw new \Exception(esc_html__('You are not allowed to do that', 'fluent-boards'), 403);
245 }
246
247 $taskIds = array_values(array_filter(array_map('intval', (array) $taskIds)));
248 if (!$taskIds) {
249 return [];
250 }
251
252 $rows = Notification::query()
253 // Board/list views can return many tasks at once, so unread counts are
254 // grouped in one query instead of issuing a count query per task.
255 ->selectRaw('task_id, COUNT(*) as unread_count')
256 ->join(
257 (new NotificationUser())->getTable(),
258 'fbs_notifications.id',
259 '=',
260 'fbs_notification_users.notification_id'
261 )
262 ->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION)
263 ->where('fbs_notification_users.user_id', $userId)
264 ->whereNull('fbs_notification_users.marked_read_at')
265 ->whereIn('task_id', $taskIds)
266 ->groupBy('task_id')
267 ->get();
268
269 $notificationCounts = [];
270 foreach ($rows as $row) {
271 $notificationCounts[(int) $row->task_id] = (int) $row->unread_count;
272 }
273
274 return $notificationCounts;
275 }
276 }
277