Loading notifications async.
This commit is contained in:
@@ -145,12 +145,26 @@ export async function removeAlert(alertId, reqUser) {
|
||||
.delete();
|
||||
}
|
||||
|
||||
export async function fetchUnseenNotificationsCount(reqUser) {
|
||||
if (!reqUser) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawUnseen = await knex('notifications')
|
||||
.select(knex.raw('count(id)'))
|
||||
.where('user_id', reqUser.id)
|
||||
.where('seen', false)
|
||||
.first();
|
||||
|
||||
return Number(rawUnseen.count);
|
||||
}
|
||||
|
||||
export async function fetchNotifications(reqUser, options = {}) {
|
||||
if (!reqUser) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const [notifications, rawUnseen] = await Promise.all([
|
||||
const [notifications, unseen] = await Promise.all([
|
||||
knex('notifications')
|
||||
.select(
|
||||
'notifications.*',
|
||||
@@ -169,15 +183,10 @@ export async function fetchNotifications(reqUser, options = {}) {
|
||||
.limit(options.limit)
|
||||
.groupBy('notifications.id', 'alerts.id')
|
||||
.orderBy('created_at', 'desc'),
|
||||
knex('notifications')
|
||||
.select(knex.raw('count(id)'))
|
||||
.where('user_id', reqUser.id)
|
||||
.where('seen', false)
|
||||
.first(),
|
||||
fetchUnseenNotificationsCount(reqUser),
|
||||
]);
|
||||
|
||||
const scenes = await fetchScenesById(notifications.map((notification) => notification.scene_id));
|
||||
const unseen = Number(rawUnseen.count);
|
||||
|
||||
const curatedNotifications = notifications.map((notification) => {
|
||||
const scene = scenes.find((sceneX) => sceneX.id === notification.scene_id);
|
||||
|
||||
Reference in New Issue
Block a user