Skip to content

Commit ac87c92

Browse files
committed
Use shouldSend for NewRequestClaim notifications
1 parent 967d61c commit ac87c92

File tree

3 files changed

+303
-3
lines changed

3 files changed

+303
-3
lines changed

app/Http/Controllers/ClaimController.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public function store(StoreTorrentRequestClaimRequest $request, TorrentRequest $
4747

4848
$requester = $torrentRequest->user;
4949

50-
if ($requester->acceptsNotification($request->user(), $requester, 'request', 'show_request_claim')) {
51-
$requester->notify(new NewRequestClaim($claim));
52-
}
50+
$requester->notify(new NewRequestClaim($claim));
5351

5452
return to_route('requests.show', ['torrentRequest' => $torrentRequest])
5553
->withSuccess(trans('request.claimed-success'));

app/Notifications/NewRequestClaim.php

+27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace App\Notifications;
1818

19+
use App\Models\User;
1920
use App\Models\TorrentRequestClaim;
2021
use Illuminate\Bus\Queueable;
2122
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -42,6 +43,32 @@ public function via(object $notifiable): array
4243
return ['database'];
4344
}
4445

46+
/**
47+
* Determine if the notification should be sent.
48+
*
49+
* @return bool
50+
*/
51+
public function shouldSend(User $notifiable): bool
52+
{
53+
$targetGroup = 'json_request_groups';
54+
55+
if ($notifiable->notification?->block_notifications == 1) {
56+
return false;
57+
}
58+
59+
if (!$notifiable->notification?->show_request_claim) {
60+
return false;
61+
}
62+
63+
if (\is_array($notifiable->notification->$targetGroup)) {
64+
// If the sender's group ID is found in the "Block all notifications from the selected groups" array,
65+
// the expression will return false.
66+
return !\in_array($this->claim->user->group->id, $notifiable->notification->$targetGroup, true);
67+
}
68+
69+
return true;
70+
}
71+
4572
/**
4673
* Get the array representation of the notification.
4774
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* NOTICE OF LICENSE.
7+
*
8+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
9+
* The details is bundled with this project in the file LICENSE.txt.
10+
*
11+
* @project UNIT3D Community Edition
12+
*
13+
* @author HDVinnie <hdinnovations@protonmail.com>
14+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
15+
*/
16+
17+
use App\Models\Category;
18+
use App\Models\Group;
19+
use App\Models\Resolution;
20+
use App\Models\TorrentRequest;
21+
use App\Models\Torrent;
22+
use App\Models\Type;
23+
use App\Models\User;
24+
use App\Models\UserNotification;
25+
use App\Notifications\NewRequestClaim;
26+
use Illuminate\Foundation\Testing\RefreshDatabase;
27+
use Illuminate\Support\Facades\Notification;
28+
29+
uses(RefreshDatabase::class);
30+
31+
test('claim a request creates a notification for the requester', function (): void {
32+
Notification::fake();
33+
34+
$requester = User::factory()->create();
35+
$filler = User::factory()->create();
36+
37+
$fillerNotificationSettings = UserNotification::create([
38+
'user_id' => $requester->id,
39+
'block_notifications' => 0,
40+
'show_request_claim' => 1,
41+
]);
42+
43+
$category = Category::factory()->create();
44+
$type = Type::factory()->create();
45+
$resolution = Resolution::factory()->create();
46+
47+
$torrent = Torrent::factory()->create([
48+
'category_id' => $category->id,
49+
'type_id' => $type->id,
50+
'resolution_id' => $resolution->id,
51+
]);
52+
53+
$torrentRequest = TorrentRequest::factory()->create([
54+
'anon' => false,
55+
'user_id' => $requester->id,
56+
'category_id' => $category->id,
57+
'type_id' => $type->id,
58+
'resolution_id' => $resolution->id,
59+
'torrent_id' => null,
60+
'claimed' => null,
61+
'filled_by' => null,
62+
'filled_when' => now(),
63+
'approved_by' => null,
64+
'approved_when' => null,
65+
]);
66+
67+
$response = $this->actingAs($filler)->post(route('requests.claims.store', [$torrentRequest]),[
68+
'anon' => 0,
69+
]);
70+
71+
$response->assertRedirect(route('requests.show', $torrentRequest))
72+
->assertSessionHas('success', trans('request.claimed-success'));
73+
74+
Notification::assertSentTo(
75+
[$requester],
76+
NewRequestClaim::class
77+
);
78+
Notification::assertCount(1);
79+
});
80+
81+
test('claim a request creates a notification for the requester when claim notifications not disabled for specific group', function (): void {
82+
Notification::fake();
83+
84+
$randomGroup = Group::factory()->create();
85+
86+
$requester = User::factory()->create();
87+
$filler = User::factory()->create([]);
88+
89+
$fillerNotificationSettings = UserNotification::create([
90+
'user_id' => $requester->id,
91+
'block_notifications' => 0,
92+
'show_request_claim' => 1,
93+
'json_request_groups' => [$randomGroup->id],
94+
]);
95+
96+
$category = Category::factory()->create();
97+
$type = Type::factory()->create();
98+
$resolution = Resolution::factory()->create();
99+
100+
$torrent = Torrent::factory()->create([
101+
'category_id' => $category->id,
102+
'type_id' => $type->id,
103+
'resolution_id' => $resolution->id,
104+
]);
105+
106+
$torrentRequest = TorrentRequest::factory()->create([
107+
'anon' => false,
108+
'user_id' => $requester->id,
109+
'category_id' => $category->id,
110+
'type_id' => $type->id,
111+
'resolution_id' => $resolution->id,
112+
'torrent_id' => null,
113+
'claimed' => null,
114+
'filled_by' => null,
115+
'filled_when' => now(),
116+
'approved_by' => null,
117+
'approved_when' => null,
118+
]);
119+
120+
$response = $this->actingAs($filler)->post(route('requests.claims.store', [$torrentRequest]),[
121+
'anon' => 0,
122+
]);
123+
124+
$response->assertRedirect(route('requests.show', $torrentRequest))
125+
->assertSessionHas('success', trans('request.claimed-success'));
126+
127+
Notification::assertSentTo(
128+
[$requester],
129+
NewRequestClaim::class
130+
);
131+
Notification::assertCount(1);
132+
});
133+
134+
test('claim a request creates a notification for the requester when all notifications are disabled', function (): void {
135+
Notification::fake();
136+
137+
$requester = User::factory()->create();
138+
$filler = User::factory()->create([]);
139+
140+
$fillerNotificationSettings = UserNotification::create([
141+
'user_id' => $requester->id,
142+
'block_notifications' => 1,
143+
'show_request_claim' => 1,
144+
]);
145+
146+
$category = Category::factory()->create();
147+
$type = Type::factory()->create();
148+
$resolution = Resolution::factory()->create();
149+
150+
$torrent = Torrent::factory()->create([
151+
'category_id' => $category->id,
152+
'type_id' => $type->id,
153+
'resolution_id' => $resolution->id,
154+
]);
155+
156+
$torrentRequest = TorrentRequest::factory()->create([
157+
'anon' => false,
158+
'user_id' => $requester->id,
159+
'category_id' => $category->id,
160+
'type_id' => $type->id,
161+
'resolution_id' => $resolution->id,
162+
'torrent_id' => null,
163+
'claimed' => null,
164+
'filled_by' => null,
165+
'filled_when' => now(),
166+
'approved_by' => null,
167+
'approved_when' => null,
168+
]);
169+
170+
$response = $this->actingAs($filler)->post(route('requests.claims.store', [$torrentRequest]),[
171+
'anon' => 0,
172+
]);
173+
174+
$response->assertRedirect(route('requests.show', $torrentRequest))
175+
->assertSessionHas('success', trans('request.claimed-success'));
176+
177+
Notification::assertCount(0);
178+
});
179+
180+
test('claim a request creates a notification for the requester when request claim notifications are disabled', function (): void {
181+
Notification::fake();
182+
183+
$requester = User::factory()->create();
184+
$filler = User::factory()->create([]);
185+
186+
$fillerNotificationSettings = UserNotification::create([
187+
'user_id' => $requester->id,
188+
'block_notifications' => 0,
189+
'show_request_claim' => 0,
190+
]);
191+
192+
$category = Category::factory()->create();
193+
$type = Type::factory()->create();
194+
$resolution = Resolution::factory()->create();
195+
196+
$torrent = Torrent::factory()->create([
197+
'category_id' => $category->id,
198+
'type_id' => $type->id,
199+
'resolution_id' => $resolution->id,
200+
]);
201+
202+
$torrentRequest = TorrentRequest::factory()->create([
203+
'anon' => false,
204+
'user_id' => $requester->id,
205+
'category_id' => $category->id,
206+
'type_id' => $type->id,
207+
'resolution_id' => $resolution->id,
208+
'torrent_id' => null,
209+
'claimed' => null,
210+
'filled_by' => null,
211+
'filled_when' => now(),
212+
'approved_by' => null,
213+
'approved_when' => null,
214+
]);
215+
216+
$response = $this->actingAs($filler)->post(route('requests.claims.store', [$torrentRequest]),[
217+
'anon' => 0,
218+
]);
219+
220+
$response->assertRedirect(route('requests.show', $torrentRequest))
221+
->assertSessionHas('success', trans('request.claimed-success'));
222+
223+
Notification::assertCount(0);
224+
});
225+
226+
test('claim a request creates a notification for the requester when request claim notifications are disabled for specific group', function (): void {
227+
Notification::fake();
228+
229+
$group = Group::factory()->create();
230+
231+
$requester = User::factory()->create();
232+
$filler = User::factory()->create([
233+
'group_id' => $group->id,
234+
]);
235+
236+
$fillerNotificationSettings = UserNotification::create([
237+
'user_id' => $requester->id,
238+
'block_notifications' => 0,
239+
'show_request_claim' => 0,
240+
'json_request_groups' => [$group->id],
241+
]);
242+
243+
$category = Category::factory()->create();
244+
$type = Type::factory()->create();
245+
$resolution = Resolution::factory()->create();
246+
247+
$torrent = Torrent::factory()->create([
248+
'category_id' => $category->id,
249+
'type_id' => $type->id,
250+
'resolution_id' => $resolution->id,
251+
]);
252+
253+
$torrentRequest = TorrentRequest::factory()->create([
254+
'anon' => false,
255+
'user_id' => $requester->id,
256+
'category_id' => $category->id,
257+
'type_id' => $type->id,
258+
'resolution_id' => $resolution->id,
259+
'torrent_id' => null,
260+
'claimed' => null,
261+
'filled_by' => null,
262+
'filled_when' => now(),
263+
'approved_by' => null,
264+
'approved_when' => null,
265+
]);
266+
267+
$response = $this->actingAs($filler)->post(route('requests.claims.store', [$torrentRequest]),[
268+
'anon' => 0,
269+
]);
270+
271+
$response->assertRedirect(route('requests.show', $torrentRequest))
272+
->assertSessionHas('success', trans('request.claimed-success'));
273+
274+
Notification::assertCount(0);
275+
});

0 commit comments

Comments
 (0)