| 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) |
| 15 |
{ |
| 16 |
$user = wp_get_current_user(); |
| 17 |
|
| 18 |
$notifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION) |
| 19 |
->whereHas('users', function($q) use ($user) { |
| 20 |
$q->where('user_id', $user->ID); |
| 21 |
})->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page); |
| 22 |
|
| 23 |
foreach ($notifications as $notification){ |
| 24 |
$notification->read = $notification->checkReadOrNot(); |
| 25 |
} |
| 26 |
|
| 27 |
return $notifications; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @throws \Exception |
| 32 |
*/ |
| 33 |
public function getAllUnreadNotifications($per_page, $page) |
| 34 |
{ |
| 35 |
$userId = get_current_user_id(); |
| 36 |
if (!$userId) { |
| 37 |
throw new \Exception('You are not allowed to do that', 403); |
| 38 |
} |
| 39 |
$unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION) |
| 40 |
->whereHas('users', function($q) use ($userId) { |
| 41 |
$q->where('user_id', $userId) |
| 42 |
->whereNull('marked_read_at'); |
| 43 |
})->with('activitist', 'task')->orderBy('created_at', 'desc')->paginate($per_page, ['*'], 'page', $page); |
| 44 |
return $unreadNotifications; |
| 45 |
} |
| 46 |
|
| 47 |
public function markAllRead() |
| 48 |
{ |
| 49 |
$user = wp_get_current_user(); |
| 50 |
|
| 51 |
$userNotifications = NotificationUser::where('user_id', $user->ID) |
| 52 |
->with('notification') |
| 53 |
->get(); |
| 54 |
|
| 55 |
foreach ($userNotifications as $data) |
| 56 |
{ |
| 57 |
$data->marked_read_at = current_time('mysql'); |
| 58 |
$data->save(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public function markNotificationRead($notificationId) |
| 63 |
{ |
| 64 |
$user = wp_get_current_user(); |
| 65 |
|
| 66 |
$notification = NotificationUser::where('user_id', $user->ID) |
| 67 |
->where('notification_id', $notificationId) |
| 68 |
->first(); |
| 69 |
$notification->marked_read_at = current_time('mysql'); |
| 70 |
$notification->save(); |
| 71 |
return $notification; |
| 72 |
} |
| 73 |
|
| 74 |
public function newNotificationNumber() |
| 75 |
{ |
| 76 |
$user = wp_get_current_user(); |
| 77 |
|
| 78 |
$unread_notifications = Notification::query()->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION) |
| 79 |
->whereHas('users', function($q) use ($user) { |
| 80 |
$q->where('user_id', $user->ID) |
| 81 |
->whereNull('marked_read_at'); |
| 82 |
}); |
| 83 |
|
| 84 |
return $unread_notifications->count(); |
| 85 |
} |
| 86 |
|
| 87 |
public function isCurrentUserObservingTask($task) |
| 88 |
{ |
| 89 |
$currentUserId = get_current_user_id(); |
| 90 |
$observers = $task->watchers()->get()->pluck('ID'); |
| 91 |
|
| 92 |
foreach ($observers as $id){ |
| 93 |
if($id == $currentUserId) |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
public function getBoardNotificationSettingsOfUser($id, $userId) |
| 101 |
{ |
| 102 |
$boardSettings = Relation::where('object_type', Constant::OBJECT_TYPE_BOARD_USER) |
| 103 |
->where('object_id', $id) |
| 104 |
->where('foreign_id', $userId) |
| 105 |
->first(); |
| 106 |
|
| 107 |
return $boardSettings; |
| 108 |
} |
| 109 |
|
| 110 |
public function getGlobalNotificationSettingsOfUser($userId) |
| 111 |
{ |
| 112 |
$settings = Meta::where('object_type', Constant::OBJECT_TYPE_USER) |
| 113 |
->where('object_id', $userId) |
| 114 |
->where('key', Constant::USER_GLOBAL_NOTIFICATIONS) |
| 115 |
->first(); |
| 116 |
|
| 117 |
return $settings; |
| 118 |
} |
| 119 |
|
| 120 |
public function updateBoardNotificationSettings($newSettings, $id) |
| 121 |
{ |
| 122 |
$userId = get_current_user_id(); |
| 123 |
$boardSettings = $this->getBoardNotificationSettingsOfUser($id, $userId); |
| 124 |
if(empty($boardSettings)){ |
| 125 |
return; |
| 126 |
} |
| 127 |
foreach ($newSettings as $index => $setting) |
| 128 |
{ |
| 129 |
$newSettings[$index] = $setting == 'true' ? true : false; |
| 130 |
} |
| 131 |
$boardSettings->preferences = $newSettings; |
| 132 |
$boardSettings->save(); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
public function filterAssigneeToSendEmail($taskId, $emailPurpose){ |
| 137 |
$task = Task::findOrFail($taskId); |
| 138 |
$watchers = $task->watchers; |
| 139 |
$currentUserId = get_current_user_id(); |
| 140 |
|
| 141 |
$wathersToSendEmail = array(); |
| 142 |
|
| 143 |
foreach ($watchers as $watcher){ |
| 144 |
if($watcher->ID != $currentUserId) |
| 145 |
{ |
| 146 |
if($this->checkIfEmailEnable($watcher->ID, $emailPurpose, $task->board_id)) |
| 147 |
{ |
| 148 |
$wathersToSendEmail[] = $watcher->user_email; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return $wathersToSendEmail; |
| 154 |
} |
| 155 |
|
| 156 |
public function checkIfEmailEnable($userId, $emailPurpose, $boardId) |
| 157 |
{ |
| 158 |
if( |
| 159 |
$this->checkIfEmailEnabled($boardId, $userId, $emailPurpose) |
| 160 |
){ |
| 161 |
return true; |
| 162 |
}else{ |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public function checkIfEmailEnabled($boardId, $userId, $purpose) |
| 168 |
{ |
| 169 |
$boardSettings = $this->getBoardNotificationSettingsOfUser($boardId, $userId); |
| 170 |
|
| 171 |
if($boardSettings){ |
| 172 |
$preferences = maybe_unserialize($boardSettings['preferences']); |
| 173 |
if(!array_key_exists($purpose, $preferences)){ |
| 174 |
$preferences[$purpose] = true; |
| 175 |
$boardSettings->preferences = $preferences; |
| 176 |
$boardSettings->save(); |
| 177 |
return true; |
| 178 |
} |
| 179 |
return $preferences[$purpose]; |
| 180 |
} |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
public function checkIfEmailEnabledGlobally($userId, $purpose) |
| 186 |
{ |
| 187 |
$globalSettings = $this->getGlobalNotificationSettingsOfUser($userId); |
| 188 |
|
| 189 |
if($globalSettings){ |
| 190 |
$preferences = maybe_unserialize($globalSettings['value']); |
| 191 |
if(!array_key_exists($purpose, $preferences)){ |
| 192 |
$preferences[$purpose] = true; |
| 193 |
$globalSettings->preferences = $preferences; |
| 194 |
$globalSettings->save(); |
| 195 |
return true; |
| 196 |
} |
| 197 |
return $preferences[$purpose]; |
| 198 |
} |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
public function mentionInComment($comment, $mentionedUserIds) |
| 204 |
{ |
| 205 |
$uniqueIds = array_unique($mentionedUserIds); |
| 206 |
$currentUserId = get_current_user_id(); |
| 207 |
if (in_array($currentUserId, $uniqueIds)) |
| 208 |
{ |
| 209 |
$key = array_search($currentUserId, $uniqueIds); |
| 210 |
if ($key !== false) { |
| 211 |
unset($uniqueIds[$key]); |
| 212 |
} |
| 213 |
// Re-index the array (optional) |
| 214 |
$uniqueIds = array_values($uniqueIds); |
| 215 |
} |
| 216 |
//sending emails to mentioned users |
| 217 |
$mentionedUserEmails = User::whereIn('ID', $uniqueIds)->pluck('user_email'); |
| 218 |
$this->sendMailAfterMention($comment->id, $mentionedUserEmails); |
| 219 |
|
| 220 |
//sending desktop notifications |
| 221 |
do_action('fluent_boards/mention_comment_notification', $comment, $mentionedUserIds); |
| 222 |
} |
| 223 |
|
| 224 |
public function sendMailAfterMention($commentId, $usersToSendEmail) |
| 225 |
{ |
| 226 |
$current_user_id = get_current_user_id(); |
| 227 |
|
| 228 |
/* this will run in background as soon as possible */ |
| 229 |
/* sending Model or Model Instance won't work here */ |
| 230 |
as_enqueue_async_action('fluent_boards/one_time_schedule_send_email_for_mention', [$commentId, $usersToSendEmail, $current_user_id], 'fluent-boards'); |
| 231 |
} |
| 232 |
|
| 233 |
public function getUnreadNotificationsOfTasks($task) |
| 234 |
{ |
| 235 |
$userId = get_current_user_id(); |
| 236 |
if (!$userId) { |
| 237 |
throw new \Exception('You are not allowed to do that', 403); |
| 238 |
} |
| 239 |
$unreadNotifications = Notification::where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION) |
| 240 |
->whereHas('users', function($q) use ($userId) { |
| 241 |
$q->where('user_id', $userId) |
| 242 |
->whereNull('marked_read_at'); |
| 243 |
})->whereHas('task', function($q) use ($task) { |
| 244 |
$q->where('id', $task->id); |
| 245 |
}) |
| 246 |
->with('activitist')->orderBy('created_at', 'desc')->count(); |
| 247 |
return $unreadNotifications; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|