67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Organization;
|
|
use App\Models\Bank;
|
|
use App\Models\Admin;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class ChatController extends Controller
|
|
{
|
|
/**
|
|
* Create a new conversation with a specific profile and redirect to the chat.
|
|
*
|
|
* @param string $profileType The type of profile (user, organization, bank, admin)
|
|
* @param int $id The ID of the profile
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function startConversationWith(string $profileType, int $id)
|
|
{
|
|
// Ensure user is authenticated
|
|
if (!Auth::guard('web')->check() &&
|
|
!Auth::guard('organization')->check() &&
|
|
!Auth::guard('bank')->check() &&
|
|
!Auth::guard('admin')->check()) {
|
|
abort(403, 'Unauthorized');
|
|
}
|
|
|
|
// Map profile type to model class
|
|
$modelClass = match(strtolower($profileType)) {
|
|
'user' => User::class,
|
|
'organization' => Organization::class,
|
|
'bank' => Bank::class,
|
|
'admin' => Admin::class,
|
|
default => abort(404, 'Invalid profile type'),
|
|
};
|
|
|
|
// Find the recipient profile
|
|
$recipient = $modelClass::find($id);
|
|
|
|
if (!$recipient) {
|
|
abort(404, 'Profile not found');
|
|
}
|
|
|
|
// Check if recipient is removed
|
|
if (method_exists($recipient, 'isRemoved') && $recipient->isRemoved()) {
|
|
abort(404, 'Profile no longer available');
|
|
}
|
|
|
|
// Get the active profile using the helper function
|
|
$activeProfile = getActiveProfile();
|
|
|
|
// Prevent creating conversation with yourself
|
|
if ($activeProfile->id === $recipient->id && get_class($activeProfile) === get_class($recipient)) {
|
|
return redirect()->route('chats')->with('error', __('You cannot start a conversation with yourself.'));
|
|
}
|
|
|
|
// Create or get existing conversation
|
|
$conversation = $activeProfile->createConversationWith($recipient);
|
|
|
|
// Redirect to the chat page
|
|
return redirect()->route('chat', ['conversation' => $conversation->id]);
|
|
}
|
|
}
|