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:
@@ -10,6 +10,13 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
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 @@ trait HasNotificationSettings
|
|||||||
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 @@ trait HasNotificationSettings
|
|||||||
];
|
];
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user