Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable killmail processing #67

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Config/notifications.killmails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015 to 2021 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.
*/

return [
'corporation_id' => env('NOTIFICATIONS_KILLMAILS_CORPORATION_ID', null),
'alliance_id' => env('NOTIFICATIONS_KILLMAILS_ALLIANCE_ID', null),
'process_kills' => env('NOTIFICATIONS_KILLMAILS_KILLS', true),
'process_losses' => env('NOTIFICATIONS_KILLMAILS_LOSSES', true),
];
81 changes: 81 additions & 0 deletions src/Notifications/AbstractKillmailNotification.php
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 2021 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;

use AbstractNotification;

/**
* Class AbstractKillmailNotification.
*
* @package Seat\Notifications\Notifications
*/
class AbstractKillmailNotification extends AbstractNotification
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing abstract keyword if class is Abstract*

{
/**
* Get the Slack representation of the notification.
*
* @param $notifiable
* @return bool
*/
protected function shouldProcessKillmail($notifiable) {

$killmailConfig = config('notifications.killmails');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep config for either static content or server configuration (ie: mysql, redis, menu etc...)

What about using settings instead ? We would create a new pane in SeAT Settings area where instance owner would be able to define corporations and alliances to which the instance is related.
Other features like limited sign-in could also benefits of such settings.


$allianceId = $killmailConfig['alliance_id'];
$corporationId = $killmailConfig['corporation_id'];
$processKills = $killmailConfig['process_kills'];
$processLosses = $killmailConfig['process_losses'];

$shouldProcess = true;

if ($allianceId && $corporationId) {
if ($this->killmail->victim->alliance_id === $allianceId && $this->killmail->victim->corporation_id === $corporationId) {
$shouldProcess = $processLosses;
} elseif (in_array($allianceId, $killmail->attackers->pluck('alliance_id')) || in_array($corporationId, $killmail->attackers->pluck('character_id'))) {
$shouldProcess = $processKills;
} else {
$shouldProcess = false;
}
} elseif ($allianceId) {
if ($this->killmail->victim->alliance_id === $allianceId) {
$shouldProcess = $processLosses;
}
elseif (in_array($allianceId, $killmail->attackers->pluck('alliance_id'))) {
$shouldProcess = $processKills;
} else {
$shouldProcess = false;
}
} elseif ($corporationId) {
if ($this->killmail->victim->corporation_id === $corporationId) {
$shouldProcess = $processLosses;
}
elseif (in_array($corporationId, $killmail->attackers->pluck('character_id'))) {
$shouldProcess = $processKills;
} else {
$shouldProcess = false;
}
}

return $shouldProcess;
}
}
16 changes: 10 additions & 6 deletions src/Notifications/Characters/Mail/Killmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@

use Illuminate\Notifications\Messages\MailMessage;
use Seat\Eveapi\Models\Killmails\KillmailDetail;
use Seat\Notifications\Notifications\AbstractNotification;
use Seat\Notifications\Notifications\AbstractKillmailNotification;
use Seat\Notifications\Traits\NotificationTools;

/**
* Class Killmail.
*
* @package Seat\Notifications\Notifications\Characters
*/
class Killmail extends AbstractNotification
class Killmail extends AbstractKillmailNotification
{
use NotificationTools;

Expand All @@ -44,7 +44,7 @@ class Killmail extends AbstractNotification
/**
* Create a new notification instance.
*
* @param \Seat\Eveapi\Models\Killmails\KillmailDetail $killmail
* @param \Seat\Eveapi\Models\Killmails\KillmailDetail $killmail
*/
public function __construct(KillmailDetail $killmail)
{
Expand All @@ -55,7 +55,7 @@ public function __construct(KillmailDetail $killmail)
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
Expand All @@ -67,12 +67,16 @@ public function via($notifiable)
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{

if (! parent::shouldProcessKillmail($notifiable)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done in routing section (via) - not the parser (toX) - since a notification is already ready to be sent at this stage

return null;
}

return (new MailMessage)
->subject('Killmail Notification')
->line(
Expand All @@ -93,7 +97,7 @@ public function toMail($notifiable)
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
Expand Down
25 changes: 18 additions & 7 deletions src/Notifications/Characters/Slack/Killmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
namespace Seat\Notifications\Notifications\Characters\Slack;

use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Support\Facades\DB;
use Seat\Eveapi\Models\Corporation\CorporationInfo;
use Seat\Eveapi\Models\Killmails\KillmailDetail;
use Seat\Notifications\Notifications\AbstractNotification;
use Seat\Notifications\Notifications\AbstractKillmailNotification;
use Seat\Notifications\Traits\NotificationTools;

/**
* Class Killmail.
*
* @package Seat\Notifications\Notifications\Characters
*/
class Killmail extends AbstractNotification
class Killmail extends AbstractKillmailNotification
{
use NotificationTools;

Expand All @@ -45,7 +46,7 @@ class Killmail extends AbstractNotification
/**
* Create a new notification instance.
*
* @param \Seat\Eveapi\Models\Killmails\KillmailDetail $killmail
* @param \Seat\Eveapi\Models\Killmails\KillmailDetail $killmail
*/
public function __construct(KillmailDetail $killmail)
{
Expand All @@ -56,7 +57,7 @@ public function __construct(KillmailDetail $killmail)
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
Expand All @@ -68,12 +69,16 @@ public function via($notifiable)
/**
* Get the Slack representation of the notification.
*
* @param $notifiable
* @param $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{

if (! parent::shouldProcessKillmail($notifiable)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done in routing section (via) - not the parser (toX) - since a notification is already ready to be sent at this stage

return null;
}

$message = (new SlackMessage)
->content('A kill has been recorded for your corporation!')
->from('SeAT Kilometer', $this->typeIconUrl($this->killmail->victim->ship_type_id))
Expand All @@ -100,7 +105,13 @@ public function toSlack($notifiable)
->footerIcon('https://zkillboard.com/img/wreck.png');
});

$allied_corporation_ids = CorporationInfo::select('corporation_id')->get()->pluck('corporation_id')->toArray();
$allied_corporation_ids = DB::table('corporation_infos')
->join('users', 'corporation_infos.ceo_id', '=', 'users.main_character_id')
->where('users.active', '=', '1')
->select('corporation_id')
->get()
->pluck('corporation_id')
->toArray();

(in_array($this->killmail->victim->corporation_id, $allied_corporation_ids)) ?
$message->error() : $message->success();
Expand All @@ -111,7 +122,7 @@ public function toSlack($notifiable)
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
Expand Down