Skip to content

Commit

Permalink
#8333 Updated code to consider the notification_subscription_settings…
Browse files Browse the repository at this point in the history
….context_id
  • Loading branch information
jonasraoni committed Jul 24, 2024
1 parent e45b09a commit 1d6663b
Showing 1 changed file with 32 additions and 48 deletions.
80 changes: 32 additions & 48 deletions classes/notification/NotificationSubscriptionSettingsDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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'
]);
}
}

Expand All @@ -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');
Expand Down

0 comments on commit 1d6663b

Please sign in to comment.