Skip to content

Commit

Permalink
Merge pull request #232 from ECFMP/division-discord-multiple-tags
Browse files Browse the repository at this point in the history
Division discord multiple tags
  • Loading branch information
AndyTWF authored Aug 5, 2022
2 parents 4bb65c5 + 4d26906 commit 99635b7
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 45 deletions.
11 changes: 7 additions & 4 deletions app/Discord/FlowMeasure/Content/DivisionWebhookRecipients.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
namespace App\Discord\FlowMeasure\Content;

use App\Discord\Message\Tag\TagInterface;
use Illuminate\Support\Collection;

class DivisionWebhookRecipients implements FlowMeasureRecipientsInterface
{
private readonly TagInterface $tag;
private readonly Collection $tags;

public function __construct(TagInterface $tag)
public function __construct(Collection $tags)
{
$this->tag = $tag;
$this->tags = $tags;
}

public function toString(): string
{
return (string)$this->tag;
return $this->tags
->map(fn (TagInterface $tag) => (string)$tag)
->join(' ');
}
}
15 changes: 12 additions & 3 deletions app/Discord/FlowMeasure/Content/FlowMeasureRecipientsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,19 @@ private function ecfmpRecipients(PendingMessageInterface $pendingMessage): FlowM

private function divisionRecipients(PendingMessageInterface $pendingMessage): FlowMeasureRecipientsInterface
{
$divisionWebhook = DivisionDiscordWebhook::find($pendingMessage->webhook()->id());
$recipients = DivisionDiscordWebhook::find($pendingMessage->webhook()->id())
->flightInformationRegions
->filter(
fn (FlightInformationRegion $flightInformationRegion) => $pendingMessage
->flowMeasure()
->notifiedFlightInformationRegions
->firstWhere(fn (FlightInformationRegion $notifiedFir) => $notifiedFir->id === $flightInformationRegion->id)
)
->filter(fn (FlightInformationRegion $flightInformationRegion) => !empty($flightInformationRegion->pivot->tag))
->map(fn (FlightInformationRegion $flightInformationRegion) => new Tag($flightInformationRegion->pivot));

return empty($divisionWebhook->tag)
return $recipients->isEmpty()
? new NoRecipients()
: new DivisionWebhookRecipients(new Tag($divisionWebhook));
: new DivisionWebhookRecipients($recipients);
}
}
16 changes: 6 additions & 10 deletions app/Models/DivisionDiscordWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

namespace App\Models;

use App\Discord\Message\Tag\TagProviderInterface;
use App\Discord\Webhook\WebhookInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class DivisionDiscordWebhook extends Model implements WebhookInterface, TagProviderInterface
class DivisionDiscordWebhook extends Model implements WebhookInterface
{
use HasFactory;
use SoftDeletes;

protected $fillable = [
'url',
'description',
'tag',
'description'
];

public function flightInformationRegions(): BelongsToMany
{
return $this->belongsToMany(FlightInformationRegion::class);
return $this->belongsToMany(FlightInformationRegion::class)
->withTimestamps()
->withPivot('tag')
->using(DivisionDiscordWebhookFlightInformationRegion::class);
}

public function id(): ?int
Expand All @@ -39,9 +40,4 @@ public function description(): string
{
return $this->description;
}

public function rawTagString(): string
{
return $this->tag;
}
}
18 changes: 18 additions & 0 deletions app/Models/DivisionDiscordWebhookFlightInformationRegion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

use App\Discord\Message\Tag\TagProviderInterface;
use Illuminate\Database\Eloquent\Relations\Pivot;

class DivisionDiscordWebhookFlightInformationRegion extends Pivot implements TagProviderInterface
{
public $incrementing = true;

public $timestamps = true;

public function rawTagString(): string
{
return (string)$this->tag;
}
}
5 changes: 4 additions & 1 deletion app/Models/FlightInformationRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ protected function identifierName(): Attribute

public function divisionDiscordWebhooks(): BelongsToMany
{
return $this->belongsToMany(DivisionDiscordWebhook::class);
return $this->belongsToMany(DivisionDiscordWebhook::class)
->withPivot('tag')
->withTimestamps()
->using(DivisionDiscordWebhookFlightInformationRegion::class);
}
}
8 changes: 1 addition & 7 deletions database/factories/DivisionDiscordWebhookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ public function definition()
{
return [
'url' => $this->faker->url(),
'description' => $this->faker->sentence(3),
'tag' => sprintf('@%s', $this->faker->unique()->numberBetween(0, PHP_INT_MAX)),
'description' => $this->faker->sentence(3)
];
}

public function withNoTag(): static
{
return $this->state(fn (array $attributes) => ['tag' => '']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('division_discord_webhook_flight_information_region', function (Blueprint $table) {
$table->string('tag')
->nullable()
->after('flight_information_region_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('division_discord_webhook_flight_information_region', function (Blueprint $table) {
//
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use App\Models\DivisionDiscordWebhook;
use App\Models\FlightInformationRegion;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Migrations\Migration;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Migrate the existing tag values
FlightInformationRegion::all()
->each(function (FlightInformationRegion $fir) {
$fir->divisionDiscordWebhooks
->each(function (DivisionDiscordWebhook $discordWebhook) use ($fir) {
$fir->divisionDiscordWebhooks()->updateExistingPivot(
$discordWebhook->id,
['tag' => $discordWebhook->tag]
);
});
});

// Deduplicate them by URL
DivisionDiscordWebhook::all()
->groupBy('url')
->reject(fn (Collection $webhooks) => $webhooks->count() === 1)
->each(function (Collection $webhooks) {
// Get the one we're going to keep
$webhookToKeep = $webhooks->shift();

$webhooks->each(function (DivisionDiscordWebhook $webhook) use ($webhookToKeep) {
// Migrate the FIRs to the main webhook
$webhook->flightInformationRegions
->each(function (FlightInformationRegion $flightInformationRegion) use ($webhook, $webhookToKeep) {
$flightInformationRegion->divisionDiscordWebhooks()
->attach($webhookToKeep->id, ['tag' => $webhook->tag]);
});

// Delete the webhooks
$webhook->forceDelete();
});
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('division_discord_webhooks', function (Blueprint $table) {
$table->dropColumn('tag');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class DivisionWebhookRecipientsTest extends TestCase
{
public function testItReturnsRecipients()
{
$this->assertSame(
'<@1234>',
(new DivisionWebhookRecipients(new Tag(new DiscordTag(['tag' => '1234']))))->toString()
$this->assertEquals(
'<@1234> <@5678>',
(new DivisionWebhookRecipients(
collect([new Tag(new DiscordTag(['tag' => '1234'])), new Tag(new DiscordTag(['tag' => '5678']))])
))->toString()
);
}
}
Loading

0 comments on commit 99635b7

Please sign in to comment.