Skip to content

fea: (LAR-87) Mise en place des test pour la signalisation d'un contenu #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2024
Merged
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
29 changes: 29 additions & 0 deletions app/Actions/ReportSpamAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Actions;

use App\Contracts\SpamReportableContract;
use App\Exceptions\CanReportSpamException;
use App\Models\User;
use App\Notifications\ReportedSpamToTelegram;

final class ReportSpamAction
{
public function execute(User $user, SpamReportableContract $model, ?string $content = null): void
{
if ($model->spamReports()->whereBelongsTo($user)->exists()) {
throw new CanReportSpamException(
message: __('notifications.exceptions.spam_exist'),
);
}

$spam = $model->spamReports()->create([
'user_id' => $user->id,
'reason' => $content,
]);

$user->notify(new ReportedSpamToTelegram($spam));
}
}
14 changes: 14 additions & 0 deletions app/Contracts/SpamReportableContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Contracts;

use Illuminate\Database\Eloquent\Relations\MorphMany;

interface SpamReportableContract
{
public function getPathUrl(): string;

public function spamReports(): MorphMany;
}
9 changes: 9 additions & 0 deletions app/Exceptions/CanReportSpamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Exceptions;

use Exception;

final class CanReportSpamException extends Exception {}
66 changes: 66 additions & 0 deletions app/Livewire/ReportSpam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Livewire;

use App\Actions\ReportSpamAction;
use App\Contracts\SpamReportableContract;
use App\Exceptions\CanReportSpamException;
use Filament\Actions\Action;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

final class ReportSpam extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public SpamReportableContract $model;

public ?string $reason = null;

public function reportAction(): Action
{
return Action::make('report')
->color('danger')
->badge()
->label(__('pages/forum.report_spam'))
->authorize('report', $this->model) // @phpstan-ignore-line
->requiresConfirmation()
->action(function (): void {
try {
app(ReportSpamAction::class)->execute(
user: Auth::user(), // @phpstan-ignore-line
model: $this->model,
content: $this->reason,
);

Notification::make()
->title(__('notifications.spam_send'))
->success()
->duration(3500)
->send();

$this->reset('reason');
} catch (CanReportSpamException $e) {
Notification::make()
->title($e->getMessage())
->danger()
->duration(3500)
->send();
}
});
}

public function render(): View
{
return view('livewire.components.report-spam');
}
}
7 changes: 6 additions & 1 deletion app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use App\Contracts\ReactableInterface;
use App\Contracts\ReplyInterface;
use App\Contracts\SpamReportableContract;
use App\Contracts\SubscribeInterface;
use App\Models\Builders\DiscussionQueryBuilder;
use App\Traits\HasAuthor;
use App\Traits\HasReplies;
use App\Traits\HasSlug;
use App\Traits\HasSpamReports;
use App\Traits\HasSubscribers;
use App\Traits\HasTags;
use App\Traits\Reactable;
Expand All @@ -21,6 +23,7 @@
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
Expand All @@ -35,13 +38,15 @@
* @property Carbon $created_at
* @property Carbon $updated_at
* @property User $user
* @property Collection | SpamReport[] $spamReports
*/
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SubscribeInterface, Viewable
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
use HasFactory;
use HasReplies;
use HasSlug;
use HasSpamReports;
use HasSubscribers;
use HasTags;
use InteractsWithViews;
Expand All @@ -63,7 +68,7 @@
];

protected $appends = [
'count_all_replies_with_child',

Check failure on line 71 in app/Models/Discussion.php

View workflow job for this annotation

GitHub Actions / phpstan

Property 'count_all_replies_with_child' does not exist in model.
];

protected bool $removeViewsOnDelete = true;
Expand Down
17 changes: 11 additions & 6 deletions app/Models/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use App\Contracts\ReactableInterface;
use App\Contracts\ReplyInterface;
use App\Contracts\SpamReportableContract;
use App\Traits\HasAuthor;
use App\Traits\HasReplies;
use App\Traits\HasSpamReports;
use App\Traits\Reactable;
use App\Traits\RecordsActivity;
use Carbon\Carbon;
Expand All @@ -17,6 +19,7 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
Expand All @@ -28,12 +31,14 @@
* @property User $user
* @property int $replyable_id
* @property string $replyable_type
* @property Collection | SpamReport[] $spamReports
*/
final class Reply extends Model implements ReactableInterface, ReplyInterface
final class Reply extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract
{
use HasAuthor;
use HasFactory;
use HasReplies;
use HasSpamReports;
use Reactable;
use RecordsActivity;

Expand All @@ -56,11 +61,6 @@ public function getPathUrl(): string
return "#reply-{$this->id}";
}

public function solutionTo(): HasOne
{
return $this->hasOne(Thread::class, 'solution_reply_id');
}

public function wasJustPublished(): bool
{
return $this->created_at->gt(Carbon::now()->subMinute());
Expand All @@ -83,6 +83,11 @@ public function to(ReplyInterface $replyable): void
$this->replyAble()->associate($replyable); // @phpstan-ignore-line
}

public function solutionTo(): HasOne
{
return $this->hasOne(Thread::class, 'solution_reply_id');
}

public function allChildReplies(): MorphMany
{
return $this->replies()
Expand Down
40 changes: 40 additions & 0 deletions app/Models/SpamReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property-read int $id
* @property int $user_id
* @property int $reportable_id
* @property string $reportable_type
* @property string | null $reason
* @property User | null $user
*/
final class SpamReport extends Model
{
use HasFactory;

protected $fillable = [
'user_id',
'reportable_id',
'reportable_type',
'reason',
];

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

public function reportable(): MorphTo
{
return $this->morphTo();
}
}
5 changes: 4 additions & 1 deletion app/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use App\Contracts\ReactableInterface;
use App\Contracts\ReplyInterface;
use App\Contracts\SpamReportableContract;
use App\Contracts\SubscribeInterface;
use App\Exceptions\CouldNotMarkReplyAsSolution;
use App\Filters\Thread\ThreadFilters;
use App\Traits\HasAuthor;
use App\Traits\HasReplies;
use App\Traits\HasSlug;
use App\Traits\HasSpamReports;
use App\Traits\HasSubscribers;
use App\Traits\Reactable;
use App\Traits\RecordsActivity;
Expand Down Expand Up @@ -49,12 +51,13 @@
* @property Reply | null $solutionReply
* @property \Illuminate\Database\Eloquent\Collection | Channel[] $channels
*/
final class Thread extends Model implements Feedable, ReactableInterface, ReplyInterface, SubscribeInterface, Viewable
final class Thread extends Model implements Feedable, ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
use HasFactory;
use HasReplies;
use HasSlug;
use HasSpamReports;
use HasSubscribers;
use InteractsWithViews;
use Notifiable;
Expand Down
7 changes: 6 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function profile(): array
return [
'name' => $this->name,
'username' => $this->username,
'picture' => (string) $this->profile_photo_url,
'picture' => $this->profile_photo_url,
];
}

Expand Down Expand Up @@ -269,6 +269,11 @@ public function transactions(): HasMany
return $this->hasMany(Transaction::class);
}

public function spamReports(): HasMany
{
return $this->hasMany(SpamReport::class, 'user_id');
}

public function deleteThreads(): void
{
// We need to explicitly iterate over the threads and delete them
Expand Down
2 changes: 1 addition & 1 deletion app/Notifications/ArticleSubmitted.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private function content(): string
{
$content = "*Nouvel Article Soumis!*\n\n";
$content .= 'Titre: '.$this->article->title."\n";
$content .= 'Par: [@'.$this->article->user?->username.']('.route('profile', $this->article->user?->username).')';
$content .= 'Par: [@'.$this->article->user->username.']('.route('profile', $this->article->user->username).')';

return $content;
}
Expand Down
31 changes: 31 additions & 0 deletions app/Notifications/ReportedSpamToTelegram.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\SpamReport;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;

final class ReportedSpamToTelegram extends Notification
{
use Queueable;

public function __construct(public SpamReport $spamReport) {}

public function via(object $notifiable): array
{
return [TelegramChannel::class];
}

public function toTelegram(): TelegramMessage
{
return TelegramMessage::create()
->to('@laravelcm')
->content("{$this->spamReport->user?->name} vient de reporter un contenu spam")
->button('Voir les spams', route('filament.admin.pages.dashboard'));
}
}
5 changes: 5 additions & 0 deletions app/Policies/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public function unsubscribe(User $user, Discussion $discussion): bool
{
return $discussion->hasSubscriber($user);
}

public function report(User $user, Discussion $discussion): bool
{
return $user->hasVerifiedEmail() && ! $discussion->isAuthoredBy($user);
}
}
5 changes: 5 additions & 0 deletions app/Policies/ReplyPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public function delete(User $user, Reply $reply): bool
{
return $reply->isAuthoredBy($user) || $user->isModerator() || $user->isAdmin();
}

public function report(User $user, Reply $reply): bool
{
return $user->hasVerifiedEmail() && ! $reply->isAuthoredBy($user);
}
}
5 changes: 5 additions & 0 deletions app/Policies/ThreadPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public function unsubscribe(User $user, Thread $thread): bool
{
return $thread->hasSubscriber($user);
}

public function report(User $user, Thread $thread): bool
{
return $user->hasVerifiedEmail() && ! $thread->isAuthoredBy($user);
}
}
16 changes: 16 additions & 0 deletions app/Traits/HasSpamReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Traits;

use App\Models\SpamReport;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasSpamReports
{
public function spamReports(): MorphMany
{
return $this->morphMany(SpamReport::class, 'reportable');
}
}
Loading
Loading