Improved notification efficiency.

This commit is contained in:
DebaucheryLibrarian 2024-05-28 03:12:33 +02:00
parent cec927ba0b
commit b9afde051e
1 changed files with 47 additions and 41 deletions

View File

@ -150,7 +150,8 @@ export async function fetchNotifications(reqUser, options = {}) {
return [];
}
const notifications = await knex('notifications')
const [notifications, rawUnseen] = await Promise.all([
knex('notifications')
.select(
'notifications.*',
'alerts.id as alert_id',
@ -165,15 +166,20 @@ export async function fetchNotifications(reqUser, options = {}) {
.leftJoin('alerts_entities', 'alerts_entities.alert_id', 'alerts.id')
.leftJoin('alerts_matches', 'alerts_matches.alert_id', 'alerts.id')
.where('notifications.user_id', reqUser.id)
.limit(options.limit)
.groupBy('notifications.id', 'alerts.id')
.orderBy('created_at', 'desc');
.orderBy('created_at', 'desc'),
knex('notifications')
.select(knex.raw('count(id)'))
.where('user_id', reqUser.id)
.where('seen', false)
.first(),
]);
const scenes = await fetchScenesById(notifications.map((notification) => notification.scene_id));
const unseen = notifications.filter((notification) => !notification.seen).length;
const unseen = Number(rawUnseen.count);
const curatedNotifications = notifications
.slice(0, options.limit || 10)
.map((notification) => {
const curatedNotifications = notifications.map((notification) => {
const scene = scenes.find((sceneX) => sceneX.id === notification.scene_id);
return {