diff --git a/src/Notifications/BaseNotification.php b/src/Notifications/BaseNotification.php index 0c48c70..e1b2ec3 100644 --- a/src/Notifications/BaseNotification.php +++ b/src/Notifications/BaseNotification.php @@ -11,12 +11,13 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; +use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Seat\Web\Models\Group; abstract class BaseNotification extends Notification implements ShouldQueue { - use Queueable, SerializesModels; + use Queueable, SerializesModels, InteractsWithQueue; /** * @var array diff --git a/src/Notifications/RefreshTokenDeletedNotification.php b/src/Notifications/RefreshTokenDeletedNotification.php index ac50a0d..f1bdbf4 100644 --- a/src/Notifications/RefreshTokenDeletedNotification.php +++ b/src/Notifications/RefreshTokenDeletedNotification.php @@ -12,6 +12,7 @@ use Herpaderpaldent\Seat\SeatNotifications\Channels\Discord\DiscordMessage; use Herpaderpaldent\Seat\SeatNotifications\Channels\Slack\SlackChannel; use Herpaderpaldent\Seat\SeatNotifications\Channels\Slack\SlackMessage; +use Illuminate\Support\Facades\Redis; use Seat\Eveapi\Models\Corporation\CorporationInfo; use Seat\Eveapi\Models\RefreshToken; @@ -37,7 +38,7 @@ public function __construct(RefreshToken $refresh_token) $this->user_name = $refresh_token->user->name; $this->image = 'https://imageserver.eveonline.com/Character/' . $refresh_token->character_id . '_128.jpg'; $this->main_character = $this->getMainCharacter($refresh_token->user->group)->name; - $this->corporation = CorporationInfo::find($refresh_token->user->character->corporation_id)->name; + $this->corporation = optional(CorporationInfo::find($refresh_token->user->character->corporation_id))->name ?: 'NPC Corporation'; } /** @@ -48,26 +49,39 @@ public function __construct(RefreshToken $refresh_token) */ public function via($notifiable) { - switch($notifiable->via) { - case 'discord': - $this->tags = [ - 'refresh_token', - 'discord', - $notifiable->type === 'private' ? $notifiable->recipient() : 'channel', - ]; - - return [DiscordChannel::class]; - break; - case 'slack': - $this->tags = [ - 'refresh_token', - 'slack', - $notifiable->type === 'private' ? $notifiable->recipient() : 'channel', - ]; - - return [SlackChannel::class]; - break; - } + Redis::funnel('seatnotification:channel_id' . $notifiable->channel_id)->limit(1)->then(function () use ($notifiable) { + switch($notifiable->via) { + case 'discord': + $this->tags = [ + 'refresh_token', + 'discord', + $notifiable->type === 'private' ? $notifiable->recipient() : 'channel', + ]; + + return [DiscordChannel::class]; + break; + case 'slack': + $this->tags = [ + 'refresh_token', + 'slack', + $notifiable->type === 'private' ? $notifiable->recipient() : 'channel', + ]; + + return [SlackChannel::class]; + break; + default: + return ['']; + } + }, function () use ($notifiable) { + + logger()->warning(' A notification on ' . $notifiable->via . + ' for channel ' . $notifiable->channel_id . + ' has already been dispateched. Removing the job from the queue'); + + $this->delete(); + }); + + return ['']; } public function toDiscord($notifiable) diff --git a/src/Observers/RefreshTokenObserver.php b/src/Observers/RefreshTokenObserver.php index d5e041b..55a0a4d 100644 --- a/src/Observers/RefreshTokenObserver.php +++ b/src/Observers/RefreshTokenObserver.php @@ -12,21 +12,25 @@ use Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshTokenDeletedNotification; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; +use Illuminate\Support\Facades\Redis; use Seat\Eveapi\Models\RefreshToken; class RefreshTokenObserver { public function deleting(RefreshToken $refresh_token) { - Log::info('SoftDelete detected of ' . $refresh_token->user->name); - - $receipients = RefreshTokenNotification::all() - ->filter(function ($recepient) { - return $recepient->shouldReceive(); - }); - - Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token))); - + Redis::funnel('soft_delete:refresh_token_' . $refresh_token->user->name)->limit(1)->then(function () use ($refresh_token) { + Log::info('SoftDelete detected of ' . $refresh_token->user->name); + + $receipients = RefreshTokenNotification::all() + ->filter(function ($recepient) { + return $recepient->shouldReceive(); + }); + + Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token))); + }, function () use ($refresh_token) { + Log::info('A Soft-Delete job is already running for ' . $refresh_token->user->name); + }); } public function test() diff --git a/src/config/seatnotifications.config.php b/src/config/seatnotifications.config.php index ba42bb3..6d36c79 100644 --- a/src/config/seatnotifications.config.php +++ b/src/config/seatnotifications.config.php @@ -6,5 +6,5 @@ * Time: 10:24. */ return [ - 'version' => '0.0.1', + 'version' => '1.0.2', ];