forked from eveseat/notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent concurrent notifications to be dispatched
Closes eveseat/seat#680
- Loading branch information
Showing
80 changed files
with
444 additions
and
995 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to 2020 Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Notifications\Jobs; | ||
|
||
use Seat\Eveapi\Models\Character\CharacterNotification; | ||
use Seat\Notifications\Jobs\Middleware\CharacterNotificationThrottler; | ||
|
||
/** | ||
* Class AbstractCharacterNotification. | ||
* | ||
* @package Seat\Notifications\Notifications | ||
*/ | ||
abstract class AbstractCharacterNotification extends AbstractNotification | ||
{ | ||
/** | ||
* @var \Seat\Eveapi\Models\Character\CharacterNotification | ||
*/ | ||
protected $notification; | ||
|
||
/** | ||
* AbstractCharacterNotification constructor. | ||
* | ||
* @param \Seat\Eveapi\Models\Character\CharacterNotification $notification | ||
*/ | ||
public function __construct(CharacterNotification $notification) | ||
{ | ||
$this->notification = $notification; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function middleware() | ||
{ | ||
return [ | ||
new CharacterNotificationThrottler, | ||
]; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getNotificationId() | ||
{ | ||
return $this->notification->notification_id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to 2020 Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Notifications\Jobs\Middleware; | ||
|
||
use Illuminate\Support\Facades\Redis; | ||
|
||
/** | ||
* Class CharacterThrottler. | ||
* | ||
* @package Seat\Notifications\Jobs\Middleware | ||
*/ | ||
class CharacterNotificationThrottler | ||
{ | ||
/** | ||
* @param \Seat\Notifications\Jobs\AbstractCharacterNotification $job | ||
* @param $next | ||
*/ | ||
public function handle($job, $next) | ||
{ | ||
$key = sprintf('%s:%d', implode(',', $job->via(true)), $job->getNotificationId()); | ||
|
||
Redis::throttle($key)->block(0)->allow(1)->every(2)->then(function () use ($job, $next) { | ||
$next($job); | ||
}, function () use ($job) { | ||
logger()->debug('Notification has been queued more than once. Removing duplicates.', [ | ||
'id' => $job->getNotificationId(), | ||
'channel' => $job->via(true), | ||
]); | ||
|
||
$job->delete(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to 2020 Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Notifications\Notifications\Characters\Discord; | ||
|
||
use Seat\Eveapi\Models\Corporation\CorporationInfo; | ||
use Seat\Eveapi\Models\Killmails\KillmailDetail; | ||
use Seat\Notifications\Jobs\AbstractNotification; | ||
use Seat\Notifications\Services\Discord\Messages\DiscordEmbed; | ||
use Seat\Notifications\Services\Discord\Messages\DiscordEmbedField; | ||
use Seat\Notifications\Services\Discord\Messages\DiscordMessage; | ||
use Seat\Notifications\Traits\NotificationTools; | ||
|
||
/** | ||
* Class Killmail | ||
* | ||
* @package Seat\Notifications\Notifications\Characters\Discord | ||
*/ | ||
class Killmail extends AbstractNotification | ||
{ | ||
use NotificationTools; | ||
|
||
/** | ||
* @var \Seat\Eveapi\Models\Killmails\KillmailDetail | ||
*/ | ||
private $killmail; | ||
|
||
/** | ||
* Killmail constructor. | ||
* | ||
* @param \Seat\Eveapi\Models\Killmails\KillmailDetail $killmail | ||
*/ | ||
public function __construct(KillmailDetail $killmail) | ||
{ | ||
$this->killmail = $killmail; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['discord']; | ||
} | ||
|
||
/** | ||
* @param $notifiable | ||
* | ||
* @return \Seat\Notifications\Services\Discord\Messages\DiscordMessage | ||
*/ | ||
public function toDiscord($notifiable) | ||
{ | ||
return (new DiscordMessage()) | ||
->content('A kill has been recorded for your corporation!') | ||
->embed(function (DiscordEmbed $embed) { | ||
$embed->timestamp($this->killmail->killmail_time); | ||
$embed->author('SeAT Kilometer', asset('web/img/favico/apple-icon-180x180.png')); | ||
|
||
$embed->field('Ship Type', $this->killmail->victim->ship->typeName); | ||
$embed->field('zKB Link', sprintf('https://zkillboard.com/kill/%d/', $this->killmail->killmail_id)); | ||
$embed->field(function (DiscordEmbedField $field) { | ||
$field->name('System'); | ||
$field->value( | ||
$this->zKillBoardToDiscordLink( | ||
'system', | ||
$this->killmail->solar_system_id, | ||
sprintf('%s (%s)', | ||
$this->killmail->solar_system->name, | ||
number_format($this->killmail->solar_system->security, 2)))); | ||
}); | ||
|
||
$embed->thumb($this->typeIconUrl($this->killmail->victim->ship_type_id)); | ||
$embed->footer('zKillboard', 'https://zkillboard.com/img/wreck.png'); | ||
|
||
(CorporationInfo::find($this->killmail->victim->corporation_id)) ? | ||
$embed->color(DiscordMessage::ERROR) : $embed->color(DiscordMessage::SUCCESS); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SeAT | ||
* | ||
* Copyright (C) 2015 to 2020 Leon Jacobs | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
namespace Seat\Notifications\Notifications\Characters\Discord; | ||
|
||
use Illuminate\Support\Str; | ||
use Seat\Notifications\Jobs\AbstractNotification; | ||
use Seat\Notifications\Services\Discord\Messages\DiscordEmbed; | ||
use Seat\Notifications\Services\Discord\Messages\DiscordMessage; | ||
|
||
/** | ||
* Class NewMailMessage | ||
* | ||
* @package Seat\Notifications\Notifications\Characters\Discord | ||
*/ | ||
class NewMailMessage extends AbstractNotification | ||
{ | ||
/** | ||
* @var | ||
*/ | ||
private $message; | ||
|
||
/** | ||
* NewMailMessage constructor. | ||
* | ||
* @param $message | ||
*/ | ||
public function __construct($message) | ||
{ | ||
$this->message = $message; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['discord']; | ||
} | ||
|
||
/** | ||
* @param $notifiable | ||
* | ||
* @return \Seat\Notifications\Services\Discord\Messages\DiscordMessage | ||
*/ | ||
public function toDiscord($notifiable) | ||
{ | ||
return (new DiscordMessage()) | ||
->content('New EVEMail Received!') | ||
->embed(function (DiscordEmbed $embed) { | ||
$embed->timestamp($this->message->timestamp); | ||
$embed->author('SeAT Personal Agent', asset('web/img/favico/apple-icon-180x180.png')); | ||
|
||
$embed->description(Str::limit( | ||
str_replace('<br>', ' ', clean_ccp_html($this->message->body->body, '<br>')), | ||
2000)); | ||
|
||
$embed->field('Subject', $this->message->subject); | ||
$embed->field('Sent Date', $this->message->timestamp); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.