Skip to content

Commit 26f316c

Browse files
committed
Fix activity pagination: use single query with subquery COUNT to avoid MariaDB parameter issues
1 parent 28e5c8f commit 26f316c

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

services/activity.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ export async function logActivity(userId, action, details = '', serverId = null)
1212
}
1313

1414
export async function getRecentActivity(userId, limit = 20, offset = 0) {
15-
const countRows = await query(
16-
'SELECT COUNT(*) as total FROM activity_log WHERE user_id = ?',
17-
[userId]
18-
);
19-
const total = (countRows && countRows[0]) ? countRows[0].total : 0;
2015
const rows = await query(
21-
'SELECT * FROM activity_log WHERE user_id = ? ORDER BY created_at DESC LIMIT ?, ?',
22-
[userId, offset, limit]
16+
`SELECT *, (SELECT COUNT(*) FROM activity_log WHERE user_id = ?) as _total FROM activity_log WHERE user_id = ? ORDER BY created_at DESC LIMIT ${parseInt(offset)}, ${parseInt(limit)}`,
17+
[userId, userId]
2318
);
19+
const total = rows.length > 0 ? Number(rows[0]._total) : 0;
20+
rows.forEach(r => delete r._total);
2421
return { activities: rows, total };
2522
}

0 commit comments

Comments
 (0)