Implement chat support functionality: update .env.example for Reverb integration, enhance ChatController with support conversation methods, and modify Conversation model for improved chat handling. Add middleware for chat operator checks and update broadcasting configuration for Reverb.
This commit is contained in:
92
app/Support/ChatSupport.php
Normal file
92
app/Support/ChatSupport.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\ChatUserResource;
|
||||
use App\Http\Controllers\Api\V1\Chat\Resources\MessageResource;
|
||||
use App\Models\Chat\Conversation;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ChatSupport
|
||||
{
|
||||
public static function userId(): ?int
|
||||
{
|
||||
$id = (int) config('chat.support_user_id');
|
||||
|
||||
return $id > 0 ? $id : null;
|
||||
}
|
||||
|
||||
public static function user(): User
|
||||
{
|
||||
$id = static::userId();
|
||||
|
||||
abort_unless($id, 422, 'Chat support user is not configured.');
|
||||
|
||||
$user = User::query()->find($id);
|
||||
|
||||
abort_unless($user, 422, 'Chat support user was not found.');
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function isSupportUser(?User $user): bool
|
||||
{
|
||||
$id = static::userId();
|
||||
|
||||
return $user !== null && $id !== null && (int) $user->id === $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nova staff who may operate the support inbox (vendors excluded).
|
||||
*/
|
||||
public static function isOperator(?User $user): bool
|
||||
{
|
||||
if ($user === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->isMe() || $user->isAdmin() || $user->isDirektor() || $user->isManager();
|
||||
}
|
||||
|
||||
public static function conversationIncludesSupport(int $conversationId): bool
|
||||
{
|
||||
$id = static::userId();
|
||||
|
||||
if ($id === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return DB::table('conversation_user')
|
||||
->where('conversation_id', $conversationId)
|
||||
->where('user_id', $id)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function conversationPayload(Conversation $conversation, User $viewer, bool $includePhone = false): array
|
||||
{
|
||||
$conversation->loadMissing(['users', 'latestMessage.user']);
|
||||
|
||||
$otherUser = $conversation->users->firstWhere('id', '!=', $viewer->id);
|
||||
|
||||
$user = $otherUser ? (new ChatUserResource($otherUser))->resolve() : null;
|
||||
|
||||
if ($includePhone && $otherUser && is_array($user)) {
|
||||
$user['phone_number'] = $otherUser->phone_number;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $conversation->id,
|
||||
'user' => $user,
|
||||
'last_message' => $conversation->latestMessage
|
||||
? (new MessageResource($conversation->latestMessage))->resolve()
|
||||
: null,
|
||||
'unread_count' => (int) ($conversation->unread_count ?? 0),
|
||||
'created_at' => $conversation->created_at,
|
||||
'updated_at' => $conversation->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user