mc-cms-namelessmc/modules/Core/hooks/DefaultUserNotificationPreferencesHook.php
Sam 26a2e5846d
feat: default notification preferences (#3590)
Co-authored-by: Sam <samerton@users.noreply.github.com>
2025-03-29 14:22:14 +00:00

32 lines
1,017 B
PHP

<?php
class DefaultUserNotificationPreferencesHook {
public static function execute(UserRegisteredEvent $event): void {
$userId = $event->user->data()->id;
if (!$userId) {
return;
}
self::subscribeUserToDefaultNotifications($userId);
}
public static function subscribeUserToDefaultNotifications(int $userId): void {
$defaultNotifications = array_filter(
Notification::getTypes(),
static fn($type) => $type['defaultPreferences']['alert'] || $type['defaultPreferences']['email']
);
foreach ($defaultNotifications as $notificationType) {
DB::getInstance()->insert('users_notification_preferences', [
'user_id' => $userId,
'type' => $notificationType['key'],
'alert' => $notificationType['defaultPreferences']['alert'] === true,
'email' => $notificationType['defaultPreferences']['email'] === true,
]);
}
}
}