bound('debugbar')) { app('debugbar')->disable(); } $key = config('broadcasting.connections.reverb.key'); return view('nova.chat', [ 'chatCss' => $this->panelAsset('css/chat.css'), 'chatScripts' => array_values(array_filter([ $key ? $this->panelAsset('js/pusher.min.js') : null, $key ? $this->panelAsset('js/echo.iife.js') : null, $this->panelAsset('js/chat.js'), ])), 'pusher' => $this->chatPusherConfig($key), ]); } public function conversations(): JsonResponse { $support = ChatSupport::user(); $conversations = Conversation::inboxFor($support) ->map(fn (Conversation $conversation) => ChatSupport::conversationPayload($conversation, $support, true)) ->values(); return response()->rest($conversations); } public function messages(ChatMessagesRequest $request, Conversation $conversation): JsonResponse { $this->assertSupportConversation($conversation); $paginator = $conversation->messages() ->with('user') ->latest() ->simplePaginate($request->perPage); $paginator->through(fn (Message $message) => (new MessageResource($message))->resolve()); return response()->rest_paginate($paginator); } public function send(ChatSendMessageRequest $request, Conversation $conversation): JsonResponse { $support = $this->assertSupportConversation($conversation); $message = $conversation->messages()->create([ 'user_id' => $support->id, 'body' => $request->validated('body'), ]); $message->load('user'); broadcast(new MessageSent($message))->toOthers(); return response()->rest(new MessageResource($message), 201); } public function read(Conversation $conversation): JsonResponse { $support = $this->assertSupportConversation($conversation); $conversation->messages() ->where('user_id', '!=', $support->id) ->whereNull('seen_at') ->update(['seen_at' => now()]); return response()->rest(); } private function assertSupportConversation(Conversation $conversation): User { $support = ChatSupport::user(); abort_unless($conversation->hasParticipant($support), 403); return $support; } private function panelAsset(string $relative): string { $path = public_path('assets/panel/'.$relative); abort_unless(is_readable($path), 500, "Missing chat asset [{$relative}]."); return (string) file_get_contents($path); } /** * Browser websocket host. Reverb itself stays on 127.0.0.1:8080; * production browsers connect through nginx at wss://mm.com.tm/app. * * @return array{key: mixed, host: string, port: int, scheme: string} */ private function chatPusherConfig(mixed $key): array { $requestHost = request()->getHost(); $public = ! in_array($requestHost, ['127.0.0.1', 'localhost', '::1', '0.0.0.0'], true); if ($public) { $https = request()->secure() || request()->header('X-Forwarded-Proto') === 'https' || str_contains($requestHost, '.'); return [ 'key' => $key, 'host' => $requestHost, 'port' => $https ? 443 : 80, 'scheme' => $https ? 'https' : 'http', ]; } return [ 'key' => $key, 'host' => (string) config('broadcasting.connections.reverb.options.host'), 'port' => (int) config('broadcasting.connections.reverb.options.port', 8080), 'scheme' => (string) config('broadcasting.connections.reverb.options.scheme', 'http'), ]; } }