feat: add Reverb realtime messaging
This commit is contained in:
47
app/Policies/ConversationPolicy.php
Normal file
47
app/Policies/ConversationPolicy.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Conversation;
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\User;
|
||||
|
||||
class ConversationPolicy
|
||||
{
|
||||
public function view(User $user, Conversation $conversation): bool
|
||||
{
|
||||
return $this->participantRecord($user, $conversation) !== null
|
||||
&& (bool) ($conversation->is_active ?? true);
|
||||
}
|
||||
|
||||
public function send(User $user, Conversation $conversation): bool
|
||||
{
|
||||
return $this->view($user, $conversation);
|
||||
}
|
||||
|
||||
public function manageParticipants(User $user, Conversation $conversation): bool
|
||||
{
|
||||
$participant = $this->participantRecord($user, $conversation);
|
||||
|
||||
return $participant !== null && $participant->role === 'admin';
|
||||
}
|
||||
|
||||
public function rename(User $user, Conversation $conversation): bool
|
||||
{
|
||||
return $conversation->isGroup() && $this->manageParticipants($user, $conversation);
|
||||
}
|
||||
|
||||
public function joinPresence(User $user, Conversation $conversation): bool
|
||||
{
|
||||
return $this->view($user, $conversation);
|
||||
}
|
||||
|
||||
private function participantRecord(User $user, Conversation $conversation): ?ConversationParticipant
|
||||
{
|
||||
return ConversationParticipant::query()
|
||||
->where('conversation_id', $conversation->id)
|
||||
->where('user_id', $user->id)
|
||||
->whereNull('left_at')
|
||||
->first();
|
||||
}
|
||||
}
|
||||
29
app/Policies/MessagePolicy.php
Normal file
29
app/Policies/MessagePolicy.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\ConversationParticipant;
|
||||
use App\Models\Message;
|
||||
use App\Models\User;
|
||||
|
||||
class MessagePolicy
|
||||
{
|
||||
public function view(User $user, Message $message): bool
|
||||
{
|
||||
return ConversationParticipant::query()
|
||||
->where('conversation_id', $message->conversation_id)
|
||||
->where('user_id', $user->id)
|
||||
->whereNull('left_at')
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function update(User $user, Message $message): bool
|
||||
{
|
||||
return $message->sender_id === $user->id && $message->deleted_at === null;
|
||||
}
|
||||
|
||||
public function delete(User $user, Message $message): bool
|
||||
{
|
||||
return $message->sender_id === $user->id || $user->isAdmin();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user