Enhance chat functionality: add support for vendor-specific chat channels, implement methods for managing conversations with admin and store owners, and improve conversation payload structure. Update middleware and policies for better access control, and refine chat UI to indicate view-only status for store owner replies.
This commit is contained in:
1 parent
c55694ec8c
commit
5c17c35741
23 files changed
+729
-59
No files matched your search
@@ -12,17 +12,19 @@
|
||||
use App\Models\User;
|
||||
use App\Support\ChatSupport;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class SupportChatController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
public function index(Request $request): View
|
||||
{
|
||||
if (app()->bound('debugbar')) {
|
||||
app('debugbar')->disable();
|
||||
}
|
||||
|
||||
$key = config('broadcasting.connections.reverb.key');
|
||||
$actor = $request->user();
|
||||
|
||||
return view('nova.chat', [
|
||||
'chatCss' => $this->panelAsset('css/chat.css'),
|
||||
@@ -32,23 +34,30 @@ public function index(): View
|
||||
$this->panelAsset('js/chat.js'),
|
||||
])),
|
||||
'pusher' => $this->chatPusherConfig($key),
|
||||
'inboxChannel' => ChatSupport::isOperator($actor)
|
||||
? 'chat.support'
|
||||
: 'chat.inbox.'.$actor->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function conversations(): JsonResponse
|
||||
public function conversations(Request $request): JsonResponse
|
||||
{
|
||||
$support = ChatSupport::user();
|
||||
$actor = $request->user();
|
||||
|
||||
$conversations = Conversation::inboxFor($support)
|
||||
->map(fn (Conversation $conversation) => ChatSupport::conversationPayload($conversation, $support, true))
|
||||
$conversations = ChatSupport::isOperator($actor)
|
||||
? Conversation::inboxForStaff()
|
||||
: Conversation::inboxForVendor($actor);
|
||||
|
||||
$payload = $conversations
|
||||
->map(fn (Conversation $conversation) => ChatSupport::conversationPayload($conversation, $actor, true))
|
||||
->values();
|
||||
|
||||
return response()->rest($conversations);
|
||||
return response()->rest($payload);
|
||||
}
|
||||
|
||||
public function messages(ChatMessagesRequest $request, Conversation $conversation): JsonResponse
|
||||
{
|
||||
$this->assertSupportConversation($conversation);
|
||||
$this->assertCanView($request->user(), $conversation);
|
||||
|
||||
$paginator = $conversation->messages()
|
||||
->with('user')
|
||||
@@ -62,10 +71,10 @@ public function messages(ChatMessagesRequest $request, Conversation $conversatio
|
||||
|
||||
public function send(ChatSendMessageRequest $request, Conversation $conversation): JsonResponse
|
||||
{
|
||||
$support = $this->assertSupportConversation($conversation);
|
||||
$sender = $this->assertCanReply($request->user(), $conversation);
|
||||
|
||||
$message = $conversation->messages()->create([
|
||||
'user_id' => $support->id,
|
||||
'user_id' => $sender->id,
|
||||
'body' => $request->validated('body'),
|
||||
]);
|
||||
|
||||
@@ -78,23 +87,39 @@ public function send(ChatSendMessageRequest $request, Conversation $conversation
|
||||
|
||||
public function read(Conversation $conversation): JsonResponse
|
||||
{
|
||||
$support = $this->assertSupportConversation($conversation);
|
||||
$sender = $this->assertCanReply(request()->user(), $conversation);
|
||||
|
||||
$conversation->messages()
|
||||
->where('user_id', '!=', $support->id)
|
||||
->where('user_id', '!=', $sender->id)
|
||||
->whereNull('seen_at')
|
||||
->update(['seen_at' => now()]);
|
||||
|
||||
return response()->rest();
|
||||
}
|
||||
|
||||
private function assertSupportConversation(Conversation $conversation): User
|
||||
private function assertCanView(User $actor, Conversation $conversation): void
|
||||
{
|
||||
$support = ChatSupport::user();
|
||||
if (ChatSupport::isOperator($actor)) {
|
||||
return;
|
||||
}
|
||||
|
||||
abort_unless($conversation->hasParticipant($support), 403);
|
||||
abort_unless($conversation->hasParticipant($actor), 403);
|
||||
$conversation->loadMissing('channel');
|
||||
abort_unless(ChatSupport::ownsChannel($actor, $conversation->channel), 403);
|
||||
}
|
||||
|
||||
return $support;
|
||||
private function assertCanReply(User $actor, Conversation $conversation): User
|
||||
{
|
||||
abort_unless(ChatSupport::canReply($actor, $conversation), 403);
|
||||
|
||||
if (ChatSupport::isOperator($actor)) {
|
||||
$support = ChatSupport::user();
|
||||
abort_unless($conversation->hasParticipant($support), 403);
|
||||
|
||||
return $support;
|
||||
}
|
||||
|
||||
return $actor;
|
||||
}
|
||||
|
||||
private function panelAsset(string $relative): string
|
||||
|
||||
Reference in new issue
Block a user