Skip to content

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

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
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

declare(strict_types=1);

namespace App\Actions\Discussion;

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

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

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

$discussion->delete();

app(NotifyUsersOfThreadConversion::class)->execute($thread);

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

declare(strict_types=1);

namespace App\Actions\Discussion;

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

final class NotifyUsersOfThreadConversion
{
public function execute(Thread $thread): void
{
$usersToNotify = $thread->replies()->pluck('user_id')->unique()->toArray();

User::whereIn('id', $usersToNotify)->get()->each->notify(new ThreadConvertedByCreator($thread));

if (Auth::check() && Auth::user()->isAdmin()) {
$thread->user->notify(new ThreadConvertedByAdmin($thread));
}
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class ArticleResource extends Resource

protected static ?string $navigationIcon = 'heroicon-o-newspaper';

public static function getNavigationGroup(): ?string
public static function getNavigationGroup(): string
{
return __('Contenu');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class ListArticles extends ListRecords
{
protected static string $resource = ArticleResource::class;

public function isTableRecordSelectable(): ?Closure
public function isTableRecordSelectable(): Closure
{
return fn (Article $record): bool => $record->isNotPublished();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/ChannelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class ChannelResource extends Resource

protected static ?string $navigationIcon = 'untitledui-git-branch';

public static function getNavigationGroup(): ?string
public static function getNavigationGroup(): string
{
return __('Forum');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/DiscussionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class DiscussionResource extends Resource

protected static ?string $navigationIcon = 'untitledui-message-chat-square';

public static function getNavigationGroup(): ?string
public static function getNavigationGroup(): string
{
return __('Contenu');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/TagResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class TagResource extends Resource

protected static ?string $navigationIcon = 'untitledui-tag-03';

public static function getNavigationGroup(): ?string
public static function getNavigationGroup(): string
{
return __('Contenu');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class UserResource extends Resource

protected static ?string $navigationIcon = 'untitledui-users-02';

public static function getNavigationGroup(): ?string
public static function getNavigationGroup(): string
{
return __('Management');
}
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Components/Forum/ReplyForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function open(?int $replyId = null): void
{
$this->reply = Reply::query()->find($replyId);

$this->form->fill(['body' => $this->reply?->body ?? '']);
$this->form->fill(['body' => $this->reply->body ?? '']);

$this->show = true;
}
Expand Down
32 changes: 32 additions & 0 deletions app/Livewire/Modals/ConvertDiscussion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Modals;

use App\Actions\Discussion\ConvertDiscussionToThreadAction;
use App\Models\Discussion;
use Illuminate\Contracts\View\View;
use LivewireUI\Modal\ModalComponent;

final class ConvertDiscussion extends ModalComponent
{
public int $discussionId;

public function save(): void
{
/** @var Discussion $discussion */
$discussion = Discussion::query()->findOrFail($this->discussionId);

$this->authorize('convertedToThread', $discussion);

$thread = app(ConvertDiscussionToThreadAction::class)->execute($discussion);

$this->redirectRoute('forum.show', $thread, navigate: true);
}

public function render(): View
{
return view('livewire.modals.convert-discussion');
}
}
2 changes: 1 addition & 1 deletion app/Livewire/Modals/Unsplash.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static function modalMaxWidth(): string

public function render(): View
{
return view('livewire.modals.unsplash');
return view('livewire.modals.unsplash'); // @phpstan-ignore-line
}
}
1 change: 0 additions & 1 deletion app/Mail/NewReplyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function __construct(

public function build(): self
{
// @phpstan-ignore-next-line
return $this->subject("Re: {$this->reply->replyAble->subject()}")
->markdown('emails.new_reply');
}
Expand Down
1 change: 1 addition & 0 deletions app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class Discussion extends Model implements ReactableInterface, ReplyInterfa
];

protected $appends = [
// @phpstan-ignore-next-line
'count_all_replies_with_child',
];

Expand Down
7 changes: 4 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function getFilamentAvatarUrl(): ?string
}

/**
* @return array{name: string, username: string, picture: string}
* @return array{name: string, username: string, picture: string|null}
*/
public function profile(): array
{
Expand Down Expand Up @@ -343,6 +343,7 @@ public function hasPassword(): bool
{
$password = $this->getAuthPassword();

// @phpstan-ignore-next-line
return $password !== '' && $password !== null;
}

Expand Down Expand Up @@ -382,12 +383,12 @@ public function countReplies(): int

public function countSolutions(): int
{
return $this->replyAble()->isSolution()->count();
return $this->replyAble()->isSolution()->count(); // @phpstan-ignore-line
}

public function countArticles(): int
{
return $this->articles()->approved()->count();
return $this->articles()->approved()->count(); // @phpstan-ignore-line
}

public function countDiscussions(): int
Expand Down
41 changes: 41 additions & 0 deletions app/Notifications/ThreadConvertedByAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\Thread;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByAdmin extends Notification
{
use Queueable;

public function __construct(public Thread $thread) {}

/**
* 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)
->subject(__('pages/discussion.converted_by_admin.subject'))
->greeting(__('pages/discussion.converted_by_admin.greeting'))
->line(__('pages/discussion.converted_by_admin.converted_line'))
->line(__('pages/discussion.converted_by_admin.thread_title').$this->thread->title)
->action(__('pages/discussion.converted_by_admin.action_text'), route('forum.show', $this->thread))
->line(__('pages/discussion.converted_by_admin.admin_action_line'));
}
}
40 changes: 40 additions & 0 deletions app/Notifications/ThreadConvertedByCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Notifications;

use App\Models\Thread;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

final class ThreadConvertedByCreator extends Notification
{
use Queueable;

public function __construct(public Thread $thread) {}

/**
* 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)
->subject(__('pages/discussion.converted_by_creator'))
->line(__('pages/discussion.converted_by_creator.converted_line'))
->line(__('pages/discussion.converted_by_creator.thread_title').$this->thread->title)
->action(__('pages/discussion.converted_by_creator.action_text'), route('forum.show', $this->thread))
->line(__('pages/discussion.converted_by_creator.thank_you_line'));
}
}
5 changes: 5 additions & 0 deletions app/Policies/DiscussionPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function report(User $user, Discussion $discussion): bool
{
return $user->hasVerifiedEmail() && ! $discussion->isAuthoredBy($user);
}

public function convertedToThread(User $user, Discussion $discussion): bool
{
return $discussion->isAuthoredBy($user) || $user->isAdmin();
}
}
2 changes: 1 addition & 1 deletion app/Policies/NotificationPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ final class NotificationPolicy

public function markAsRead(User $user, DatabaseNotification $notification): bool
{
return $notification->notifiable->is($user); // @phpstan-ignore-line
return $notification->notifiable->is($user);
}
}
2 changes: 1 addition & 1 deletion app/Spotlight/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Article extends SpotlightCommand

protected array $synonyms = [];

public function dependencies(): ?SpotlightCommandDependencies
public function dependencies(): SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()
->add(
Expand Down
2 changes: 1 addition & 1 deletion app/Spotlight/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Discussion extends SpotlightCommand

protected array $synonyms = [];

public function dependencies(): ?SpotlightCommandDependencies
public function dependencies(): SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()
->add(
Expand Down
2 changes: 1 addition & 1 deletion app/Spotlight/Sujet.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Sujet extends SpotlightCommand
'thread',
];

public function dependencies(): ?SpotlightCommandDependencies
public function dependencies(): SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()
->add(
Expand Down
2 changes: 1 addition & 1 deletion app/Spotlight/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class User extends SpotlightCommand

protected array $synonyms = [];

public function dependencies(): ?SpotlightCommandDependencies
public function dependencies(): SpotlightCommandDependencies
{
return SpotlightCommandDependencies::collection()
->add(
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/HasReplies.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function isConversationOld(): bool
$sixMonthsAgo = now()->subMonths(6);

if ($reply = $this->replies()->latest()->first()) {
/** @var $reply Reply */
/** @var Reply $reply */
return $reply->created_at->lt($sixMonthsAgo);
}

Expand Down
3 changes: 3 additions & 0 deletions app/Traits/HasSocialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Laravel\Socialite\Contracts\User;
use Laravel\Socialite\Facades\Socialite;

/**
* @phpstan-ignore trait.unused
*/
trait HasSocialite
{
/**
Expand Down
3 changes: 3 additions & 0 deletions app/Traits/UserResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use App\Http\Resources\EnterpriseResource;
use App\Models\User;

/**
* @phpstan-ignore trait.unused
*/
trait UserResponse
{
/**
Expand Down
1 change: 1 addition & 0 deletions lang/en/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
'save' => 'Save',
'ban' => 'Ban',
'unban' => 'Cancel ban',
'confirm' => 'Confirm',

];
Loading
Loading