Skip to content
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

feat: (LAR-105) create TransfertDiscutionToThreadAction file #228

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
36 changes: 36 additions & 0 deletions app/Actions/Discussion/ConvertDiscussionToThreadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Actions\Discussion;

use App\Models\Discussion;
use App\Models\Thread;
use App\Notifications\NotifyUserConvertionDiscussionToThread;
use Illuminate\Support\Facades\DB;

class ConvertDiscussionToThreadAction
{
public function execute(Discussion $discussion, bool $isAdmin = false): Thread
{
return DB::transaction(function () use ($discussion) {
$thread = Thread::create([
'title' => $discussion->title,
'slug' => $discussion->slug,
'body' => $discussion->body,
'user_id' => $discussion->user_id,
'locked' => $discussion->locked,
'last_posted_at' => now(),
]);

$discussion->replies()->update([
'replyable_type' => Thread::class,
'replyable_id' => $thread->id,
]);

$discussion->delete();

app(NotifyUserConvertionDiscussionToThread::class)->execute($thread, $isAdmin);

return $thread;
});
}
}
23 changes: 23 additions & 0 deletions app/Actions/Discussion/NotifyUsersOfThreadConversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Actions\Discussion;

use App\Models\Thread;
use App\Notifications\ThreadConvertedByAdmin;
use App\Notifications\ThreadConvertedByCreator;
use Illuminate\Support\Facades\Mail;

class NotifyUsersOfThreadConversion
{
public function execute(Thread $thread, bool $isAdmin = false): void
{
$usersToNotify = $thread->replies->pluck('user')->unique();

// ToDo: send mail to the replying user with this class notification ThreadConvertedByCreator

if ($isAdmin) {
$creator = $thread->user;
// ToDo: send mail to the creator with this class notification ThreadConvertedByAdmin
}
}
}
36 changes: 36 additions & 0 deletions app/Livewire/Discussions/ConvertedToThread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Discussions;

use App\Models\Discussion;
use App\Policies\DiscussionPolicy;
use Filament\Notifications\Notification;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;

final class ConvertedToThread
{
use AuthorizesRequests;

public Discussion $discussion;

public function convertDiscution(): void
{
$this->authorize(DiscussionPolicy::CONVERTEDTOTHREAD, $this->discussion);

$this->discussion->subscribes()
->where('user_id', Auth::id())
->delete();

Notification::make()
->title(__('Désabonnement'))
->body(__('Vous êtes maintenant désabonné à cette discussion.'))
->success()
->duration(5000)
->send();

$this->dispatch('refresh')->self();

Check failure on line 34 in app/Livewire/Discussions/ConvertedToThread.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method App\Livewire\Discussions\ConvertedToThread::dispatch().
}
}
50 changes: 50 additions & 0 deletions app/Notifications/ThreadConvertedByAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByAdmin extends Notification
{
use Queueable;

public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->markdown('emails.notify-creator-for-thread-conversion');
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
50 changes: 50 additions & 0 deletions app/Notifications/ThreadConvertedByCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByCreator extends Notification
{
use Queueable;

public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)->markdown('emails.notification-user-to-thread-conversion');
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
//
];
}
}
7 changes: 7 additions & 0 deletions app/Policies/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class DiscussionPolicy

public const UNSUBSCRIBE = 'unsubscribe';

public const string CONVERTEDTOTHREAD = 'convertedToThread';

public function create(User $user): bool
{
return $user->hasVerifiedEmail();
Expand Down Expand Up @@ -52,6 +54,11 @@ public function unsubscribe(User $user, Discussion $discussion): bool
return $discussion->hasSubscriber($user);
}

public function convertedToThread(User $user, Discussion $discussion): bool
{
return $discussion->isAuthoredBy($user) || $user->isModerator() || $user->isAdmin();
}

public function report(User $user, Discussion $discussion): bool
{
return $user->hasVerifiedEmail() && ! $discussion->isAuthoredBy($user);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-mail::message>
# Introduction

The body of your message.

<x-mail::button :url="''">
Button Text
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-mail::message>
# Introduction

The body of your message.

<x-mail::button :url="''">
Button Text
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
23 changes: 23 additions & 0 deletions resources/views/livewire/discussions/converted-to-thread.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div>
<span class="inline-flex rounded-md shadow-sm">
@can(App\Policies\DiscussionPolicy::CONVERTEDTOTHREAD, $discussion)
<x-buttons.primary type="button" wire:click="convertDiscution" wire:loading.attr="disabled">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="size-5"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0M3.124 7.5A8.969 8.969 0 015.292 3m13.416 0a8.969 8.969 0 012.168 4.5"
/>
</svg>
<span class="mx-2">Convert to thread</span>
<x-loader class="mx-0 text-white" wire:loading wire:target="unsubscribe" />
</x-buttons.primary>
@endcan
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class="font-sans text-sm leading-5 text-red-500 hover:underline focus:outline-no
</p>
@auth
<livewire:discussions.subscribe :discussion="$discussion" />
<livewire:discussions.converted-to-thread :discussion="$discussion" />
@endauth
</div>

Expand Down
Loading