nostolgagram/api/v1/direct_v2/threads.php
2026-06-24 21:39:10 +01:00

74 lines
No EOL
2.5 KiB
PHP

<?php
/**
* Module G2: Target Chat Logs Compilation Matrix
* Target Path: htdocs/api/v1/direct_v2/threads.php
*/
require_once __DIR__ . '/../../../config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
die(json_encode(["status" => "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);
// Grab target thread identifier sent through from our htaccess URL variables
$rawThreadId = $_GET['thread_id'] ?? '0';
$targetRecipientId = (int)str_replace('thread_', '', $rawThreadId);
$db = getDB();
$items = [];
try {
if ($targetRecipientId === 999) {
// Provide mock history details for system welcome thread
$items[] = [
"item_id" => "mock_msg_001",
"user_id" => 999,
"timestamp" => (time() - 60) * 1000000,
"item_type" => "text",
"text" => "Welcome to Direct Messages! Your chat nodes are active. Broadcast a message to test."
];
} else {
// Query database for logs between these two specific identities
$stmt = $db->prepare("SELECT * FROM direct_messages
WHERE (sender_id = ? AND recipient_id = ?)
OR (sender_id = ? AND recipient_id = ?)
ORDER BY id DESC LIMIT 50");
$stmt->execute([$userId, $targetRecipientId, $targetRecipientId, $userId]);
$messages = $stmt->fetchAll();
foreach ($messages as $msg) {
$items[] = [
"item_id" => (string)$msg['id'],
"user_id" => (int)$msg['sender_id'],
"timestamp" => (int)$msg['created_at'] * 1000000,
"item_type" => "text",
"text" => $msg['message_text']
];
}
}
// Build the container object structure for version 6.20.2 parsing mechanics
http_response_code(200);
echo json_encode([
"thread" => [
"thread_id" => "thread_" . $targetRecipientId,
"items" => $items,
"more_available" => false,
"status" => "ok"
],
"status" => "ok"
]);
exit;
} catch (PDOException $e) {
http_response_code(500);
die(json_encode(["status" => "fail", "message" => "Thread ledger lookup failure."]));
}