connect('127.0.0.1', 6379); $posts = []; // Define $posts early to avoid undefined errors // === Handle POST === if ($_SERVER['REQUEST_METHOD'] === 'POST') { $postData = json_decode(file_get_contents('php://input'), true); $parentPostId = htmlspecialchars(trim($postData['postId'] ?? '')); $userid = 'guest'; // Redis keys $cacheKeys = [ 'generalcomment' => "generalcomment:$parentPostId", 'dbcomments' => "dbcomments:$parentPostId", 'selfreply' => "selfreplythread:$parentPostId", 'adminreply' => "adminreplythread:$parentPostId", ]; // Check if any cache list is missing $needRefresh = !( $redis->exists($cacheKeys['generalcomment']) && $redis->exists($cacheKeys['dbcomments']) && $redis->exists($cacheKeys['selfreply']) && $redis->exists($cacheKeys['adminreply']) ); if ($needRefresh) { // Clear old lists if any $redis->del(...array_values($cacheKeys)); $stmt = $conn->prepare("SELECT * FROM comments WHERE parentpostid = ?"); $stmt->bind_param('s', $parentPostId); $stmt->execute(); $res = $stmt->get_result(); while ($row = $res->fetch_assoc()) { $postid = $row['postid']; $selfreply = $row['selfreply'] ?? 'no'; $author = $row['defaultusername'] ?? ''; $parentAuthor = $row['parentpostauthor'] ?? ''; // Cache main post $redis->setex("posts:$postid", 900, json_encode($row)); // Distribute to appropriate cache lists $redis->rpush($cacheKeys['generalcomment'], $postid); if ($selfreply === 'yes') { $redis->rpush($cacheKeys['selfreply'], $postid); } elseif ($author === $parentAuthor) { $redis->rpush($cacheKeys['adminreply'], $postid); } else { $redis->rpush($cacheKeys['dbcomments'], $postid); } } $stmt->close(); } // Now pull comments in priority order $adminReplies = $redis->lrange($cacheKeys['adminreply'], 0, -1); $selfReplies = $redis->lrange($cacheKeys['selfreply'], 0, -1); $regularComments = $redis->lrange($cacheKeys['dbcomments'], 0, -1); $finalPostIds = array_unique(array_merge($adminReplies, $selfReplies, $regularComments)); foreach ($finalPostIds as $commentId) { $json = $redis->get("posts:$commentId"); if (!$json) continue; $comment = json_decode($json, true); if (!$comment) continue; $media = json_decode($comment['mediaurl'] ?? '[]', true) ?: []; $originals = json_decode($comment['originalfilename'] ?? '[]', true) ?: []; $posts[] = [ 'id' => $comment['id'] ?? '', 'accountId' => (int)($comment['defaultaccountid'] ?? 0), 'username' => $comment['defaultusername'] ?? 'guest', 'content' => $comment['postcontent'] ?? '', 'media' => $media, 'audiofilename' => $comment['audiofilename'] ?? '', 'originals' => $originals, 'hashtags' => explode(',', $comment['hashtag'] ?? ''), 'niche' => $comment['niche'] ?? '', 'ip' => $comment['ipaddress'] ?? '', 'replyingto' => $comment['parentpostid'] ?? '', 'mainparentpostid' => $comment['replythreadid'] ?? '', 'userAgent' => $comment['useragent'] ?? '', 'postId' => $comment['postid'] ?? '', 'likes' => getLikesCount($conn, $comment['postid'], $redis), 'liked' => userLikedPost($conn, $redis, $comment['postid'], $userid), 'comments' => getNumberOfCommentCount($redis, $comment['totalcomment'] ?? 0, $comment['postid']), 'shares' => $comment['totalshares'] ?? 0, 'viewType' => $comment['distributedviewstype'] ?? '', 'svd' => userBookmarkedPost($conn, $redis, $comment['postid'], $userid), 'bookmarks' => getNumberOfBookMark($redis, $comment['bookmarks'] ?? 0, $comment['postid']), 'mutestatus' => $comment['mutestatus'] ?? 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'adminreply' => $comment['adminreply'] ?? '', 'potentialImpressions' => (int)($comment['assignedpotentialimpressions'] ?? 0), 'impressions' => getNumberOfViews($redis, $comment['totalimpressions'] ?? 0, $comment['postid']), 'numberofrepost' => getNumberOfRepost($redis, $comment['numberofreposts'] ?? 0, $comment['postid']), 'nsfw' => $comment['nsfw'] ?? 'no' ]; } } // Load generalcomment list directly $adminReplies = []; $generalKey = "generalcomment:$commentId"; $adminIds = array_unique($redis->lrange($generalKey, 0, -1)); foreach ($adminIds as $adminPostId) { $adminJson = $redis->get("posts:$adminPostId"); if (!$adminJson) continue; $adminReply = json_decode($adminJson, true); if (!$adminReply) continue; $adminReplies[] = [ 'id' => $adminReply['id'] ?? '', 'accountId' => (int)($adminReply['defaultaccountid'] ?? 0), 'username' => $adminReply['defaultusername'] ?? 'guest', 'content' => $adminReply['postcontent'] ?? '', 'media' => json_decode($adminReply['mediaurl'] ?? '[]', true) ?: [], 'audiofilename' => $adminReply['audiofilename'] ?? '', 'originals' => json_decode($adminReply['originalfilename'] ?? '[]', true) ?: [], 'hashtags' => explode(',', $adminReply['hashtag'] ?? ''), 'niche' => $adminReply['niche'] ?? '', 'ip' => $adminReply['ipaddress'] ?? '', 'replyingto' => $adminReply['parentpostid'] ?? '', 'mainparentpostid' => $adminReply['replythreadid'] ?? '', 'userAgent' => $adminReply['useragent'] ?? '', 'postId' => $adminReply['postid'] ?? '', 'likes' => 0, 'comments' => 0, 'shares' => 0, 'viewType' => $adminReply['distributedviewstype'] ?? '', 'svd' => 'no', 'bookmarks' => 0, 'mutestatus' => 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'adminreply' => $adminReply['adminreply'] ?? 'no', 'potentialImpressions' => 0, 'impressions' => 0, 'liked' => 'no', 'nsfw' => $adminReply['nsfw'] ?? 'no', ]; } // Return JSON echo json_encode([ 'status' => 'success', 'posts' => $posts, 'adminReplies' => $adminReplies ]); // === Utility Functions === // (Same as your original functions — getLikesCount, userLikedPost, getNumberOfCommentCount, etc.) // Keep them defined here or in a separate included file. function getLikesCount($conn, $postid, $redis) { $stmt = $conn->prepare("SELECT COUNT(*) as total FROM likes WHERE postid = ? AND status = 'like'"); $stmt->bind_param('s', $postid); $stmt->execute(); $res = $stmt->get_result()->fetch_assoc(); $stmt->close(); $dbtotal = (int)($res['total'] ?? 0); $likesCount = (int)($redis->get("likes:$postid") ?? 0); return $dbtotal + $likesCount; } function userLikedPost($conn, $redis, $postid, $user_name) { $ttl = 15 * 60; $userLikedCacheKey = "userlikedthispost:$postid:$user_name"; $likedListKey = "likedby:$postid"; $cachedStatus = $redis->get($userLikedCacheKey); if ($cachedStatus !== false) { $redis->expire($userLikedCacheKey, $ttl); return $cachedStatus === 'yes' ? 'yes' : 'no'; } $likedUsers = $redis->lrange($likedListKey, 0, -1); if (is_array($likedUsers) && in_array($user_name, $likedUsers)) { $redis->setex($userLikedCacheKey, $ttl, 'yes'); return 'yes'; } $stmt = $conn->prepare("SELECT 1 FROM likes WHERE postid = ? AND username = ? AND status = 'like' LIMIT 1"); $stmt->bind_param('ss', $postid, $user_name); $stmt->execute(); $stmt->store_result(); $liked = $stmt->num_rows > 0; $stmt->close(); $redis->setex($userLikedCacheKey, $ttl, $liked ? 'yes' : 'no'); return $liked ? 'yes' : 'no'; } function getNumberOfCommentCount($redis, $dbCount, $postid) { $key = "cachednumberofcomment:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbCount, ['ex' => 15 * 60]); return (int)$dbCount; } function getNumberOfViews($redis, $dbTotal, $postid) { $key = "postviews:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbTotal, ['ex' => 15 * 60]); return (int)$dbTotal; } function getNumberOfBookMark($redis, $dbTotal, $postid) { $key = "cachednumberofbookmarks:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->setex($key, 15 * 60, (int)$dbTotal); return (int)$dbTotal; } function userBookmarkedPost($conn, $redis, $postid, $username) { $key = "userbookmarkedpost:$postid:$username"; $ttl = 15 * 60; $cached = $redis->get($key); if ($cached !== false) { $redis->expire($key, $ttl); return $cached === 'yes' ? 'yes' : 'no'; } $stmt = $conn->prepare("SELECT 1 FROM bookmark WHERE postid = ? AND username = ? AND status = 'success' LIMIT 1"); $stmt->bind_param('ss', $postid, $username); $stmt->execute(); $stmt->store_result(); $found = $stmt->num_rows > 0; $stmt->close(); $redis->setex($key, $ttl, $found ? 'yes' : 'no'); return $found ? 'yes' : 'no'; } function getNumberOfRepost($redis, $dbTotal, $postid) { $key = "cachednumberrespost:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbTotal, ['ex' => 15 * 60]); return (int)$dbTotal; } ....................................................................... ....................................................................... connect('127.0.0.1', 6379); $posts = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $postData = json_decode(file_get_contents('php://input'), true); $parentPostId = htmlspecialchars(trim($postData['postId'] ?? '')); $userid = 'guest'; $cacheKeys = [ 'generalcomment' => "generalcomment:$parentPostId", 'dbcomments' => "dbcomments:$parentPostId", 'selfreply' => "selfreplythread:$parentPostId", 'adminreply' => "adminreplythread:$parentPostId", ]; $needRefresh = !( $redis->exists($cacheKeys['generalcomment']) && $redis->exists($cacheKeys['dbcomments']) && $redis->exists($cacheKeys['selfreply']) && $redis->exists($cacheKeys['adminreply']) ); if ($needRefresh) { $redis->del(...array_values($cacheKeys)); $stmt = $conn->prepare("SELECT * FROM comments WHERE parentpostid = ?"); $stmt->bind_param('s', $parentPostId); $stmt->execute(); $res = $stmt->get_result(); $childIds = []; while ($row = $res->fetch_assoc()) { $postid = $row['postid']; $childIds[] = $postid; $selfreply = $row['selfreply'] ?? 'no'; $author = $row['defaultusername'] ?? ''; $parentAuthor = $row['parentpostauthor'] ?? ''; $redis->setex("posts:$postid", 900, json_encode($row)); $redis->rpush($cacheKeys['generalcomment'], $postid); if ($selfreply === 'yes') { $redis->rpush($cacheKeys['selfreply'], $postid); } elseif ($author === $parentAuthor) { $redis->rpush($cacheKeys['adminreply'], $postid); } else { $redis->rpush($cacheKeys['dbcomments'], $postid); } } $stmt->close(); if (!empty($childIds)) { $inClause = implode(',', array_fill(0, count($childIds), '?')); $types = str_repeat('s', count($childIds)); $stmt = $conn->prepare("SELECT * FROM comments WHERE parentpostid IN ($inClause) AND adminreply = 'yes'"); $stmt->bind_param($types, ...$childIds); $stmt->execute(); $res = $stmt->get_result(); while ($row = $res->fetch_assoc()) { $postid = $row['postid']; $parentid = $row['parentpostid']; $redis->setex("posts:$postid", 900, json_encode($row)); $redis->rpush("generalcomment:$parentid", $postid); } $stmt->close(); } } $finalPostIds = array_unique(array_merge( $redis->lrange($cacheKeys['adminreply'], 0, -1), $redis->lrange($cacheKeys['selfreply'], 0, -1), $redis->lrange($cacheKeys['dbcomments'], 0, -1) )); foreach ($finalPostIds as $commentId) { $json = $redis->get("posts:$commentId"); if (!$json) continue; $comment = json_decode($json, true); if (!$comment) continue; $media = json_decode($comment['mediaurl'] ?? '[]', true) ?: []; $originals = json_decode($comment['originalfilename'] ?? '[]', true) ?: []; $adminReplies = []; $generalKey = "generalcomment:$commentId"; $adminIds = array_unique($redis->lrange($generalKey, 0, -1)); foreach ($adminIds as $adminPostId) { $adminJson = $redis->get("posts:$adminPostId"); if (!$adminJson) continue; $adminReply = json_decode($adminJson, true); if (!$adminReply) continue; $adminReplies[] = [ 'id' => $adminReply['id'] ?? '', 'accountId' => (int)($adminReply['defaultaccountid'] ?? 0), 'username' => $adminReply['defaultusername'] ?? 'guest', 'content' => $adminReply['postcontent'] ?? '', 'media' => json_decode($adminReply['mediaurl'] ?? '[]', true) ?: [], 'audiofilename' => $adminReply['audiofilename'] ?? '', 'originals' => json_decode($adminReply['originalfilename'] ?? '[]', true) ?: [], 'hashtags' => explode(',', $adminReply['hashtag'] ?? ''), 'niche' => $adminReply['niche'] ?? '', 'ip' => $adminReply['ipaddress'] ?? '', 'replyingto' => $adminReply['parentpostid'] ?? '', 'mainparentpostid' => $adminReply['replythreadid'] ?? '', 'userAgent' => $adminReply['useragent'] ?? '', 'postId' => $adminReply['postid'] ?? '', 'likes' => 0, 'comments' => 0, 'shares' => 0, 'viewType' => $adminReply['distributedviewstype'] ?? '', 'svd' => 'no', 'bookmarks' => 0, 'mutestatus' => 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'adminreply' => $adminReply['adminreply'] ?? 'no', 'potentialImpressions' => 0, 'impressions' => 0, 'liked' => 'no', 'nsfw' => $adminReply['nsfw'] ?? 'no' ]; } $posts[] = [ 'id' => $comment['id'] ?? '', 'accountId' => (int)($comment['defaultaccountid'] ?? 0), 'username' => $comment['defaultusername'] ?? 'guest', 'content' => $comment['postcontent'] ?? '', 'media' => $media, 'audiofilename' => $comment['audiofilename'] ?? '', 'originals' => $originals, 'hashtags' => explode(',', $comment['hashtag'] ?? ''), 'niche' => $comment['niche'] ?? '', 'ip' => $comment['ipaddress'] ?? '', 'replyingto' => $comment['parentpostid'] ?? '', 'mainparentpostid' => $comment['replythreadid'] ?? '', 'userAgent' => $comment['useragent'] ?? '', 'postId' => $comment['postid'] ?? '', 'likes' => getLikesCount($conn, $comment['postid'], $redis), 'liked' => userLikedPost($conn, $redis, $comment['postid'], $userid), 'comments' => getNumberOfCommentCount($redis, $comment['totalcomment'] ?? 0, $comment['postid']), 'shares' => $comment['totalshares'] ?? 0, 'viewType' => $comment['distributedviewstype'] ?? '', 'svd' => userBookmarkedPost($conn, $redis, $comment['postid'], $userid), 'bookmarks' => getNumberOfBookMark($redis, $comment['bookmarks'] ?? 0, $comment['postid']), 'mutestatus' => $comment['mutestatus'] ?? 'no', 'following' => 'no', 'notintrested' => 'no', 'shlink' => 'https://voidvenus.online/sharelink', 'blockedstatus' => 'no', 'adminreply' => $comment['adminreply'] ?? '', 'potentialImpressions' => (int)($comment['assignedpotentialimpressions'] ?? 0), 'impressions' => getNumberOfViews($redis, $comment['totalimpressions'] ?? 0, $comment['postid']), 'numberofrepost' => getNumberOfRepost($redis, $comment['numberofreposts'] ?? 0, $comment['postid']), 'nsfw' => $comment['nsfw'] ?? 'no', 'adminReplies' => $adminReplies ]; } echo json_encode([ 'status' => 'success', 'posts' => $posts ]); } // Load generalcomment list directly // === Utility Functions === // (Same as your original functions — getLikesCount, userLikedPost, getNumberOfCommentCount, etc.) // Keep them defined here or in a separate included file. function getLikesCount($conn, $postid, $redis) { $stmt = $conn->prepare("SELECT COUNT(*) as total FROM likes WHERE postid = ? AND status = 'like'"); $stmt->bind_param('s', $postid); $stmt->execute(); $res = $stmt->get_result()->fetch_assoc(); $stmt->close(); $dbtotal = (int)($res['total'] ?? 0); $likesCount = (int)($redis->get("likes:$postid") ?? 0); return $dbtotal + $likesCount; } function userLikedPost($conn, $redis, $postid, $user_name) { $ttl = 15 * 60; $userLikedCacheKey = "userlikedthispost:$postid:$user_name"; $likedListKey = "likedby:$postid"; $cachedStatus = $redis->get($userLikedCacheKey); if ($cachedStatus !== false) { $redis->expire($userLikedCacheKey, $ttl); return $cachedStatus === 'yes' ? 'yes' : 'no'; } $likedUsers = $redis->lrange($likedListKey, 0, -1); if (is_array($likedUsers) && in_array($user_name, $likedUsers)) { $redis->setex($userLikedCacheKey, $ttl, 'yes'); return 'yes'; } $stmt = $conn->prepare("SELECT 1 FROM likes WHERE postid = ? AND username = ? AND status = 'like' LIMIT 1"); $stmt->bind_param('ss', $postid, $user_name); $stmt->execute(); $stmt->store_result(); $liked = $stmt->num_rows > 0; $stmt->close(); $redis->setex($userLikedCacheKey, $ttl, $liked ? 'yes' : 'no'); return $liked ? 'yes' : 'no'; } function getNumberOfCommentCount($redis, $dbCount, $postid) { $key = "cachednumberofcomment:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbCount, ['ex' => 15 * 60]); return (int)$dbCount; } function getNumberOfViews($redis, $dbTotal, $postid) { $key = "postviews:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbTotal, ['ex' => 15 * 60]); return (int)$dbTotal; } function getNumberOfBookMark($redis, $dbTotal, $postid) { $key = "cachednumberofbookmarks:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->setex($key, 15 * 60, (int)$dbTotal); return (int)$dbTotal; } function userBookmarkedPost($conn, $redis, $postid, $username) { $key = "userbookmarkedpost:$postid:$username"; $ttl = 15 * 60; $cached = $redis->get($key); if ($cached !== false) { $redis->expire($key, $ttl); return $cached === 'yes' ? 'yes' : 'no'; } $stmt = $conn->prepare("SELECT 1 FROM bookmark WHERE postid = ? AND username = ? AND status = 'success' LIMIT 1"); $stmt->bind_param('ss', $postid, $username); $stmt->execute(); $stmt->store_result(); $found = $stmt->num_rows > 0; $stmt->close(); $redis->setex($key, $ttl, $found ? 'yes' : 'no'); return $found ? 'yes' : 'no'; } function getNumberOfRepost($redis, $dbTotal, $postid) { $key = "cachednumberrespost:$postid"; if ($redis->exists($key)) { return (int)$redis->get($key); } $redis->set($key, (int)$dbTotal, ['ex' => 15 * 60]); return (int)$dbTotal; }