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:
@@ -7,12 +7,16 @@ use App\Http\Controllers\Api\V1\Chat\Requests\ChatContactsRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Requests\ChatMessagesRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Requests\ChatSendMessageRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Requests\ChatStartRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Requests\ChatStartTargetRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Requests\ChatTargetsRequest;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\ChatChannelTargetResource;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\ChatUserResource;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\ConversationResource;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\MessageResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Chat\Conversation;
|
||||
use App\Models\Chat\Message;
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use App\Models\User;
|
||||
use App\Support\ChatSupport;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -30,6 +34,59 @@ class ChatController extends Controller
|
||||
return response()->rest(ConversationResource::collection($conversations));
|
||||
}
|
||||
|
||||
/**
|
||||
* (*) Admin + chat-enabled stores for the widget picker
|
||||
*/
|
||||
public function targets(ChatTargetsRequest $request): JsonResponse
|
||||
{
|
||||
$viewer = $request->user();
|
||||
$search = $request->q;
|
||||
|
||||
$paginator = Channel::query()
|
||||
->chatTargets($viewer)
|
||||
->with('media')
|
||||
->when($search, fn ($query) => $query->where('name', 'like', '%'.$search.'%'))
|
||||
->ordered()
|
||||
->simplePaginate($request->perPage);
|
||||
|
||||
$paginator->through(fn (Channel $channel) => (new ChatChannelTargetResource($channel))->resolve());
|
||||
|
||||
return response()->json([
|
||||
'message' => 'success',
|
||||
'data' => [
|
||||
'admin' => ChatSupport::adminTarget($viewer),
|
||||
'channels' => $paginator->items(),
|
||||
],
|
||||
'pagination' => [
|
||||
'page' => $paginator->currentPage(),
|
||||
'perPage' => $paginator->perPage(),
|
||||
'count' => $paginator->count(),
|
||||
'first_page_url' => $paginator->url(1),
|
||||
'next_page_url' => $paginator->nextPageUrl(),
|
||||
'prev_page_url' => $paginator->previousPageUrl(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (*) Find or create a conversation with admin or a store
|
||||
*/
|
||||
public function startTarget(ChatStartTargetRequest $request): JsonResponse
|
||||
{
|
||||
abort_if(ChatSupport::isSupportUser($request->user()), 403);
|
||||
|
||||
if ($request->boolean('admin')) {
|
||||
$conversation = Conversation::findOrCreateSupport($request->user());
|
||||
} else {
|
||||
$channel = Channel::query()->findOrFail($request->integer('channel_id'));
|
||||
$conversation = Conversation::findOrCreateForChannel($request->user(), $channel);
|
||||
}
|
||||
|
||||
$conversation->loadForViewer($request->user());
|
||||
|
||||
return response()->rest(new ConversationResource($conversation));
|
||||
}
|
||||
|
||||
/**
|
||||
* (*) Existing support conversation (does not create)
|
||||
*/
|
||||
@@ -37,10 +94,14 @@ class ChatController extends Controller
|
||||
{
|
||||
abort_if(ChatSupport::isSupportUser($request->user()), 403);
|
||||
|
||||
$conversation = Conversation::findOneToOne(
|
||||
$request->user()->id,
|
||||
ChatSupport::user()->id
|
||||
);
|
||||
$conversation = Conversation::findSupportFor($request->user());
|
||||
|
||||
if (! $conversation && ChatSupport::userId()) {
|
||||
$conversation = Conversation::findOneToOne(
|
||||
$request->user()->id,
|
||||
ChatSupport::user()->id
|
||||
);
|
||||
}
|
||||
|
||||
if (! $conversation) {
|
||||
return response()->rest(null);
|
||||
@@ -58,10 +119,7 @@ class ChatController extends Controller
|
||||
{
|
||||
abort_if(ChatSupport::isSupportUser($request->user()), 403);
|
||||
|
||||
$conversation = Conversation::findOrCreateOneToOne(
|
||||
$request->user()->id,
|
||||
ChatSupport::user()->id
|
||||
);
|
||||
$conversation = Conversation::findOrCreateSupport($request->user());
|
||||
|
||||
$conversation->loadForViewer($request->user());
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Chat\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Validator;
|
||||
|
||||
class ChatStartTargetRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'admin' => ['sometimes', 'boolean'],
|
||||
'channel_id' => ['sometimes', 'integer', 'exists:channels,id'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator(Validator $validator): void
|
||||
{
|
||||
$validator->after(function (Validator $validator) {
|
||||
$admin = $this->boolean('admin');
|
||||
$hasChannel = $this->filled('channel_id');
|
||||
|
||||
if ($admin && $hasChannel) {
|
||||
$validator->errors()->add('channel_id', 'Choose either admin or a channel.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $admin && ! $hasChannel) {
|
||||
$validator->errors()->add('admin', 'Choose either admin or a channel.');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Chat\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChatTargetsRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'perPage' => ['nullable', 'integer', 'min:1', 'max:50'],
|
||||
];
|
||||
}
|
||||
|
||||
protected function passedValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'perPage' => $this->perPage ?: 20,
|
||||
'q' => is_string($this->q) ? trim($this->q) : null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Chat\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ChatChannelTargetResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'image' => $this->thumbnail('200x200'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,9 +16,18 @@ class ConversationResource extends JsonResource
|
||||
? $this->users->firstWhere('id', '!=', $request->user()?->id)
|
||||
: null;
|
||||
|
||||
$channel = $this->relationLoaded('channel') ? $this->channel : null;
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'thread_key' => $this->thread_key,
|
||||
'user' => $this->when($otherUser, fn () => new ChatUserResource($otherUser)),
|
||||
'channel' => $channel ? [
|
||||
'id' => $channel->id,
|
||||
'name' => $channel->name,
|
||||
'slug' => $channel->slug,
|
||||
'image' => $channel->thumbnail('200x200'),
|
||||
] : null,
|
||||
'last_message' => $this->when(
|
||||
$this->relationLoaded('latestMessage') && $this->latestMessage,
|
||||
fn () => new MessageResource($this->latestMessage)
|
||||
|
||||
@@ -12,17 +12,19 @@ use App\Models\Chat\Message;
|
||||
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 @@ class SupportChatController extends Controller
|
||||
$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 @@ class SupportChatController extends Controller
|
||||
|
||||
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 @@ class SupportChatController extends Controller
|
||||
|
||||
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