diff --git a/app/Discord/DiscordMessageSender.php b/app/Discord/DiscordMessageSender.php index 03441456..5aea745f 100644 --- a/app/Discord/DiscordMessageSender.php +++ b/app/Discord/DiscordMessageSender.php @@ -26,6 +26,7 @@ public function sendMessage(MessageInterface $message): bool 'allowed_mentions' => [ 'parse' => [ 'users', + 'roles', ], ], ] diff --git a/app/Discord/FlowMeasure/Content/AbstractFlowMeasureContent.php b/app/Discord/FlowMeasure/Content/AbstractFlowMeasureContent.php new file mode 100644 index 00000000..8d3b889d --- /dev/null +++ b/app/Discord/FlowMeasure/Content/AbstractFlowMeasureContent.php @@ -0,0 +1,16 @@ +flowMeasure = $flowMeasure; + } +} diff --git a/app/Discord/FlowMeasure/Description/EventNameAndInterestedParties.php b/app/Discord/FlowMeasure/Content/InterestedParties.php similarity index 69% rename from app/Discord/FlowMeasure/Description/EventNameAndInterestedParties.php rename to app/Discord/FlowMeasure/Content/InterestedParties.php index 14b4b1ac..62184928 100644 --- a/app/Discord/FlowMeasure/Description/EventNameAndInterestedParties.php +++ b/app/Discord/FlowMeasure/Content/InterestedParties.php @@ -1,67 +1,58 @@ -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 - ); - } -} +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 + ); + } +} diff --git a/app/Discord/FlowMeasure/Message/FlowMeasureActivatedMessage.php b/app/Discord/FlowMeasure/Message/FlowMeasureActivatedMessage.php index 31c20049..228c5981 100644 --- a/app/Discord/FlowMeasure/Message/FlowMeasureActivatedMessage.php +++ b/app/Discord/FlowMeasure/Message/FlowMeasureActivatedMessage.php @@ -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; @@ -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 @@ -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))) diff --git a/app/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessage.php b/app/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessage.php index 82497587..0a6a487e 100644 --- a/app/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessage.php +++ b/app/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessage.php @@ -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; @@ -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 @@ -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))) diff --git a/app/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessage.php b/app/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessage.php index 490a1ef8..28402478 100644 --- a/app/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessage.php +++ b/app/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessage.php @@ -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; @@ -25,7 +26,7 @@ public function __construct(FlowMeasure $measure) public function content(): string { - return ''; + return InterestedParties::interestedPartiesString($this->measure); } public function embeds(): EmbedCollection @@ -33,7 +34,7 @@ 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))) diff --git a/tests/Discord/DiscordMessageSenderTest.php b/tests/Discord/DiscordMessageSenderTest.php index fec1291d..3dad6670 100644 --- a/tests/Discord/DiscordMessageSenderTest.php +++ b/tests/Discord/DiscordMessageSenderTest.php @@ -83,6 +83,7 @@ public function testItSendsTheMessage() 'allowed_mentions' => [ 'parse' => [ 'users', + 'roles', ], ], ] @@ -118,6 +119,7 @@ public function testItFailsToSendTheMessage() 'allowed_mentions' => [ 'parse' => [ 'users', + 'roles', ], ], ] diff --git a/tests/Discord/FlowMeasure/Content/InterestedPartiesTest.php b/tests/Discord/FlowMeasure/Content/InterestedPartiesTest.php new file mode 100644 index 00000000..7375c88f --- /dev/null +++ b/tests/Discord/FlowMeasure/Content/InterestedPartiesTest.php @@ -0,0 +1,54 @@ +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) + ); + } +} diff --git a/tests/Discord/FlowMeasure/Description/EventNameAndInterestedPartiesTest.php b/tests/Discord/FlowMeasure/Description/EventNameAndInterestedPartiesTest.php deleted file mode 100644 index 116d5f00..00000000 --- a/tests/Discord/FlowMeasure/Description/EventNameAndInterestedPartiesTest.php +++ /dev/null @@ -1,105 +0,0 @@ -description(); - } - - public function testItReturnsInterestedPartiesIfNoEvent() - { - $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(' ') - ), - $this->getContent($measure) - ); - } - - public function testItReturnsInterestedPartiesWithAtSymbolIfDiscordTagMissingAndNoEvent() - { - $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(' ') - ), - $this->getContent($measure) - ); - } - - public function testItReturnsBlankIfNoInterestedPartiesAndNoEvent() - { - $fir = FlightInformationRegion::factory()->create(); - $measure = FlowMeasure::factory()->create(); - $measure->notifiedFlightInformationRegions()->sync([$fir->id]); - - $this->assertEquals( - '', - $this->getContent($measure) - ); - } - - public function testItReturnsInterestedPartiesAndEvent() - { - $fir = FlightInformationRegion::factory()->has(DiscordTag::factory()->count(2))->create(); - $measure = FlowMeasure::factory()->withEvent()->create(); - $measure->notifiedFlightInformationRegions()->sync([$fir->id]); - - $this->assertEquals( - sprintf( - "%s\n\n**FAO**: %s\nPlease acknowledge receipt with a :white_check_mark: reaction.", - $measure->event->name, - $fir->discordTags->pluck('tag')->map(fn(string $tag) => '<' . $tag . '>')->join(' ') - ), - $this->getContent($measure) - ); - } - - public function testItReturnsInterestedPartiesWithAtSymbolIfDiscordTagMissingAndEvent() - { - $fir = FlightInformationRegion::factory()->has( - DiscordTag::factory()->withoutAtSymbol()->count(2) - )->create(); - $measure = FlowMeasure::factory()->withEvent()->create(); - $measure->notifiedFlightInformationRegions()->sync([$fir->id]); - - $this->assertEquals( - sprintf( - "%s\n\n**FAO**: %s\nPlease acknowledge receipt with a :white_check_mark: reaction.", - $measure->event->name, - $fir->discordTags->pluck('tag')->map(fn(string $tag) => '<@' . $tag . '>')->join(' ') - ), - $this->getContent($measure) - ); - } - - public function testItReturnsJustEventIfNoInterestedParties() - { - $fir = FlightInformationRegion::factory()->create(); - $measure = FlowMeasure::factory()->withEvent()->create(); - $measure->notifiedFlightInformationRegions()->sync([$fir->id]); - - $this->assertEquals( - $measure->event->name, - $this->getContent($measure) - ); - } -} diff --git a/tests/Discord/FlowMeasure/Message/FlowMeasureActivatedMessageTest.php b/tests/Discord/FlowMeasure/Message/FlowMeasureActivatedMessageTest.php index 2e28b648..a9889aec 100644 --- a/tests/Discord/FlowMeasure/Message/FlowMeasureActivatedMessageTest.php +++ b/tests/Discord/FlowMeasure/Message/FlowMeasureActivatedMessageTest.php @@ -2,7 +2,7 @@ namespace Tests\Discord\FlowMeasure\Message; -use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties; +use App\Discord\FlowMeasure\Description\EventName; use App\Discord\FlowMeasure\Message\FlowMeasureActivatedMessage; use App\Discord\Message\Embed\Colour; use App\Models\DiscordTag; @@ -19,12 +19,17 @@ public function setUp(): void Carbon::setTestNow(Carbon::parse('2022-05-22T14:59:23Z')); } - public function testItHasNoContent() + public function testItHasFaoContent() { + $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(' ') + ), (new FlowMeasureActivatedMessage($measure, false))->content() ); } @@ -44,7 +49,7 @@ public function testItHasEmbeds() [ 'title' => $measure->identifier . ' - ' . 'Active', 'color' => Colour::ACTIVATED->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]', @@ -108,7 +113,7 @@ public function testItHasEmbedsWhenReissued() [ 'title' => $measure->identifier . ' - ' . 'Active (Reissued)', 'color' => Colour::ACTIVATED->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]', diff --git a/tests/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessageTest.php b/tests/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessageTest.php index 15969ad7..71927708 100644 --- a/tests/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessageTest.php +++ b/tests/Discord/FlowMeasure/Message/FlowMeasureNotifiedMessageTest.php @@ -2,7 +2,7 @@ namespace Tests\Discord\FlowMeasure\Message; -use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties; +use App\Discord\FlowMeasure\Description\EventName; use App\Discord\FlowMeasure\Message\FlowMeasureNotifiedMessage; use App\Discord\Message\Embed\Colour; use App\Models\DiscordTag; @@ -19,12 +19,17 @@ public function setUp(): void Carbon::setTestNow(Carbon::parse('2022-05-22T13:54:23Z')); } - public function testItHasNoContent() + public function testItHasFaoContent() { + $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(' ') + ), (new FlowMeasureNotifiedMessage($measure, false))->content() ); } @@ -45,7 +50,7 @@ public function testItHasEmbeds() [ 'title' => $measure->identifier . ' - ' . 'Notified', 'color' => Colour::NOTIFIED->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]', @@ -110,7 +115,7 @@ public function testItHasEmbedsWhenReissued() [ 'title' => $measure->identifier . ' - ' . 'Notified (Reissued)', 'color' => Colour::NOTIFIED->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]', diff --git a/tests/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessageTest.php b/tests/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessageTest.php index 037f68d7..c6ab15e5 100644 --- a/tests/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessageTest.php +++ b/tests/Discord/FlowMeasure/Message/FlowMeasureWithdrawnMessageTest.php @@ -2,7 +2,7 @@ namespace Tests\Discord\FlowMeasure\Message; -use App\Discord\FlowMeasure\Description\EventNameAndInterestedParties; +use App\Discord\FlowMeasure\Description\EventName; use App\Discord\FlowMeasure\Message\FlowMeasureWithdrawnMessage; use App\Discord\Message\Embed\Colour; use App\Models\DiscordTag; @@ -12,12 +12,17 @@ class FlowMeasureWithdrawnMessageTest extends TestCase { - public function testItHasNoContent() + public function testItHasFaoContent() { + $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(' ') + ), (new FlowMeasureWithdrawnMessage($measure))->content() ); } @@ -36,7 +41,7 @@ public function testItHasEmbedsWhenWithinActivePeriod() [ 'title' => $measure->identifier . ' - ' . 'Withdrawn', 'color' => Colour::WITHDRAWN->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]', @@ -75,7 +80,7 @@ public function testItHasEmbedsWhenExpired() [ 'title' => $measure->identifier . ' - ' . 'Withdrawn', 'color' => Colour::WITHDRAWN->value, - 'description' => (new EventNameAndInterestedParties($measure))->description(), + 'description' => (new EventName($measure))->description(), 'fields' => [ [ 'name' => 'Minimum Departure Interval [MDI]',