![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/-1fcb2e4b/ |
<?php
/**
* Get Notification Count API
* Returns the total count of unread notifications for the current user
*/
header('Content-Type: application/json');
session_start();
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/security.php';
// Check authentication
if (!isset($_SESSION['user_id'])) {
echo json_encode(['success' => false, 'message' => 'Authentication required']);
exit;
}
$user_id = $_SESSION['user_id'];
$pdo = getDBConnection();
if (!$pdo) {
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
exit;
}
// Create notification_reads table if it doesn't exist
try {
$pdo->exec("
CREATE TABLE IF NOT EXISTS notification_reads (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
notification_type ENUM('friend_request', 'like', 'comment') NOT NULL,
notification_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY unique_notification (user_id, notification_type, notification_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
INDEX idx_user_type (user_id, notification_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
");
} catch (PDOException $e) {
// Table might already exist, that's okay
}
try {
// Count unread friend requests (excluding read ones)
$stmt = $pdo->prepare("
SELECT COUNT(*)
FROM user_friends uf
LEFT JOIN notification_reads nr ON nr.user_id = ? AND nr.notification_type = 'friend_request' AND nr.notification_id = uf.id
WHERE uf.friend_id = ? AND uf.status = 'pending' AND nr.id IS NULL
");
$stmt->execute([$user_id, $user_id]);
$friend_requests = (int)$stmt->fetchColumn();
// Count unread likes on user's tracks (excluding user's own likes and read ones)
$stmt = $pdo->prepare("
SELECT COUNT(*)
FROM track_likes tl
JOIN music_tracks mt ON tl.track_id = mt.id
LEFT JOIN notification_reads nr ON nr.user_id = ? AND nr.notification_type = 'like' AND nr.notification_id = tl.id
WHERE mt.user_id = ? AND tl.user_id != ? AND nr.id IS NULL
");
$stmt->execute([$user_id, $user_id, $user_id]);
$likes = (int)$stmt->fetchColumn();
// Count unread comments on user's tracks (excluding user's own comments and read ones)
$stmt = $pdo->prepare("
SELECT COUNT(*)
FROM track_comments tc
JOIN music_tracks mt ON tc.track_id = mt.id
LEFT JOIN notification_reads nr ON nr.user_id = ? AND nr.notification_type = 'comment' AND nr.notification_id = tc.id
WHERE mt.user_id = ? AND tc.user_id != ? AND nr.id IS NULL
");
$stmt->execute([$user_id, $user_id, $user_id]);
$comments = (int)$stmt->fetchColumn();
$total_count = $friend_requests + $likes + $comments;
echo json_encode([
'success' => true,
'unread_count' => $total_count,
'breakdown' => [
'friend_requests' => $friend_requests,
'likes' => $likes,
'comments' => $comments
]
]);
} catch (PDOException $e) {
error_log("Error getting notification count: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Database error occurred']);
}