From 1d6663be9ca3f83ce27cd2078629fc1c3b30e003 Mon Sep 17 00:00:00 2001 From: Jonas Raoni Soares da Silva Date: Tue, 23 Jul 2024 22:57:49 +0300 Subject: [PATCH] pkp/pkp-lib#8333 Updated code to consider the notification_subscription_settings.context_id --- .../NotificationSubscriptionSettingsDAO.php | 80 ++++++++----------- 1 file changed, 32 insertions(+), 48 deletions(-) diff --git a/classes/notification/NotificationSubscriptionSettingsDAO.php b/classes/notification/NotificationSubscriptionSettingsDAO.php index 1f0c0233578..6ce3ec45940 100644 --- a/classes/notification/NotificationSubscriptionSettingsDAO.php +++ b/classes/notification/NotificationSubscriptionSettingsDAO.php @@ -38,33 +38,27 @@ class NotificationSubscriptionSettingsDAO extends \PKP\db\DAO */ public function deleteNotificationSubscriptionSettings(int $notificationId, int $userId, ?string $settingName = null): int { - $params = [$notificationId, $userId]; - if ($settingName !== null) { - $params[] = $settingName; - } - - return $this->update( - 'DELETE FROM notification_subscription_settings - WHERE notification_id= ? AND user_id = ?' . isset($settingName) ? ' AND setting_name = ?' : '', - $params - ); + return DB::table('notification_subscription_settings') + ->where('notification_id', '=', $notificationId) + ->where('user_id', '=', $userId) + ->when(isset($settingName), fn (Builder $q) => $q->where('setting_name', '=', $settingName)) + ->delete(); } /** * Retrieve Notification subscription settings by user id + * + * @return int[] */ public function getNotificationSubscriptionSettings(string $settingName, int $userId, ?int $contextId): array { - $result = $this->retrieve( - 'SELECT setting_value FROM notification_subscription_settings WHERE user_id = ? AND setting_name = ? AND COALESCE(context, 0) = ?', - [$userId, $settingName, (int) $contextId] - ); - - $settings = []; - foreach ($result as $row) { - $settings[] = (int) $row->setting_value; - } - return $settings; + return DB::table('notification_subscription_settings') + ->where('user_id', '=', $userId) + ->where('setting_name', '=', $settingName) + ->whereRaw('COALESCE(context_id, 0) = ?', [(int) $contextId]) + ->pluck('setting_value') + ->map(fn ($v) => intval($v)) + ->toArray(); } /** @@ -73,25 +67,20 @@ public function getNotificationSubscriptionSettings(string $settingName, int $us public function updateNotificationSubscriptionSettings(string $settingName, array $settings, int $userId, ?int $contextId): void { // Delete old settings first, then insert new settings - $this->update( - 'DELETE FROM notification_subscription_settings WHERE user_id = ? AND setting_name = ? AND COALESCE(context, 0) = ?', - [$userId, $settingName, (int) $contextId] - ); + DB::table('notification_subscription_settings') + ->where('user_id', '=', $userId) + ->where('setting_name', '=', $settingName) + ->whereRaw('COALESCE(context_id, 0) = ?', [(int) $contextId]) + ->delete(); foreach ($settings as $setting) { - $this->update( - 'INSERT INTO notification_subscription_settings - (setting_name, setting_value, user_id, context, setting_type) - VALUES - (?, ?, ?, ?, ?)', - [ - $settingName, - (int) $setting, - $userId, - $contextId, - 'int' - ] - ); + DB::table('notification_subscription_settings')->insert([ + 'setting_name' => $settingName, + 'setting_value' => (int) $setting, + 'user_id' => $userId, + 'context_id' => $contextId, + 'setting_type' => 'int' + ]); } } @@ -104,25 +93,20 @@ public function updateNotificationSubscriptionSettings(string $settingName, arra public function getSubscribedUserIds(array $blockedNotificationKey, array $blockedNotificationType, array $contextIds, ?array $roleIds = null): Collection { $currentDateTime = Core::getCurrentDate(); - return DB::table('users as u')->select('u.user_id') + return DB::table('users as u') + ->select('u.user_id') ->whereNotIn( 'u.user_id', - fn (Builder $q) => - $q->select('nss.user_id')->from('notification_subscription_settings as nss') + fn (Builder $q) => $q->select('nss.user_id') + ->from('notification_subscription_settings as nss') ->whereIn('setting_name', $blockedNotificationKey) ->whereIn('setting_value', $blockedNotificationType) )->whereExists( fn (Builder $q) => $q->from('user_user_groups', 'uug') ->join('user_groups AS ug', 'uug.user_group_id', '=', 'ug.user_group_id') ->whereColumn('uug.user_id', '=', 'u.user_id') - ->where( - fn (Builder $q) => $q->where('uug.date_start', '<=', $currentDateTime) - ->orWhereNull('uug.date_start') - ) - ->where( - fn (Builder $q) => $q->where('uug.date_end', '>', $currentDateTime) - ->orWhereNull('uug.date_end') - ) + ->where(fn (Builder $q) => $q->where('uug.date_start', '<=', $currentDateTime)->orWhereNull('uug.date_start')) + ->where(fn (Builder $q) => $q->where('uug.date_end', '>', $currentDateTime)->orWhereNull('uug.date_end')) ->whereIn(DB::raw('COALESCE(ug.context_id, 0)'), array_map(intval(...), $contextIds)) ->when(!is_null($roleIds), fn (Builder $q) => $q->whereIn('ug.role_id', $roleIds)) )->pluck('user_id');