32 lines
825 B
PHP
32 lines
825 B
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Events\MessageSent;
|
|
use App\Models\User;
|
|
|
|
final readonly class SendMessageAction
|
|
{
|
|
public function __construct(private CreateOrGetInboxAction $inboxAction) {}
|
|
|
|
/**
|
|
* @throws \Throwable
|
|
*/
|
|
public function execute(User $sender, User $recipient, array $data): void
|
|
{
|
|
// find the inbox between the two users
|
|
\DB::beginTransaction();
|
|
$inbox = $this->inboxAction->execute($recipient, $sender);
|
|
$inbox->last_message = $data['message'];
|
|
$inbox->last_user_id = $sender->id;
|
|
$inbox->save();
|
|
$message = $inbox->messages()->create([
|
|
'message' => $data['message'],
|
|
'user_id' => $sender->id,
|
|
]);
|
|
|
|
broadcast(new MessageSent($message))->toOthers();
|
|
\DB::commit();
|
|
}
|
|
}
|