Uname:Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64

Base Dir : /var/www/machinox.in

User : root


403WebShell
403Webshell
Server IP : 13.235.167.51  /  Your IP : 216.73.217.116
Web Server : Apache
System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64
User : root ( 0)
PHP Version : 8.2.29
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /var/www/app.machinox.in/app/Http/Controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/app.machinox.in/app/Http/Controllers/MessageController.php
<?php

namespace App\Http\Controllers;

use App\Actions\MessageAction;
use App\Http\Requests\StoreMessageRequest;
use App\Models\Advertisement;
use App\Models\GroupChat;
use App\Models\Message;
use App\Models\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MessageController extends Controller
{

    public function store(StoreMessageRequest $request, MessageAction $action, $userId)
    {
        try {
            $res = $action->execute($request, $userId);

            return $this->sendSuccess($res->message, ['path' => property_exists($res, 'Path') ? $res->Path : null]);
        } catch (Exception $e) {
            return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
        }
    }


    public function getAdChats($adId)
    {
        try {
            $ownerId = USER_ID();

            // Ensure the logged-in user is the ad owner
            $ad = Advertisement::where('uuid', $adId)
                ->where('user_id', $ownerId)
                ->first();

            if (!$ad) {
                return response()->json(['message' => 'You do not own this ad.'], 403);
            }

            // Get all group chats for this ad
            $groupChats = GroupChat::with(['user1', 'user2', 'latestMessage'])
                ->where('ad_id', $ad->id)
                ->where(function ($query) use ($ownerId) {
                    $query->where('user1_id', $ownerId)
                        ->orWhere('user2_id', $ownerId);
                })
                ->get();

            $chatList = $groupChats->map(function ($groupChat) use ($ownerId) {
                // Determine the chat partner
                $chatPartner = $groupChat->user1_id == $ownerId ? $groupChat->user2 : $groupChat->user1;

                $lastMessage = $groupChat->latestMessage;
                $unreadCount = $groupChat->messages()
                    ->where('sender_id', '!=', $ownerId)
                    ->where('is_read', false)
                    ->count();

                return [
                    'group_chat_id' => $groupChat->id,
                    'ad_uuid' => $groupChat->ad->uuid,
                    'chat_partner_id' => $chatPartner->id,
                    'chat_partner_name' => $chatPartner->name,
                    'chat_partner_avatar' => $chatPartner->profile_image ?? null,
                    'last_message' => $lastMessage?->message ?? null,
                    'type' => $lastMessage?->type ?? 'TEXT',
                    'sent_at' => $lastMessage?->created_at?->format('h:i A') ?? null,
                    'unread_count' => $unreadCount,
                ];
            });

            return $this->sendSuccess(trans('messages.OK'), ['list' => $chatList]);
        } catch (Exception $th) {
            return $this->sendError($th->getMessage());
        }
    }

    public function myAllChats()
    {
        try {
            $userId = USER_ID();

            $groupChats = GroupChat::with([
                'ad',
                'ad.user',
                'user1',
                'user2',
                'messages' => fn($q) => $q->latest()->limit(1)
            ])
                ->where(function ($q) use ($userId) {
                    $q->where('user1_id', $userId)
                        ->orWhere('user2_id', $userId);
                })
                ->get();

            $chats = $groupChats->map(function ($groupChat) use ($userId) {
                $chatPartner = $groupChat->user1_id == $userId ? $groupChat->user2 : $groupChat->user1;

                if (!$chatPartner || $chatPartner->id == $userId) {
                    return null;
                }

                $lastMessage = $groupChat->messages->first();
                $unreadCount = $groupChat->messages()
                    ->where('sender_id', $chatPartner->id)
                    ->where('is_read', false)
                    ->count();
                $locale = app()->getLocale(); // e.g., 'en', 'hi'
                $timezone = 'Asia/Kolkata';
                Carbon::setLocale($locale);
                return [
                    'group_chat_id' => $groupChat->id,
                    'ad_id' => $groupChat->ad->id,
                    'ad_uuid' => $groupChat->ad->uuid,
                    'ad_title' => $groupChat->ad->title,
                    'chat_partner_id' => $chatPartner->id,
                    'chat_partner_name' => $chatPartner->name,
                    'chat_partner_avatar' => $chatPartner->profile_image ?? null,
                    'last_message' => $lastMessage?->message,
                    'type' => $lastMessage?->type ?? 'TEXT',
                    'sent_at' => optional($lastMessage->created_at)
                        ->copy()
                        ->setTimezone($timezone)
                        ->translatedFormat('h:i A'),
                    'unread_count' => $unreadCount,
                ];
            })->filter()->unique('group_chat_id')->values();

            return $this->sendSuccess(trans('messages.OK'), ['list' => $chats]);
        } catch (Exception $th) {
            return $this->sendError($th->getMessage());
        }
    }

    public function messagesBetweenUsers($adId, $otherUserId)
    {
        $userId = USER_ID();

        $ad = Advertisement::with('user')->where('uuid', $adId)->firstOrFail();
        $user = User::findOrFail($otherUserId);
        // Fetch the single group chat for these two users under this ad
        $groupChat = GroupChat::where('ad_id', $ad->id)
            ->where(function ($q) use ($userId, $otherUserId) {
                $q->where(function ($q2) use ($userId, $otherUserId) {
                    $q2->where('user1_id', $userId)->where('user2_id', $otherUserId);
                })->orWhere(function ($q2) use ($userId, $otherUserId) {
                    $q2->where('user1_id', $otherUserId)->where('user2_id', $userId);
                });
            })
            ->first();

        if (!$groupChat) {
            return $this->sendSuccess(trans('messages.OK'), [
                'ad_id' => $ad->id,
                'user_id' => $userId,
                'user' => $user,
                'list' => [],
            ]);
        }

        // Mark unread messages from the other user as read
        Message::where('gd_id', $groupChat->id)
            ->where('sender_id', $otherUserId)
            ->where('is_read', false)
            ->update(['is_read' => true]);

        // Retrieve all messages in this thread
        $messages = Message::where('gd_id', $groupChat->id)
            ->orderBy('created_at')
            ->get();

        return $this->sendSuccess(trans('messages.OK'), [
            'ad_id' => $ad->id,
            'user_id' => $userId,
            'user' => $user,
            'list' => $messages,
        ]);
    }

    public function getChatsCount()
    {
        try {
            $userId = USER_ID();

            $groupChats = GroupChat::with([
                'user1',
                'user2',
                'messages' => fn($q) => $q->latest()->limit(1),
            ])
                ->where(function ($q) use ($userId) {
                    $q->where('user1_id', $userId)
                        ->orWhere('user2_id', $userId);
                })
                ->get();

            $unreadChatsCount = $groupChats->filter(function ($groupChat) use ($userId) {
                $chatPartnerId = $groupChat->user1_id == $userId ? $groupChat->user2_id : $groupChat->user1_id;

                return $groupChat->messages()
                    ->where('sender_id', $chatPartnerId)
                    ->where('is_read', false)
                    ->exists();
            })->count();

            return $this->sendSuccess(trans('messages.OK'), [
                'total_unread_chat_count' => $unreadChatsCount,
            ]);
        } catch (Exception $th) {
            return $this->sendError($th->getMessage());
        }
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit