Skip to content

Commit 1ca5625

Browse files
committed
feat: (LAR-87) Update SpamReport tests
1 parent ad062a6 commit 1ca5625

File tree

6 files changed

+224
-108
lines changed

6 files changed

+224
-108
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Actions\SpamReport;
6+
7+
use App\Models\SpamReport;
8+
use App\Models\User;
9+
use App\Notifications\ContentReportedSpamNotification;
10+
use Illuminate\Support\Facades\Auth;
11+
use Illuminate\Support\Facades\Notification;
12+
13+
class ReportSpamAction
14+
{
15+
public function execute(int $contentId, string $contentType, string $reason): SpamReport
16+
{
17+
$user = Auth::user();
18+
19+
if (! $user->hasVerifiedEmail()) {
20+
throw new \Exception('Vous devez avoir un email vérifié pour signaler du contenu.');
21+
}
22+
23+
if (SpamReport::where('user_id', $user->id)
24+
->where('reportable_id', $contentId)
25+
->where('reportable_type', $contentType)
26+
->exists()) {
27+
throw new \Exception('Vous avez déjà signalé ce contenu.');
28+
}
29+
30+
$report = SpamReport::create([
31+
'user_id' => $user->id,
32+
'reportable_id' => $contentId,
33+
'reportable_type' => $contentType,
34+
'reason' => $reason,
35+
]);
36+
37+
$adminsAndModerators = User::whereIn('role', ['admin', 'moderator'])->get();
38+
Notification::send($adminsAndModerators, new ContentReportedSpamNotification($contentId, $contentType));
39+
40+
return $report;
41+
}
42+
}

app/Actions/SpamReport/SpamReportContent.php renamed to app/Livewire/ReportSpam.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,58 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Actions\SpamReport;
5+
namespace App\Livewire;
66

7-
use App\Models\SpamReport;
87
use App\Models\User;
9-
use App\Notifications\ContentReportedSpamNotification;
8+
use Livewire\Component;
9+
use App\Models\SpamReport;
1010
use Illuminate\Support\Facades\Auth;
1111
use Illuminate\Support\Facades\Notification;
12-
use Livewire\Component;
12+
use App\Notifications\ContentReportedSpamNotification;
1313

14-
final class SpamReportContent extends Component
14+
final class ReportSpam extends Component
1515
{
16-
public $contentId;
16+
public $recordId;
1717

18-
public $contentType;
18+
public $recordType;
1919

2020
public $reason;
2121

2222
public $alreadyReported;
2323

24-
public function mount($contentId, $contentType): void
24+
public $isModalOpen = false;
25+
26+
public function mount($recordId, $recordType): void
2527
{
26-
$this->contentId = $contentId;
27-
$this->contentType = $contentType;
28+
$this->recordId = $recordId;
29+
$this->recordType = $recordType;
2830
$this->alreadyReported = SpamReport::where('user_id', Auth::id())
29-
->where('reportable_id', $contentId)
30-
->where('reportable_type', $contentType)
31+
->where('reportable_id', $recordId)
32+
->where('reportable_type', $recordType)
3133
->exists();
3234
}
3335

36+
public function openModal(): void
37+
{
38+
$this->isModalOpen = true;
39+
}
40+
41+
public function closeModal(): void
42+
{
43+
$this->isModalOpen = false;
44+
$this->reset('reason');
45+
}
46+
3447
public function report(): void
3548
{
36-
if (! Auth::check()) {
49+
$user = Auth::user();
50+
51+
if (!$user) {
3752
abort(403, 'Vous devez être connecté pour signaler du contenu.');
3853
}
3954

40-
if (! Auth::user()->hasVerifiedEmail()) {
41-
$this->addError('email', 'Vous devez avoir un email vérifié pour signaler du contenu.');
42-
43-
return;
55+
if (!$user->hasVerifiedEmail()) {
56+
abort(403, 'Vous devez avoir un email vérifié pour signaler du contenu');
4457
}
4558

4659
if ($this->alreadyReported) {
@@ -51,12 +64,12 @@ public function report(): void
5164

5265
SpamReport::create([
5366
'user_id' => Auth::id(),
54-
'reportable_id' => $this->contentId,
55-
'reportable_type' => $this->contentType,
67+
'reportable_id' => $this->recordId,
68+
'reportable_type' => $this->recordType,
5669
'reason' => $this->reason,
5770
]);
5871

59-
Notification::send(User::whereIn('role', ['admin', 'moderator'])->get(), new ContentReportedSpamNotification($this->contentId, $this->contentType));
72+
Notification::send(User::whereIn('role', ['admin', 'moderator'])->get(), new ContentReportedSpamNotification($this->recordId, $this->recordType));
6073

6174
$this->alreadyReported = true;
6275
$this->reset('reason');
@@ -66,6 +79,6 @@ public function report(): void
6679

6780
public function render()
6881
{
69-
return view('livewire.report-content');
82+
return view('livewire.components.spam-report');
7083
}
7184
}

app/Models/SpamReport.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ final class SpamReport extends Model
1616
'reportable_id',
1717
'reportable_type',
1818
'reason',
19-
'status',
2019
];
2120

2221
public function user()

database/migrations/2024_11_13_155231_create_spam_reports_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function up(): void
1414
$table->id();
1515
$table->foreignId('user_id')->constrained()->onDelete('cascade');
1616
$table->morphs('reportable');
17-
$table->text('reason');
17+
$table->text('reason')->nullable();
1818
$table->timestamps();
1919
});
2020
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<div>
2+
@can('report-spam')
3+
@if ($alreadyReported)
4+
<!-- Badge si déjà signalé -->
5+
<span class="badge badge-success">Déjà signalé</span>
6+
@else
7+
<!-- Lien pour ouvrir le modal -->
8+
<button class="btn btn-link text-danger" wire:click="openModal">Signaler</button>
9+
@endif
10+
@endcan
11+
12+
<!-- Modal -->
13+
@if ($isModalOpen)
14+
<div class="modal d-block" tabindex="-1" role="dialog" style="background: rgba(0, 0, 0, 0.5);">
15+
<div class="modal-dialog" role="document">
16+
<div class="modal-content">
17+
<div class="modal-header">
18+
<h5 class="modal-title">Signaler un contenu</h5>
19+
<button type="button" class="close" wire:click="closeModal" aria-label="Close">
20+
<span aria-hidden="true">&times;</span>
21+
</button>
22+
</div>
23+
<div class="modal-body">
24+
<textarea
25+
wire:model="reason"
26+
class="form-control"
27+
placeholder="Raison du signalement">
28+
</textarea>
29+
@error('reason')
30+
<span class="text-danger">{{ $message }}</span>
31+
@enderror
32+
</div>
33+
<div class="modal-footer">
34+
<button type="button" class="btn btn-secondary" wire:click="closeModal">Fermer</button>
35+
<button type="button" class="btn btn-primary" wire:click="report">Envoyer</button>
36+
</div>
37+
</div>
38+
</div>
39+
</div>
40+
@endif
41+
42+
<!-- Messages de feedback -->
43+
@if (session()->has('message'))
44+
<div class="alert alert-success mt-2">
45+
{{ session('message') }}
46+
</div>
47+
@endif
48+
49+
@if (session()->has('error'))
50+
<div class="alert alert-danger mt-2">
51+
{{ session('error') }}
52+
</div>
53+
@endif
54+
</div>

0 commit comments

Comments
 (0)