From 034fecac9f7aff1572aed7bb1f261d05ea39be65 Mon Sep 17 00:00:00 2001 From: Jay Sizzla Date: Wed, 18 Dec 2024 13:48:28 +0000 Subject: [PATCH] Use shouldSend for NewRequestFillApprove notifications --- .../ApprovedRequestFillController.php | 4 +- app/Notifications/NewRequestFillApprove.php | 20 ++ .../NewRequestFillApproveNotificationTest.php | 314 ++++++++++++++++++ 3 files changed, 335 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/Notifications/NewRequestFillApproveNotificationTest.php diff --git a/app/Http/Controllers/ApprovedRequestFillController.php b/app/Http/Controllers/ApprovedRequestFillController.php index 1bd845417..9ba053788 100644 --- a/app/Http/Controllers/ApprovedRequestFillController.php +++ b/app/Http/Controllers/ApprovedRequestFillController.php @@ -74,9 +74,7 @@ class ApprovedRequestFillController extends Controller ); } - if ($filler->acceptsNotification($approver, $filler, 'request', 'show_request_fill_approve')) { - $filler->notify(new NewRequestFillApprove($torrentRequest)); - } + $filler->notify(new NewRequestFillApprove($torrentRequest)); return to_route('requests.show', ['torrentRequest' => $torrentRequest]) ->with('success', \sprintf(trans('request.approved-user'), $torrentRequest->name, $torrentRequest->filled_anon ? 'Anonymous' : $filler->username)); diff --git a/app/Notifications/NewRequestFillApprove.php b/app/Notifications/NewRequestFillApprove.php index 1e70ed58c..eb3d6ec0d 100644 --- a/app/Notifications/NewRequestFillApprove.php +++ b/app/Notifications/NewRequestFillApprove.php @@ -16,6 +16,7 @@ declare(strict_types=1); namespace App\Notifications; +use App\Models\User; use App\Models\TorrentRequest; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -42,6 +43,25 @@ class NewRequestFillApprove extends Notification implements ShouldQueue return ['database']; } + /** + * Determine if the notification should be sent. + */ + public function shouldSend(User $notifiable): bool + { + if ($this->torrentRequest->approver->id === $notifiable->id || + $notifiable->notification?->block_notifications == 1) { + return false; + } + + if (!$notifiable->notification?->show_request_fill_approve) { + return false; + } + + // If the sender's group ID is found in the "Block all notifications from the selected groups" array, + // the expression will return false. + return ! \in_array($this->torrentRequest->approver->group_id, $notifiable->notification->json_request_groups, true); + } + /** * Get the array representation of the notification. * diff --git a/tests/Feature/Notifications/NewRequestFillApproveNotificationTest.php b/tests/Feature/Notifications/NewRequestFillApproveNotificationTest.php new file mode 100644 index 000000000..7e991fd78 --- /dev/null +++ b/tests/Feature/Notifications/NewRequestFillApproveNotificationTest.php @@ -0,0 +1,314 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +use App\Models\Bot; +use App\Models\Chatroom; +use App\Models\Category; +use App\Models\Group; +use App\Models\Resolution; +use App\Models\TorrentRequest; +use App\Models\Torrent; +use App\Models\Type; +use App\Models\User; +use App\Models\UserNotification; +use App\Notifications\NewRequestFillApprove; +use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Notification; + +uses(RefreshDatabase::class); + +test('accept a request fill creates a notification for the filler', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $requester = User::factory()->create(); + $filler = User::factory()->create(); + + $fillerNotificationSettings = UserNotification::factory()->create([ + 'user_id' => $filler->id, + 'block_notifications' => 0, + 'show_request_fill_approve' => 1, + ]); + + $category = Category::factory()->create(); + $type = Type::factory()->create(); + $resolution = Resolution::factory()->create(); + + $torrent = Torrent::factory()->create([ + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + ]); + + $torrentRequest = TorrentRequest::factory()->create([ + 'anon' => false, + 'user_id' => $requester->id, + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + 'torrent_id' => $torrent->id, + 'claimed' => true, + 'filled_by' => $filler->id, + 'filled_when' => now(), + 'approved_by' => null, + 'approved_when' => null, + ]); + + $response = $this->actingAs($requester)->post(route('requests.approved_fills.store', [$torrentRequest])); + + $response->assertRedirect(route('requests.show', $torrentRequest)); + + Notification::assertSentTo( + [$filler], + NewRequestFillApprove::class + ); + Notification::assertCount(1); +}); + +test('accept a request fill creates a notification for the filler when request accept notifications are not disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $randomGroup = Group::factory()->create(); + + $requester = User::factory()->create(); + $filler = User::factory()->create(); + + $fillerNotificationSettings = UserNotification::factory()->create([ + 'user_id' => $filler->id, + 'block_notifications' => 0, + 'show_request_fill_approve' => 1, + 'json_request_groups' => [$randomGroup->id], + ]); + + $category = Category::factory()->create(); + $type = Type::factory()->create(); + $resolution = Resolution::factory()->create(); + + $torrent = Torrent::factory()->create([ + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + ]); + + $torrentRequest = TorrentRequest::factory()->create([ + 'anon' => false, + 'user_id' => $requester->id, + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + 'torrent_id' => $torrent->id, + 'claimed' => true, + 'filled_by' => $filler->id, + 'filled_when' => now(), + 'approved_by' => null, + 'approved_when' => null, + ]); + + $response = $this->actingAs($requester)->post(route('requests.approved_fills.store', [$torrentRequest])); + + $response->assertRedirect(route('requests.show', $torrentRequest)); + + Notification::assertSentTo( + [$filler], + NewRequestFillApprove::class + ); + Notification::assertCount(1); +}); + +test('accept a request fill creates a notification for the filler when all notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $randomGroup = Group::factory()->create(); + + $requester = User::factory()->create(); + $filler = User::factory()->create(); + + $fillerNotificationSettings = UserNotification::factory()->create([ + 'user_id' => $filler->id, + 'block_notifications' => 1, + 'show_request_fill_approve' => 1, + ]); + + $category = Category::factory()->create(); + $type = Type::factory()->create(); + $resolution = Resolution::factory()->create(); + + $torrent = Torrent::factory()->create([ + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + ]); + + $torrentRequest = TorrentRequest::factory()->create([ + 'anon' => false, + 'user_id' => $requester->id, + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + 'torrent_id' => $torrent->id, + 'claimed' => true, + 'filled_by' => $filler->id, + 'filled_when' => now(), + 'approved_by' => null, + 'approved_when' => null, + ]); + + $response = $this->actingAs($requester)->post(route('requests.approved_fills.store', [$torrentRequest])); + + $response->assertRedirect(route('requests.show', $torrentRequest)); + + Notification::assertCount(0); +}); + +test('accept a request fill creates a notification for the filler when fill approve notifications are disabled', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $requester = User::factory()->create(); + $filler = User::factory()->create(); + + $fillerNotificationSettings = UserNotification::factory()->create([ + 'user_id' => $filler->id, + 'block_notifications' => 0, + 'show_request_fill_approve' => 0, + ]); + + $category = Category::factory()->create(); + $type = Type::factory()->create(); + $resolution = Resolution::factory()->create(); + + $torrent = Torrent::factory()->create([ + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + ]); + + $torrentRequest = TorrentRequest::factory()->create([ + 'anon' => false, + 'user_id' => $requester->id, + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + 'torrent_id' => $torrent->id, + 'claimed' => true, + 'filled_by' => $filler->id, + 'filled_when' => now(), + 'approved_by' => null, + 'approved_when' => null, + ]); + + $response = $this->actingAs($requester)->post(route('requests.approved_fills.store', [$torrentRequest])); + + $response->assertRedirect(route('requests.show', $torrentRequest)); + + Notification::assertCount(0); +}); + +test('accept a request fill creates a notification for the filler when reqeust notifications are disabled for specific group', function (): void { + Notification::fake(); + + // Required for ChatRepository() + $this->seed(UsersTableSeeder::class); + + $bot = Bot::factory()->create([ + 'command' => 'Systembot', + ]); + $chat = Chatroom::factory()->create([ + 'name' => config('chat.system_chatroom'), + ]); + + $group = Group::factory()->create(); + + $requester = User::factory()->create([ + 'group_id' => $group->id, + ]); + $filler = User::factory()->create(); + + $fillerNotificationSettings = UserNotification::factory()->create([ + 'user_id' => $filler->id, + 'block_notifications' => 0, + 'show_request_fill_approve' => 1, + 'json_request_groups' => [$group->id], + ]); + + $category = Category::factory()->create(); + $type = Type::factory()->create(); + $resolution = Resolution::factory()->create(); + + $torrent = Torrent::factory()->create([ + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + ]); + + $torrentRequest = TorrentRequest::factory()->create([ + 'anon' => false, + 'user_id' => $requester->id, + 'category_id' => $category->id, + 'type_id' => $type->id, + 'resolution_id' => $resolution->id, + 'torrent_id' => $torrent->id, + 'claimed' => true, + 'filled_by' => $filler->id, + 'filled_when' => now(), + 'approved_by' => null, + 'approved_when' => null, + ]); + + $response = $this->actingAs($requester)->post(route('requests.approved_fills.store', [$torrentRequest])); + + $response->assertRedirect(route('requests.show', $torrentRequest)); + + Notification::assertCount(0); +});