-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from ECFMP/discord-notifications-role-mention
feat: discord role mentioning
- Loading branch information
Showing
12 changed files
with
173 additions
and
196 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
16 changes: 16 additions & 0 deletions
16
app/Discord/FlowMeasure/Content/AbstractFlowMeasureContent.php
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,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; | ||
} | ||
} |
125 changes: 58 additions & 67 deletions
125
...ription/EventNameAndInterestedParties.php → ...FlowMeasure/Content/InterestedParties.php
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 |
---|---|---|
@@ -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 | ||
); | ||
} | ||
} |
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
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
54 changes: 54 additions & 0 deletions
54
tests/Discord/FlowMeasure/Content/InterestedPartiesTest.php
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,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) | ||
); | ||
} | ||
} |
Oops, something went wrong.