Файловый менеджер - Редактировать - /home/clickysoft/public_html/somni.clickysoft.net/app/Http/Controllers/ChatMessageController.php
Назад
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\ChatMessage; use App\Models\PropertyUnitTenant; use App\Models\User; use Carbon\Carbon; use App\Repositories\LandlordRepository; use App\Repositories\UserRepository; use App\Repositories\SubscriptionRepository; use App\Repositories\PropertyRepository; use App\Repositories\TenantRepository; use App\Repositories\NotificationRepository; use App\Interfaces\LandlordRepositoryInterface; use App\Interfaces\SubscriptionRepositoryInterface; use App\Interfaces\UserRepositoryInterface; use App\Interfaces\PropertyRepositoryInterface; use App\Interfaces\TenantRepositoryInterface; use App\Interfaces\NotificationRepositoryInterface; class ChatMessageController extends Controller { // private LandlordRepositoryInterface $landlordInterfaceObj; // private UserRepositoryInterface $userInterfaceObj; // private SubscriptionRepositoryInterface $subscriptionInterfaceObj; // private PropertyRepositoryInterface $propertyInterfaceObj; // private TenantRepositoryInterface $tenantInterfaceObj; private NotificationRepositoryInterface $notificationInterfaceObj; public function __construct(UserRepository $userRepo, LandlordRepository $landlordRepo, SubscriptionRepository $subscriptionRepo, PropertyRepository $propertyRepo, TenantRepository $tenantRepo, NotificationRepository $notificationRepo) { // $this->landlordInterfaceObj = $landlordRepo; // $this->userInterfaceObj = $userRepo; // $this->subscriptionInterfaceObj = $subscriptionRepo; // $this->propertyInterfaceObj = $propertyRepo; // $this->tenantInterfaceObj = $tenantRepo; $this->notificationInterfaceObj = $notificationRepo; } public function index() { $user = User::find(auth()->user()->id); $propertyTenants = collect(); $combinedData = []; // Initialize an empty array to store combined data if ($user->isLandlord) { $propertyTenants = $user->getTenantNamesAndIds(); } if ($user->isTenant) { $propertyTenant = PropertyUnitTenant::where('tenant_user_id', $user->id)->first(); if ($propertyTenant) { $propertyTenants = collect([ [ 'sender_id' => $propertyTenant->property->user->id, 'name' => $propertyTenant->property->user->name, ] ]); } } // Retrieve all landlord and tenant users $allUsers = $propertyTenants->pluck('sender_id')->merge([$user->id])->unique(); // Fetch chat messages for each combination of users foreach ($propertyTenants as $propertyTenant) { $senderId = $propertyTenant['sender_id']; $combinedData[] = [ 'sender_id' => $senderId, 'name' => $propertyTenant['name'], 'messages' => ChatMessage::where(function ($query) use ($user, $senderId) { $query->where(function ($q) use ($user, $senderId) { $q->where('sender_id', $user->id)->where('receiver_id', $senderId); })->orWhere(function ($q) use ($user, $senderId) { $q->where('sender_id', $senderId)->where('receiver_id', $user->id); }); })->orderBy('created_at', 'asc')->get(), ]; } $nCount = $this->notificationInterfaceObj->unreadCount($user->id); $notifications = $this->notificationInterfaceObj->recentNotifications($user->id); if($user->roles()->where('title', 'Tenant')->exists()){ return view('chat.message', compact('user', 'combinedData', 'propertyTenants', 'nCount', 'notifications')); }else{ $nCount = $nCount; return view('chat.message', compact('user', 'combinedData', 'propertyTenants', 'nCount', 'notifications')); } } public function getUserData() { $user = auth()->user(); $userData = [ 'name' => $user->name, ]; // Get the server time and calculate the time difference $serverTime = Carbon::now(); $timeDifference = $serverTime->diffForHumans(); return response()->json([ 'userData' => $userData, 'serverTime' => $timeDifference, ]); } public function socket() { $messages = ChatMessage::all(); return response()->json(['messages' => $messages]); } public function sendMessage(Request $request) { $message = new ChatMessage(); $message->sender_id = $request->sender_id; $message->receiver_id = $request->receiver_id; $message->message = $request->message; $message->save(); if ($request->hasFile('file')) { $message->addMedia($request->file('file')) ->toMediaCollection('file'); } return response()->json(['success' => true]); } public function markAsRead(Request $request) { $message = ChatMessage::find($request->id); $senderDiv = null; $html = null; if (auth()->user()->id === $message->receiver_id) { $message->status = 'read'; $message->save(); $sender = User::find($message->sender_id); // TODO: this code will be correct $senderDiv = 'sender-left-' . $message->sender_id . '-' . auth()->user()->id; $html = '<div class="chat-message-left read"> <span class="chat-username-time">' . $sender->name . ', ' . $message->created_at->diffForHumans() . '</span> <div class="cht-box"> <p>' . $message->message . '</p> </div> </div>'; } $count = ChatMessage::where('receiver_id', auth()->user()->id) ->where('sender_id', $request->sender_id) ->where('status', 'unread') ->count(); $countDiv = '.counter-' . $request->sender_id; return response()->json([ 'success' => true, 'senderdiv' => $senderDiv, 'html' => $html, 'count' => $count, 'countdiv' => $countDiv ]); } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка