nostolgagram/NostolgaGram/api/v1/feed/timeline.php
2026-06-24 21:33:55 +01:00

131 lines
No EOL
4.8 KiB
PHP

<?php
/**
* Module D: Timeline Feed Compilation Engine (Database Driven)
* Target Path: htdocs/api/v1/feed/timeline.php
*/
// Inherit the universal configuration hub
require_once __DIR__ . '/../../../config.php';
// Instagram 6.20.2 requests timeline data streams using GET
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
die(json_encode(["status" => "fail", "message" => "Method not allowed. Use GET query pipeline."]));
}
// 1. Optional Session Catching (For custom tailored feeds or liking flags later)
$sessionCookie = $_COOKIE['sessionid'] ?? '';
$currentUserId = 0;
if (!empty($sessionCookie) && strpos($sessionCookie, 'nostolga_sess_') === 0) {
$currentUserId = (int)str_replace('nostolga_sess_', '', $sessionCookie);
}
$db = getDB();
try {
/**
* 2. Pull Live Posts Linked with Creator Profiles
*
* NOTE: As you expand your database schema, ensure you create a `posts` table containing:
* id (INT AUTO_INCREMENT), user_id (INT), media_url (VARCHAR), caption (TEXT), created_at (INT)
*
* This query falls back gracefully to standard dummy content if your live table is empty.
*/
$posts = [];
// Check if the posts table exists in phpMyAdmin before running
$tableCheck = $db->query("SHOW TABLES LIKE 'posts'")->rowCount();
if ($tableCheck > 0) {
$query = "SELECT p.*, u.username, u.full_name, u.profile_pic
FROM posts p
JOIN users u ON p.user_id = u.id
ORDER BY p.id DESC LIMIT 30";
$stmt = $db->query($query);
$posts = $stmt->fetchAll();
}
$feedItems = [];
// 3. Loop through your live data records and structuralize them for Instagram 6.20.2
foreach ($posts as $post) {
$feedItems[] = [
"pk" => (int)$post['id'],
"id" => $post['id'] . "_" . $post['user_id'],
"device_timestamp" => (int)$post['created_at'],
"media_type" => 1, // 1 = Standard Square Photo Image Layout
"code" => "post_" . $post['id'],
"image_versions2" => [
"candidates" => [
[
"url" => $post['media_url'],
"width" => 600,
"height" => 600
]
]
],
"user" => [
"pk" => (int)$post['user_id'],
"username" => $post['username'],
"full_name" => $post['full_name'],
"profile_pic_url" => $post['profile_pic'] ?? 'https://picsum.photos/150/150',
"is_private" => false
],
"caption" => [
"text" => $post['caption'] ?? '',
"created_at" => (int)$post['created_at']
],
"like_count" => 0, // Expand later via a likes mapping table
"has_liked" => false,
"comment_count" => 0
];
}
// 4. Fallback Architecture: Show an initial welcome item if your database table is still empty
if (empty($feedItems)) {
$feedItems[] = [
"pk" => 999999999,
"id" => "999999999_1",
"device_timestamp" => time(),
"media_type" => 1,
"code" => "welcome_post",
"image_versions2" => [
"candidates" => [
[
"url" => "https://res.cloudinary.com/demo/image/upload/c_fill,g_auto,w_600,h_600/sample.jpg",
"width" => 600,
"height" => 600
]
]
],
"user" => [
"pk" => 1,
"username" => "nostolgagram_system",
"full_name" => "System Hub Core",
"profile_pic_url" => "https://picsum.photos/150/150",
"is_private" => false
],
"caption" => [
"text" => "Welcome to NostolgaGram! Your dynamic timeline engine is completely online. Start posting media to fill up your feed!",
"created_at" => time()
],
"like_count" => 1337,
"has_liked" => false,
"comment_count" => 0
];
}
// 5. Package output matching legacy format structures
http_response_code(200);
echo json_encode([
"items" => $feedItems,
"num_results" => count($feedItems),
"more_available" => false,
"status" => "ok"
]);
exit;
} catch (PDOException $e) {
http_response_code(500);
die(json_encode(["status" => "fail", "message" => "Feed matrix assembly array sequence dropped."]));
}