Skip to content

Commit

Permalink
Merge pull request #98 from ECFMP/discord-notifications-role-mention
Browse files Browse the repository at this point in the history
feat: discord role mentioning
  • Loading branch information
AndyTWF authored Jun 19, 2022
2 parents 5c786e5 + d69225e commit d2a537c
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 196 deletions.
1 change: 1 addition & 0 deletions app/Discord/DiscordMessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function sendMessage(MessageInterface $message): bool
'allowed_mentions' => [
'parse' => [
'users',
'roles',
],
],
]
Expand Down
16 changes: 16 additions & 0 deletions app/Discord/FlowMeasure/Content/AbstractFlowMeasureContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Discord\FlowMeasure\Content;

use App\Discord\Message\Content\ContentInterface;
use App\Models\FlowMeasure;

abstract class AbstractFlowMeasureContent implements ContentInterface
{
protected readonly FlowMeasure $flowMeasure;

public function __construct(FlowMeasure $flowMeasure)
{
$this->flowMeasure = $flowMeasure;
}
}
Original file line number Diff line number Diff line change
@@ -1,67 +1,58 @@
<?php

namespace App\Discord\FlowMeasure\Description;

use App\Discord\Message\Emoji\Emoji;
use App\Models\FlightInformationRegion;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class EventNameAndInterestedParties extends AbstractFlowMeasureDescription
{
public function description(): string
{
if (!$this->flowMeasure->event) {
return $this->formatNotifications();
}

return trim(
sprintf(
"%s\n\n%s",
$this->flowMeasure->event->name,
$this->formatNotifications()
)
);
}

private function formatNotifications(): string
{
if ($this->flowMeasure->notifiedFlightInformationRegions->isEmpty()) {
return '';
}

$firTags = $this->getNotifiedFlightInformationRegionTags();
if ($firTags->isEmpty()) {
return '';
}

return sprintf(
"**FAO**: %s\nPlease acknowledge receipt with a %s reaction.",
Arr::join($firTags->toArray(), ' '),
Emoji::WHITE_CHECK_MARK->value
);
}

private function getNotifiedFlightInformationRegionTags(): Collection
{
return $this->flowMeasure->notifiedFlightInformationRegions
->map(
fn(FlightInformationRegion $flightInformationRegion) => $flightInformationRegion->discordTags->pluck(
'tag'
)
)
->flatten()
->map(fn(string $tag) => $this->formatTag($tag))
->unique()
->values();
}

private function formatTag(string $tag): string
{
return sprintf(
'<%s>',
Str::startsWith($tag, '@') ? $tag : '@' . $tag
);
}
}
<?php

namespace App\Discord\FlowMeasure\Content;

use App\Discord\Message\Emoji\Emoji;
use App\Models\FlightInformationRegion;
use App\Models\FlowMeasure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class InterestedParties extends AbstractFlowMeasureContent
{
public static function interestedPartiesString(FlowMeasure $measure): string
{
return (new InterestedParties($measure))->toString();
}

public function toString(): string
{
if ($this->flowMeasure->notifiedFlightInformationRegions->isEmpty()) {
return '';
}

$firTags = $this->getNotifiedFlightInformationRegionTags();
if ($firTags->isEmpty()) {
return '';
}

return sprintf(
"**FAO**: %s\nPlease acknowledge receipt with a %s reaction.",
Arr::join($firTags->toArray(), ' '),
Emoji::WHITE_CHECK_MARK->value
);
}

private function getNotifiedFlightInformationRegionTags(): Collection
{
return $this->flowMeasure->notifiedFlightInformationRegions
->map(
fn(FlightInformationRegion $flightInformationRegion) => $flightInformationRegion->discordTags->pluck(
'tag'
)
)
->flatten()
->map(fn(string $tag) => $this->formatTag($tag))
->unique()
->values();
}

private function formatTag(string $tag): string
{
return sprintf(
'<%s>',
Str::startsWith($tag, '@') ? $tag : '@' . $tag
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Discord\FlowMeasure\Message;

use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties;
use App\Discord\FlowMeasure\Content\InterestedParties;
use App\Discord\FlowMeasure\Description\EventName;
use App\Discord\FlowMeasure\Field\ArrivalAirports;
use App\Discord\FlowMeasure\Field\DepartureAirports;
use App\Discord\FlowMeasure\Field\EndTime;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function __construct(FlowMeasure $measure, bool $isReissue)

public function content(): string
{
return '';
return InterestedParties::interestedPartiesString($this->measure);
}

public function embeds(): EmbedCollection
Expand All @@ -45,7 +46,7 @@ public function embeds(): EmbedCollection
? IdentifierAndActiveStatus::createReissued($this->measure)
: IdentifierAndActiveStatus::create($this->measure)
)
->withDescription(new EventNameAndInterestedParties($this->measure))
->withDescription(new EventName($this->measure))
->withField(Field::makeInline(new Restriction($this->measure)))
->withField(Field::makeInline(new StartTime($this->measure)))
->withField(Field::makeInline(new EndTime($this->measure)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Discord\FlowMeasure\Message;

use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties;
use App\Discord\FlowMeasure\Content\InterestedParties;
use App\Discord\FlowMeasure\Description\EventName;
use App\Discord\FlowMeasure\Field\ArrivalAirports;
use App\Discord\FlowMeasure\Field\DepartureAirports;
use App\Discord\FlowMeasure\Field\EndTime;
Expand Down Expand Up @@ -33,7 +34,7 @@ public function __construct(FlowMeasure $measure, bool $isReissue)

public function content(): string
{
return '';
return InterestedParties::interestedPartiesString($this->measure);
}

public function embeds(): EmbedCollection
Expand All @@ -45,7 +46,7 @@ public function embeds(): EmbedCollection
? IdentifierAndNotifiedStatus::createReissued($this->measure)
: IdentifierAndNotifiedStatus::create($this->measure)
)
->withDescription(new EventNameAndInterestedParties($this->measure))
->withDescription(new EventName($this->measure))
->withField(Field::makeInline(new Restriction($this->measure)))
->withField(Field::makeInline(new StartTime($this->measure)))
->withField(Field::makeInline(new EndTime($this->measure)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace App\Discord\FlowMeasure\Message;

use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties;
use App\Discord\FlowMeasure\Content\InterestedParties;
use App\Discord\FlowMeasure\Description\EventName;
use App\Discord\FlowMeasure\Field\ArrivalAirports;
use App\Discord\FlowMeasure\Field\DepartureAirports;
use App\Discord\FlowMeasure\Field\Restriction;
Expand All @@ -25,15 +26,15 @@ public function __construct(FlowMeasure $measure)

public function content(): string
{
return '';
return InterestedParties::interestedPartiesString($this->measure);
}

public function embeds(): EmbedCollection
{
return (new EmbedCollection())->add(
Embed::make()->withColour(Colour::WITHDRAWN)
->withTitle(new IdentifierAndWithdrawnStatus($this->measure))
->withDescription(new EventNameAndInterestedParties($this->measure))
->withDescription(new EventName($this->measure))
->withField(Field::makeInline(new Restriction($this->measure)))
->withField(Field::makeInline(new DepartureAirports($this->measure)))
->withField(Field::makeInline(new ArrivalAirports($this->measure)))
Expand Down
2 changes: 2 additions & 0 deletions tests/Discord/DiscordMessageSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function testItSendsTheMessage()
'allowed_mentions' => [
'parse' => [
'users',
'roles',
],
],
]
Expand Down Expand Up @@ -118,6 +119,7 @@ public function testItFailsToSendTheMessage()
'allowed_mentions' => [
'parse' => [
'users',
'roles',
],
],
]
Expand Down
54 changes: 54 additions & 0 deletions tests/Discord/FlowMeasure/Content/InterestedPartiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Tests\Discord\FlowMeasure\Content;

use App\Discord\FlowMeasure\Content\InterestedParties;
use App\Models\DiscordTag;
use App\Models\FlightInformationRegion;
use App\Models\FlowMeasure;
use Tests\TestCase;

class InterestedPartiesTest extends TestCase
{
public function testItReturnsInterestedParties()
{
$fir = FlightInformationRegion::factory()->has(DiscordTag::factory()->count(2))->create();
$measure = FlowMeasure::factory()->create();
$measure->notifiedFlightInformationRegions()->sync([$fir->id]);

$this->assertEquals(
sprintf(
"**FAO**: %s\nPlease acknowledge receipt with a :white_check_mark: reaction.",
$fir->discordTags->pluck('tag')->map(fn(string $tag) => '<' . $tag . '>')->join(' ')
),
InterestedParties::interestedPartiesString($measure)
);
}

public function testItReturnsInterestedPartiesWithAtSymbolIfDiscordTagMissing()
{
$fir = FlightInformationRegion::factory()->has(DiscordTag::factory()->withoutAtSymbol()->count(2))->create();
$measure = FlowMeasure::factory()->create();
$measure->notifiedFlightInformationRegions()->sync([$fir->id]);

$this->assertEquals(
sprintf(
"**FAO**: %s\nPlease acknowledge receipt with a :white_check_mark: reaction.",
$fir->discordTags->pluck('tag')->map(fn(string $tag) => '<@' . $tag . '>')->join(' ')
),
InterestedParties::interestedPartiesString($measure)
);
}

public function testItReturnsBlankIfNoInterestedParties()
{
$fir = FlightInformationRegion::factory()->create();
$measure = FlowMeasure::factory()->create();
$measure->notifiedFlightInformationRegions()->sync([$fir->id]);

$this->assertEquals(
'',
InterestedParties::interestedPartiesString($measure)
);
}
}
Loading

0 comments on commit d2a537c

Please sign in to comment.