Use shouldSend for NewRequestFillApprove notifications

This commit is contained in:
Jay Sizzla
2024-12-18 13:48:28 +00:00
parent 9e1dd2acf7
commit 034fecac9f
3 changed files with 335 additions and 3 deletions

View File

@@ -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));

View File

@@ -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.
*

View File

@@ -0,0 +1,314 @@
<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @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);
});