From 74b6cdbcfce559a57456f65d5ddd9bdc007ef038 Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Sat, 5 Jan 2019 22:32:22 +0100 Subject: [PATCH 1/5] user new app() alias to avoid conflict with warlof-discord-connector. --- src/Channels/Discord/DiscordChannel.php | 2 +- .../Discord/DiscordNotificationChannelController.php | 2 +- src/Http/Controllers/Discord/DiscordServerController.php | 2 +- src/SeatNotificationsServiceProvider.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Channels/Discord/DiscordChannel.php b/src/Channels/Discord/DiscordChannel.php index 481fdc2..b4fc9b1 100644 --- a/src/Channels/Discord/DiscordChannel.php +++ b/src/Channels/Discord/DiscordChannel.php @@ -20,7 +20,7 @@ class DiscordChannel public function __construct() { - $this->discord = app('discord'); + $this->discord = app('seatnotifications-discord'); } public function send($notifiable, Notification $notification) diff --git a/src/Http/Controllers/Discord/DiscordNotificationChannelController.php b/src/Http/Controllers/Discord/DiscordNotificationChannelController.php index 710c035..8955e28 100644 --- a/src/Http/Controllers/Discord/DiscordNotificationChannelController.php +++ b/src/Http/Controllers/Discord/DiscordNotificationChannelController.php @@ -30,7 +30,7 @@ public function getChannels() $response = cache('herpaderp.seatnotifications.discord.channels'); if(is_null($response)){ - $channels = app('discord') + $channels = app('seatnotifications-discord') ->guild ->getGuildChannels([ 'guild.id' => (int) setting('herpaderp.seatnotifications.discord.credentials.guild_id', true), diff --git a/src/Http/Controllers/Discord/DiscordServerController.php b/src/Http/Controllers/Discord/DiscordServerController.php index 320e414..f2a068e 100644 --- a/src/Http/Controllers/Discord/DiscordServerController.php +++ b/src/Http/Controllers/Discord/DiscordServerController.php @@ -96,7 +96,7 @@ public function callback(Request $request) setting(['herpaderp.seatnotifications.discord.credentials.guild_id', $request->input('guild_id')], true); // update Discord container - app()->singleton('discord', function () { + app()->singleton('seatnotifications-discord', function () { return new DiscordClient([ 'tokenType' => 'Bot', 'token' => setting('herpaderp.seatnotifications.discord.credentials.bot_token', true), diff --git a/src/SeatNotificationsServiceProvider.php b/src/SeatNotificationsServiceProvider.php index c5b21ba..ef4fb53 100644 --- a/src/SeatNotificationsServiceProvider.php +++ b/src/SeatNotificationsServiceProvider.php @@ -88,7 +88,7 @@ private function addDiscordContainer() $bot_token = setting('herpaderp.seatnotifications.discord.credentials.bot_token', true); if (! is_null($bot_token)) { - $this->app->singleton('discord', function () { + $this->app->singleton('seatnotifications-discord', function () { return new DiscordClient([ 'tokenType' => 'Bot', 'token' => setting('herpaderp.seatnotifications.discord.credentials.bot_token', true), @@ -98,7 +98,7 @@ private function addDiscordContainer() } // bind discord alias to DiscordClient - $this->app->alias('discord', DiscordClient::class); + $this->app->alias('seatnotifications-discord', DiscordClient::class); } private function addSlackContainer() From ff0dba561155992368baf6aae9fd8cc2d164ecc1 Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Sat, 5 Jan 2019 23:37:47 +0100 Subject: [PATCH 2/5] Add markdown to discord notification --- src/Notifications/RefreshTokenDeletedNotification.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Notifications/RefreshTokenDeletedNotification.php b/src/Notifications/RefreshTokenDeletedNotification.php index a47836c..0951313 100644 --- a/src/Notifications/RefreshTokenDeletedNotification.php +++ b/src/Notifications/RefreshTokenDeletedNotification.php @@ -30,10 +30,13 @@ class RefreshTokenDeletedNotification extends BaseNotification public $corporation; + private $refresh_token; + public function __construct(RefreshToken $refresh_token) { parent::__construct(); + $this->refresh_token = $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; @@ -74,13 +77,17 @@ public function via($notifiable) public function toDiscord($notifiable) { + $character = sprintf('[%s](%s)', + $this->user_name, + route('configuration.users.edit', ['user_id' => $this->refresh_token->character_id])); + return (new DiscordMessage) - ->embed(function ($embed) { + ->embed(function ($embed) use ($character) { $embed->title('**A SeAT users refresh token was removed!**') ->thumbnail($this->image) ->color('16711680') - ->field('Character', $this->user_name, true) + ->field('Character', $character, true) ->field('Corporation', $this->corporation, true) ->field('Main Character', $this->main_character, false); }); From 7a3476ac3b590a354e1f0728419c457d261f89f9 Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Sat, 5 Jan 2019 23:38:52 +0100 Subject: [PATCH 3/5] Use Job to dispatch notification to prevent redundant messages. --- src/Jobs/RefreshTokenDeletionDispatcher.php | 53 +++++++++++++++++++ src/Jobs/SeatNotificationsJobBase.php | 58 +++++++++++++++++++++ src/Models/RefreshTokenNotification.php | 4 +- src/Observers/RefreshTokenObserver.php | 34 +++--------- 4 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 src/Jobs/RefreshTokenDeletionDispatcher.php create mode 100644 src/Jobs/SeatNotificationsJobBase.php diff --git a/src/Jobs/RefreshTokenDeletionDispatcher.php b/src/Jobs/RefreshTokenDeletionDispatcher.php new file mode 100644 index 0000000..151a324 --- /dev/null +++ b/src/Jobs/RefreshTokenDeletionDispatcher.php @@ -0,0 +1,53 @@ +refresh_token = $refresh_token; + } + + public function handle() + { + Redis::funnel('soft_delete:refresh_token_' . $this->refresh_token->user->name)->limit(1)->then(function () { + logger()->info('SoftDelete detected of ' . $this->refresh_token->user->name); + + $recipients = RefreshTokenNotification::all() + ->filter(function ($recepient) { + return $recepient->shouldReceive(); + }); + + Notification::send($recipients, (new RefreshTokenDeletedNotification($this->refresh_token))); + }, function () { + + logger()->info('A Soft-Delete job is already running for ' . $this->refresh_token->user->name); + $this->delete(); + }); + } + +} \ No newline at end of file diff --git a/src/Jobs/SeatNotificationsJobBase.php b/src/Jobs/SeatNotificationsJobBase.php new file mode 100644 index 0000000..ef83eb5 --- /dev/null +++ b/src/Jobs/SeatNotificationsJobBase.php @@ -0,0 +1,58 @@ +tags, $tags); + + return $tags; + } + + /** + * Execute the job. + * + * @return void + */ + abstract public function handle(); +} diff --git a/src/Models/RefreshTokenNotification.php b/src/Models/RefreshTokenNotification.php index 0b858c7..c61643f 100644 --- a/src/Models/RefreshTokenNotification.php +++ b/src/Models/RefreshTokenNotification.php @@ -93,13 +93,13 @@ public function recipient() : string if(is_null($this->group())) return 'channel'; - $main_character = $this->group()->main_character; + $main_character = $this->group()->main_character->name; if (is_null($main_character)) { logger()->warning('Group has no main character set. Attempt to make assignation based on first attached character.', [ 'group_id' => $this->group()->id, ]); - $main_character = $group->users->first()->character; + $main_character = $group->users->first()->character->name; } return $main_character; diff --git a/src/Observers/RefreshTokenObserver.php b/src/Observers/RefreshTokenObserver.php index c6d4875..dfd0634 100644 --- a/src/Observers/RefreshTokenObserver.php +++ b/src/Observers/RefreshTokenObserver.php @@ -8,28 +8,17 @@ namespace Herpaderpaldent\Seat\SeatNotifications\Observers; -use Herpaderpaldent\Seat\SeatNotifications\Models\RefreshTokenNotification; -use Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshTokenDeletedNotification; -use Illuminate\Support\Facades\Notification; -use Illuminate\Support\Facades\Redis; +use Herpaderpaldent\Seat\SeatNotifications\Jobs\RefreshTokenDeletionDispatcher; use Seat\Eveapi\Models\RefreshToken; class RefreshTokenObserver { public function deleting(RefreshToken $refresh_token) { - Redis::funnel('soft_delete:refresh_token_' . $refresh_token->user->name)->limit(1)->then(function () use ($refresh_token) { - logger()->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) { - logger()->info('A Soft-Delete job is already running for ' . $refresh_token->user->name); - }); + + $job = new RefreshTokenDeletionDispatcher($refresh_token); + + dispatch($job)->onQueue('high'); } public function test() @@ -37,18 +26,9 @@ public function test() $refresh_token = RefreshToken::find(95725047); - Redis::funnel('soft_delete:refresh_token_' . $refresh_token->user->name)->limit(1)->then(function () use ($refresh_token) { - logger()->info('SoftDelete detected of ' . $refresh_token->user->name); - - $receipients = RefreshTokenNotification::all() - ->filter(function ($recepient) { - return $recepient->shouldReceive(); - }); + $job = new RefreshTokenDeletionDispatcher($refresh_token); - Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token))); - }, function () use ($refresh_token) { - logger()->info('A Soft-Delete job is already running for ' . $refresh_token->user->name); - }); + dispatch($job)->onQueue('high'); } } From aa9185696f701ab4f794bd3209e25feb32eea042 Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Sat, 5 Jan 2019 23:46:43 +0100 Subject: [PATCH 4/5] include version --- src/config/seatnotifications.config.php | 2 +- .../views/seatnotifications/config.blade.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/config/seatnotifications.config.php b/src/config/seatnotifications.config.php index e48aef6..b1396c3 100644 --- a/src/config/seatnotifications.config.php +++ b/src/config/seatnotifications.config.php @@ -6,5 +6,5 @@ * Time: 10:24. */ return [ - 'version' => '1.0.3', + 'version' => '1.0.5', ]; diff --git a/src/resources/views/seatnotifications/config.blade.php b/src/resources/views/seatnotifications/config.blade.php index 054ac4c..0dc520a 100644 --- a/src/resources/views/seatnotifications/config.blade.php +++ b/src/resources/views/seatnotifications/config.blade.php @@ -19,6 +19,18 @@ + @@ -31,5 +43,9 @@ @endforeach +
+ +
+ @endsection \ No newline at end of file From 7ac8c9998578c5f0d61cf892964f12a33d60e3dd Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Sat, 5 Jan 2019 22:48:26 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- src/Jobs/RefreshTokenDeletionDispatcher.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Jobs/RefreshTokenDeletionDispatcher.php b/src/Jobs/RefreshTokenDeletionDispatcher.php index 151a324..6f8759c 100644 --- a/src/Jobs/RefreshTokenDeletionDispatcher.php +++ b/src/Jobs/RefreshTokenDeletionDispatcher.php @@ -3,12 +3,11 @@ * Created by PhpStorm. * User: felix * Date: 05.01.2019 - * Time: 22:53 + * Time: 22:53. */ namespace Herpaderpaldent\Seat\SeatNotifications\Jobs; - use Herpaderpaldent\Seat\SeatNotifications\Models\RefreshTokenNotification; use Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshTokenDeletedNotification; use Illuminate\Support\Facades\Notification; @@ -49,5 +48,4 @@ public function handle() $this->delete(); }); } - -} \ No newline at end of file +}