"fail", "message" => "Method not allowed."])); } $sessionCookie = $_COOKIE['sessionid'] ?? ''; if (empty($sessionCookie) || strpos($sessionCookie, 'nostolga_sess_') !== 0) { http_response_code(401); die(json_encode(["status" => "fail", "message" => "Unauthorized session context."])); } $userId = (int)str_replace('nostolga_sess_', '', $sessionCookie); // Gather values pushed from the app UI form $text = $_POST['text'] ?? ''; $recipientJson = $_POST['recipients'] ?? ''; // App packages recipient IDs inside an explicit JSON format array string // Parse target out of the JSON parameter payload $recipientId = 0; if (!empty($recipientJson)) { $recipientsArray = json_decode($recipientJson, true); // Grab the first ID listed inside the array $recipientId = (int)($recipientsArray[0] ?? 0); } // Fallback: Check standard direct target params if the array wrapper was bypassed if ($recipientId === 0) { $recipientId = (int)($_POST['recipient_users'] ?? 0); } if (empty($text) || $recipientId === 0) { http_response_code(400); die(json_encode(["status" => "fail", "message" => "Required payload message configurations missing."])); } $db = getDB(); try { $timestamp = time(); // Write message into table matrix ledger $stmt = $db->prepare("INSERT INTO direct_messages (sender_id, recipient_id, message_text, created_at) VALUES (?, ?, ?, ?)"); $stmt->execute([$userId, $recipientId, $text, $timestamp]); $newMsgId = $db->lastInsertId(); // Mirror the broadcast output structure format so it updates instantly in the chat UI screen http_response_code(200); echo json_encode([ "payload" => [ "item_id" => (string)$newMsgId, "timestamp" => $timestamp * 1000000, "item_type" => "text", "text" => $text ], "status" => "ok" ]); exit; } catch (PDOException $e) { http_response_code(500); die(json_encode(["status" => "fail", "message" => "Data broadcast recording sequence failed."])); }