Skip to content
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
12 changes: 5 additions & 7 deletions app/Console/Commands/ResendNewPluginNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginAvailable;
use App\Notifications\NewPluginsAvailable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;

Expand All @@ -14,7 +14,7 @@ class ResendNewPluginNotifications extends Command
{plugins* : Plugin names (vendor/package) to resend notifications for}
{--dry-run : Preview what would happen without sending notifications}';

protected $description = 'Resend NewPluginAvailable notifications to opted-in users for specified plugins';
protected $description = 'Email opted-in users a single combined digest for the specified plugins';

public function handle(): int
{
Expand Down Expand Up @@ -57,13 +57,11 @@ public function handle(): int
return Command::SUCCESS;
}

foreach ($plugins as $plugin) {
$pluginRecipients = $recipients->where('id', '!=', $plugin->user_id);
Notification::send($recipients, new NewPluginsAvailable($plugins));

Notification::send($pluginRecipients, new NewPluginAvailable($plugin));
$plugins->each->update(['new_plugin_notified_at' => now()]);

$this->info("Sent NewPluginAvailable for {$plugin->name} to {$pluginRecipients->count()} users.");
}
$this->info("Sent a single digest covering {$plugins->count()} plugin(s) to {$recipients->count()} users.");

$this->newLine();
$this->info('Done. All notifications queued.');
Expand Down
23 changes: 23 additions & 0 deletions app/Filament/Resources/PluginResource/Pages/ListPlugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Filament\Resources\PluginResource\Pages;

use App\Filament\Resources\PluginResource;
use App\Jobs\SendPendingPluginEmailDigest;
use App\Models\Plugin;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;

class ListPlugins extends ListRecords
Expand All @@ -13,6 +16,26 @@ class ListPlugins extends ListRecords
protected function getHeaderActions(): array
{
return [
Actions\Action::make('sendPendingNewPluginNotifications')
->label(fn () => 'Email Subscribers ('.Plugin::query()->pendingNewPluginNotification()->count().')')
->icon('heroicon-o-envelope')
->color('warning')
->visible(fn () => Plugin::query()->pendingNewPluginNotification()->count() > 0)
->requiresConfirmation()
->modalHeading('Email Pending Plugin Notifications')
->modalDescription(fn () => 'This will email every subscribed user a single digest covering the '
.Plugin::query()->pendingNewPluginNotification()->count()
.' plugin(s) approved since the last digest.')
->action(function (): void {
SendPendingPluginEmailDigest::dispatch();

Notification::make()
->title('Digest queued')
->body('The digest email is being sent to subscribed users.')
->success()
->send();
}),

Actions\CreateAction::make(),
];
}
Expand Down
42 changes: 42 additions & 0 deletions app/Jobs/SendPendingPluginEmailDigest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Jobs;

use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginsAvailable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SendPendingPluginEmailDigest implements ShouldQueue
{
use Queueable;

public function handle(): void
{
$plugins = Plugin::query()->pendingNewPluginNotification()->get();

if ($plugins->isEmpty()) {
return;
}

$recipients = User::query()
->whereNotNull('email_verified_at')
->where('receives_new_plugin_notifications', true)
->get();

foreach ($recipients as $recipient) {
$notifiablePlugins = $plugins->reject(fn (Plugin $plugin) => $plugin->user_id === $recipient->id);

if ($notifiablePlugins->isEmpty()) {
continue;
}

$recipient->notify(new NewPluginsAvailable($notifiablePlugins));
}

Plugin::query()
->whereIn('id', $plugins->pluck('id'))
->update(['new_plugin_notified_at' => now()]);
}
}
14 changes: 14 additions & 0 deletions app/Models/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ protected function featured(Builder $query): Builder
return $query->where('featured', true);
}

/**
* Approved plugins that have not yet been included in an emailed "new plugins" digest.
*
* @param Builder<Plugin> $query
* @return Builder<Plugin>
*/
#[Scope]
protected function pendingNewPluginNotification(Builder $query): Builder
{
return $query->where('status', PluginStatus::Approved)
->whereNull('new_plugin_notified_at');
}

public function getPackagistUrl(): string
{
return "https://packagist.org/packages/{$this->name}";
Expand Down Expand Up @@ -814,6 +827,7 @@ protected function casts(): array
'type' => PluginType::class,
'tier' => PluginTier::class,
'approved_at' => 'datetime',
'new_plugin_notified_at' => 'datetime',
'featured' => 'boolean',
'is_active' => 'boolean',
'is_official' => 'boolean',
Expand Down
73 changes: 73 additions & 0 deletions app/Notifications/NewPluginsAvailable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Notifications;

use App\Http\Controllers\NotificationUnsubscribeController;
use App\Models\Plugin;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Collection;

class NewPluginsAvailable extends Notification implements ShouldQueue
{
use Queueable;

/**
* @param Collection<int, Plugin> $plugins
*/
public function __construct(
public Collection $plugins
) {}

/**
* @return array<int, string>
*/
public function via(object $notifiable): array
{
if (! $notifiable->receives_new_plugin_notifications) {
return [];
}

return ['mail', 'database'];
}

public function toMail(object $notifiable): MailMessage
{
/** @var User $notifiable */
$unsubscribeUrl = NotificationUnsubscribeController::signedUnsubscribeUrl($notifiable);

$count = $this->plugins->count();

$mail = (new MailMessage)
->subject($count === 1
? "New Plugin: {$this->plugins->first()->name}"
: "{$count} New Plugins on the NativePHP Marketplace")
->greeting($count === 1 ? 'A new plugin is available!' : 'New plugins are available!')
->line($count === 1
? 'The following plugin has just been added to the NativePHP Plugin Marketplace:'
: 'The following plugins have just been added to the NativePHP Plugin Marketplace:');

foreach ($this->plugins as $plugin) {
$mail->line('**['.$plugin->name.']('.route('plugins.show', $plugin->routeParams()).')**');
}

return $mail->line('[Unsubscribe from new plugin notifications]('.$unsubscribeUrl.').');
}

/**
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'title' => $this->plugins->count() === 1
? "New Plugin: {$this->plugins->first()->name}"
: "{$this->plugins->count()} New Plugins on the NativePHP Marketplace",
'plugin_ids' => $this->plugins->pluck('id')->all(),
'plugin_names' => $this->plugins->pluck('name')->all(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

return new class extends Migration
{
public function up(): void
{
Schema::table('plugins', function (Blueprint $table) {
$table->timestamp('new_plugin_notified_at')->nullable()->after('approved_at');
});
}

public function down(): void
{
Schema::table('plugins', function (Blueprint $table) {
$table->dropColumn('new_plugin_notified_at');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tests\Feature\Filament;

use App\Filament\Resources\PluginResource\Pages\ListPlugins;
use App\Jobs\SendPendingPluginEmailDigest;
use App\Models\Plugin;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Livewire\Livewire;
use Tests\TestCase;

class SendPendingNewPluginNotificationsActionTest extends TestCase
{
use RefreshDatabase;

private User $admin;

protected function setUp(): void
{
parent::setUp();

$this->admin = User::factory()->create(['email' => 'admin@test.com']);
config(['filament.users' => ['admin@test.com']]);
}

public function test_action_hidden_when_no_plugins_are_pending(): void
{
Livewire::actingAs($this->admin)
->test(ListPlugins::class)
->assertActionHidden('sendPendingNewPluginNotifications');
}

public function test_action_visible_when_plugins_are_pending(): void
{
Plugin::factory()->approved()->create();

Livewire::actingAs($this->admin)
->test(ListPlugins::class)
->assertActionVisible('sendPendingNewPluginNotifications');
}

public function test_action_dispatches_the_digest_job(): void
{
Bus::fake([SendPendingPluginEmailDigest::class]);

Plugin::factory()->approved()->create();

Livewire::actingAs($this->admin)
->test(ListPlugins::class)
->callAction('sendPendingNewPluginNotifications')
->assertNotified();

Bus::assertDispatched(SendPendingPluginEmailDigest::class);
}
}
Loading