fix: notification trait
fix: some events must always be enabled, so a notification is sent all the time (user cannot choose to not receive this notification). fix: check if the event is enabled before adding a channel to enabled
This commit is contained in:
parent
96c970ca4e
commit
dedfd72731
1 changed files with 29 additions and 14 deletions
|
|
@ -10,6 +10,13 @@
|
||||||
|
|
||||||
trait HasNotificationSettings
|
trait HasNotificationSettings
|
||||||
{
|
{
|
||||||
|
protected $alwaysSendEvents = [
|
||||||
|
'server_force_enabled',
|
||||||
|
'server_force_disabled',
|
||||||
|
'general',
|
||||||
|
'test',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get settings model for specific channel
|
* Get settings model for specific channel
|
||||||
*/
|
*/
|
||||||
|
|
@ -34,17 +41,31 @@ public function isNotificationEnabled(string $channel): bool
|
||||||
return $settings?->isEnabled() ?? false;
|
return $settings?->isEnabled() ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a specific notification type is enabled for a channel
|
||||||
|
*/
|
||||||
|
public function isNotificationTypeEnabled(string $channel, string $event): bool
|
||||||
|
{
|
||||||
|
$settings = $this->getNotificationSettings($channel);
|
||||||
|
|
||||||
|
if (! $settings || ! $this->isNotificationEnabled($channel)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in_array($event, $this->alwaysSendEvents)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$settingKey = "{$event}_{$channel}_notifications";
|
||||||
|
|
||||||
|
return (bool) $settings->$settingKey;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all enabled notification channels for an event
|
* Get all enabled notification channels for an event
|
||||||
*/
|
*/
|
||||||
public function getEnabledChannels(string $event): array
|
public function getEnabledChannels(string $event): array
|
||||||
{
|
{
|
||||||
$alwaysSendEvents = [
|
|
||||||
'server_force_enabled',
|
|
||||||
'server_force_disabled',
|
|
||||||
'general',
|
|
||||||
];
|
|
||||||
|
|
||||||
$channels = [];
|
$channels = [];
|
||||||
|
|
||||||
$channelMap = [
|
$channelMap = [
|
||||||
|
|
@ -55,14 +76,8 @@ public function getEnabledChannels(string $event): array
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($channelMap as $channel => $channelClass) {
|
foreach ($channelMap as $channel => $channelClass) {
|
||||||
if (in_array($event, $alwaysSendEvents)) {
|
if ($this->isNotificationEnabled($channel) && $this->isNotificationTypeEnabled($channel, $event)) {
|
||||||
if ($this->isNotificationEnabled($channel)) {
|
$channels[] = $channelClass;
|
||||||
$channels[] = $channelClass;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($this->isNotificationEnabled($channel) && $this->isNotificationTypeEnabled($channel, $event)) {
|
|
||||||
$channels[] = $channelClass;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue