Skip to content

Fix discussion comment error #84

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 4 commits into from
Mar 10, 2023
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
30 changes: 30 additions & 0 deletions app/Actions/Replies/CreateReply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Actions\Replies;

use App\Events\CommentWasAdded;
use App\Gamify\Points\ReplyCreated;
use App\Models\Reply;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Lorisleiva\Actions\Concerns\AsAction;

final class CreateReply
{
use AsAction;

public function handle(string $body, User $user, Model $model): Reply
{
$reply = new Reply(['body' => $body]);
$reply->authoredBy($user);
$reply->to($model);
$reply->save();

$user->givePoint(new ReplyCreated($model, $user));

// On envoie un event pour une nouvelle réponse à tous les abonnés de la discussion
event(new CommentWasAdded($reply, $model));

return $reply;
}
}
20 changes: 20 additions & 0 deletions app/Actions/Replies/LikeReply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Actions\Replies;

use App\Models\Reaction;
use App\Models\Reply;
use App\Models\User;
use Lorisleiva\Actions\Concerns\AsAction;

final class LikeReply
{
use AsAction;

public function handle(User $user, Reply $reply, string $reaction = 'love'): void
{
$react = Reaction::where('name', $reaction)->first();

$user->reactTo($reply, $react);
}
}
3 changes: 2 additions & 1 deletion app/Console/Commands/CreateAdminUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ protected function createUser(): void
];

try {
/** @var User $user */
$user = User::query()->create($userData);

$user->assignRole('admin');
} catch (\Exception | QueryException $e) {
} catch (\Exception|QueryException $e) {
$this->error($e->getMessage());
}
}
Expand Down
17 changes: 2 additions & 15 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ class Kernel extends ConsoleKernel
\App\Console\Commands\Cleanup\DeleteOldUnverifiedUsers::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
protected function schedule(Schedule $schedule): void
{
$schedule->command('media-library:delete-old-temporary-uploads')->daily();
$schedule->command('lcm:delete-old-unverified-users')->daily();
Expand All @@ -36,15 +30,8 @@ protected function schedule(Schedule $schedule)
}
}

/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
protected function commands(): void
{
$this->load(__DIR__.'/Commands');

require base_path('routes/console.php');
}
}
2 changes: 1 addition & 1 deletion app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function getWidgets(): array
];
}

protected function getColumns(): int | array
protected function getColumns(): int|array
{
return 5;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Widgets/BlogPostsOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BlogPostsOverview extends Widget
{
protected static string $view = 'filament.widgets.blog-posts-overview';

protected int | string | array $columnSpan = 5;
protected int|string|array $columnSpan = 5;

protected function getViewData(): array
{
Expand Down
56 changes: 56 additions & 0 deletions app/Http/Livewire/Discussions/AddComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Livewire\Discussions;

use App\Actions\Replies\CreateReply;
use App\Models\Discussion;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class AddComment extends Component
{
public $discussion;

public bool $isRoot = true;

public bool $isReply = false;

public string $body = '';

protected $listeners = ['reloadComment' => '$refresh'];

public function mount(Discussion $discussion): void
{
$this->discussion = $discussion;
}

public function saveComment(): void
{
$this->validate(
['body' => 'required'],
['body.required' => __('Votre commentaire ne peut pas être vide')]
);

$comment = CreateReply::run($this->body, auth()->user(), $this->discussion);

$this->emitSelf('reloadComment');

$this->emitUp('reloadComments');

$this->dispatchBrowserEvent('scrollToComment', ['id' => $comment->id]);

Notification::make()
->title(__('Votre commentaire a été ajouté!'))
->success()
->duration(5000)
->send();

$this->reset();
}

public function render(): View
{
return view('livewire.discussions.add-comment');
}
}
43 changes: 43 additions & 0 deletions app/Http/Livewire/Discussions/Comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Http\Livewire\Discussions;

use App\Actions\Replies\LikeReply;
use App\Models\Reply;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Livewire\Component;

class Comment extends Component
{
public Reply $comment;

protected $listeners = ['reloadComment' => '$refresh'];

public function delete(): void
{
$this->comment->delete();

Notification::make()
->title(__('Votre commentaire a été supprimé.'))
->success()
->duration(5000)
->send();

$this->emitUp('reloadComment');
}

public function toggleLike(): void
{
LikeReply::run(auth()->user(), $this->comment);

$this->emitSelf('reloadComment');
}

public function render(): View
{
return view('livewire.discussions.comment', [
'count' => $this->comment->getReactionsSummary()->sum('count'),
]);
}
}
44 changes: 44 additions & 0 deletions app/Http/Livewire/Discussions/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Livewire\Discussions;

use App\Models\Discussion;
use App\Models\Reply;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Livewire\Component;

class Comments extends Component
{
public $discussion;

public $listeners = ['reloadComments' => '$refresh'];

public function mount(Discussion $discussion): void
{
$this->discussion = $discussion;
}

public function getCommentsProperty(): Collection
{
$replies = collect();

foreach ($this->discussion->replies->load(['allChildReplies', 'author']) as $reply) {
/** @var Reply $reply */
if ($reply->allChildReplies->isNotEmpty()) {
foreach ($reply->allChildReplies as $childReply) {
$replies->add($childReply);
}
}

$replies->add($reply);
}

return $replies;
}

public function render(): View
{
return view('livewire.discussions.comments');
}
}
17 changes: 15 additions & 2 deletions app/Http/Livewire/Discussions/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Discussion;
use App\Models\Subscribe as SubscribeModel;
use App\Policies\DiscussionPolicy;
use Filament\Notifications\Notification;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
Expand All @@ -28,7 +29,13 @@ public function subscribe()
$subscribe->user()->associate(Auth::user());
$this->discussion->subscribes()->save($subscribe);

// @ToDo Mettre un nouveau system de notification avec Livewire $this->notification()->success('Abonnement', 'Vous êtes maintenant abonné à cette discussion.');
Notification::make()
->title(__('Abonnement'))
->body(__('Vous êtes maintenant abonné à cette discussion.'))
->success()
->duration(5000)
->send();

$this->emitSelf('refresh');
}

Expand All @@ -40,7 +47,13 @@ public function unsubscribe()
->where('user_id', Auth::id())
->delete();

// @ToDo Mettre un nouveau system de notification $this->notification()->success('Désabonnement', 'Vous êtes maintenant désabonné de cette discussion.');
Notification::make()
->title(__('Désabonnement'))
->body(__('Vous êtes maintenant désabonné à cette discussion.'))
->success()
->duration(5000)
->send();

$this->emitSelf('refresh');
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function isLocked(): bool

public function getCountAllRepliesWithChildAttribute(): int
{
$count = $this->replies()->count();
$count = $this->replies->count();

foreach ($this->replies()->withCount('allChildReplies')->get() as $reply) {
$count += $reply->all_child_replies_count;
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function solutionTo(): HasOne
return $this->hasOne(Thread::class, 'solution_reply_id');
}

public function wasJustPublished()
public function wasJustPublished(): bool
{
return $this->created_at->gt(Carbon::now()->subMinute());
}
Expand All @@ -72,7 +72,7 @@ public function excerpt(int $limit = 100): string
return Str::limit(strip_tags(md_to_html($this->body)), $limit);
}

public function mentionedUsers()
public function mentionedUsers(): array
{
preg_match_all('/@([a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w))/', $this->body, $matches);

Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use App\View\Composers\AuthUserComposer;
use App\View\Composers\ChannelsComposer;
use App\View\Composers\InactiveDiscussionsComposer;
use App\View\Composers\ModeratorsComposer;
Expand All @@ -28,7 +29,7 @@
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
use Spatie\Health\Facades\Health;

class AppServiceProvider extends ServiceProvider
final class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
Expand Down Expand Up @@ -84,6 +85,7 @@ public function bootViewsComposer(): void
View::composer('discussions._contributions', TopContributorsComposer::class);
View::composer('discussions._contributions', InactiveDiscussionsComposer::class);
View::composer('components.profile-users', ProfileUsersComposer::class);
View::composer('*', AuthUserComposer::class);
}

public function bootEloquentMorphs(): void
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Illuminate\Notifications\DatabaseNotification as Notification;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
final class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/BroadcastServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;

class BroadcastServiceProvider extends ServiceProvider
final class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
final class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Fortify;

class FortifyServiceProvider extends ServiceProvider
final class FortifyServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
final class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
Expand Down
Loading