feat: add Reverb realtime messaging
This commit is contained in:
46
app/Events/ConversationUpdated.php
Normal file
46
app/Events/ConversationUpdated.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ConversationUpdated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public Conversation $conversation,
|
||||
public string $reason,
|
||||
) {
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel('user.' . $this->userId)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'conversation.updated';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'conversation.updated',
|
||||
'reason' => $this->reason,
|
||||
'conversation' => app(MessagingPayloadFactory::class)->conversationSummary($this->conversation, $this->userId),
|
||||
];
|
||||
}
|
||||
}
|
||||
51
app/Events/MessageCreated.php
Normal file
51
app/Events/MessageCreated.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\Message;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class MessageCreated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(
|
||||
public Conversation $conversation,
|
||||
public Message $message,
|
||||
int $originUserId,
|
||||
) {
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
|
||||
if ($originUserId === (int) $message->sender_id) {
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel('conversation.' . $this->conversation->id)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'message.created';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'message.created',
|
||||
'conversation_id' => (int) $this->conversation->id,
|
||||
'message' => app(MessagingPayloadFactory::class)->message($this->message, (int) $this->message->sender_id),
|
||||
];
|
||||
}
|
||||
}
|
||||
45
app/Events/MessageDeleted.php
Normal file
45
app/Events/MessageDeleted.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class MessageDeleted implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(public Message $message)
|
||||
{
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel('conversation.' . $this->message->conversation_id)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'message.deleted';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'message.deleted',
|
||||
'conversation_id' => (int) $this->message->conversation_id,
|
||||
'message_id' => (int) $this->message->id,
|
||||
'uuid' => (string) $this->message->uuid,
|
||||
'deleted_at' => optional($this->message->deleted_at ?? now())?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
51
app/Events/MessageRead.php
Normal file
51
app/Events/MessageRead.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class MessageRead implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(
|
||||
public Conversation $conversation,
|
||||
public ConversationParticipant $participant,
|
||||
public User $reader,
|
||||
) {
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel('conversation.' . $this->conversation->id)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'message.read';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'message.read',
|
||||
'conversation_id' => (int) $this->conversation->id,
|
||||
'user' => app(MessagingPayloadFactory::class)->userSummary($this->reader),
|
||||
'last_read_message_id' => $this->participant->last_read_message_id ? (int) $this->participant->last_read_message_id : null,
|
||||
'last_read_at' => optional($this->participant->last_read_at)?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class MessageSent
|
||||
{
|
||||
use Dispatchable, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public int $conversationId,
|
||||
public int $messageId,
|
||||
public int $senderId,
|
||||
) {}
|
||||
}
|
||||
44
app/Events/MessageUpdated.php
Normal file
44
app/Events/MessageUpdated.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\Message;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class MessageUpdated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(public Message $message)
|
||||
{
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PrivateChannel('conversation.' . $this->message->conversation_id)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'message.updated';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'message.updated',
|
||||
'conversation_id' => (int) $this->message->conversation_id,
|
||||
'message' => app(MessagingPayloadFactory::class)->message($this->message),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,46 @@
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TypingStarted
|
||||
class TypingStarted implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, SerializesModels;
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(
|
||||
public int $conversationId,
|
||||
public int $userId,
|
||||
) {}
|
||||
public User $user,
|
||||
) {
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PresenceChannel('conversation.' . $this->conversationId)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'typing.started';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'typing.started',
|
||||
'conversation_id' => $this->conversationId,
|
||||
'user' => app(MessagingPayloadFactory::class)->userSummary($this->user),
|
||||
'expires_in_ms' => (int) config('messaging.typing.ttl_seconds', 8) * 1000,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,15 +2,45 @@
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\Messaging\MessagingPayloadFactory;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PresenceChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class TypingStopped
|
||||
class TypingStopped implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, SerializesModels;
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public bool $afterCommit = true;
|
||||
public string $queue;
|
||||
|
||||
public function __construct(
|
||||
public int $conversationId,
|
||||
public int $userId,
|
||||
) {}
|
||||
public User $user,
|
||||
) {
|
||||
$this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
|
||||
$this->dontBroadcastToCurrentUser();
|
||||
}
|
||||
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new PresenceChannel('conversation.' . $this->conversationId)];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'typing.stopped';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'event' => 'typing.stopped',
|
||||
'conversation_id' => $this->conversationId,
|
||||
'user' => app(MessagingPayloadFactory::class)->userSummary($this->user),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user