Skip to content

Commit

Permalink
Messages queue configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Raccoon254 committed Jun 9, 2024
1 parent 22b4cf7 commit 08a4423
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/Jobs/ProcessSentMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Jobs;

use App\Models\Message;
use App\Notifications\MessageSentNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessSentMessages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Message $message;
/**
* Create a new job instance.
*/
public function __construct(Message $message)
{
$this->message = $message;
}

/**
* Execute the job.
*/
public function handle(): void
{
// Send a notification to the recipient
$this->message->receiver->notify(new MessageSentNotification());
}
}
2 changes: 2 additions & 0 deletions app/Livewire/SendMessageInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Livewire;

use App\Jobs\ProcessSentMessages;
use App\Models\Attachment;
use App\Models\Message;
use App\Models\User;
Expand Down Expand Up @@ -75,6 +76,7 @@ public function sendMessage(): void
$this->reset('attachments', 'newMessage');

$this->dispatch('messagesSent');
ProcessSentMessages::dispatch($message)->delay(now()->addSeconds(10));
}

public function render(): View
Expand Down
58 changes: 58 additions & 0 deletions app/Notifications/MessageSentNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Notifications;

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

class MessageSentNotification extends Notification implements ShouldQueue
{
use Queueable;
protected Message $message;
/**
* Create a new notification instance.
*/
public function __construct(Message $message)
{
$this->message = $message;
}

/**
* 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)
->line('You have a new message from ' . $this->message->sender->name)
->line($this->message->content)
->action('View Message', url('/messages'))
->line('Thank you for using our application!');
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'message_id' => $this->message->id,
'sender_name' => $this->message->sender->name,
'message_content' => $this->message->content,
];
}
}

0 comments on commit 08a4423

Please sign in to comment.