fix: use server-side time_ago for notification timestamps #77

Merged
rafaga21 merged 1 commits from feat/server-thresholds into master 2026-06-13 16:57:20 -04:00
2 changed files with 7 additions and 14 deletions

View File

@@ -699,9 +699,14 @@ class ApiController
$result = $notificationModel->getForUser((int) $user['id'], $page, $perPage);
$unreadCount = $notificationModel->getUnreadCount((int) $user['id']);
$data = array_map(function ($n) {
$n['time_ago'] = time_ago($n['created_at'] ?? '');
return $n;
}, $result['data']);
$this->view->json([
'success' => true,
'data' => $result['data'],
'data' => $data,
'pagination' => [
'page' => $result['page'],
'per_page' => $result['per_page'],

View File

@@ -295,7 +295,7 @@ function renderNotifications(items, unreadCount) {
+ '<div class="notif-content">'
+ '<div class="notif-title">' + escapeHtml(n.title) + '</div>'
+ '<div class="notif-message">' + escapeHtml(n.message) + '</div>'
+ '<div class="notif-time">' + timeAgo(n.created_at) + '</div>'
+ '<div class="notif-time">' + (n.time_ago || '') + '</div>'
+ '</div>'
+ (n.is_read ? '' : '<div class="notif-dot"></div>')
+ '</div>';
@@ -374,18 +374,6 @@ function escapeHtml(str) {
return d.innerHTML;
}
function timeAgo(dateStr) {
const now = new Date();
const d = new Date(dateStr.replace(' ', 'T') + (dateStr.includes('Z') ? '' : 'Z'));
const diff = Math.floor((now - d) / 1000);
if (diff < 60) return 'just now';
if (diff < 3600) return Math.floor(diff / 60) + 'm ago';
if (diff < 86400) return Math.floor(diff / 3600) + 'h ago';
const days = Math.floor(diff / 86400);
if (days < 7) return days + 'd ago';
return d.toLocaleDateString();
}
// Poll unread count every 60s
setInterval(updateUnreadCount, 60000);
// Initial fetch