Added notification bell, WIP.

This commit is contained in:
2024-05-20 06:29:10 +02:00
parent dc5affb4cf
commit 342ba6191e
7 changed files with 141 additions and 53 deletions

View File

@@ -140,6 +140,28 @@ export async function removeAlert(alertId, reqUser) {
.delete();
}
export async function fetchNotifications(reqUser, options = {}) {
const notifications = await knex('notifications')
.select('notifications.*', 'alerts.id as alert_id', 'scenes.title as scene_title')
.leftJoin('releases as scenes', 'scenes.id', 'notifications.scene_id')
.leftJoin('alerts', 'alerts.id', 'notifications.alert_id')
.where('notifications.user_id', reqUser.id)
.limit(options.limit || 10)
.orderBy('created_at', 'desc');
return notifications.map((notification) => ({
id: notification.id,
sceneId: notification.scene_id,
scene: {
id: notification.scene_id,
title: notification.scene_title,
},
alertId: notification.alert_id,
isSeen: notification.seen,
createdAt: notification.created_at,
}));
}
export async function updateNotification(notificationId, updatedNotification, reqUser) {
await knex('notifications')
.where('id', notificationId)

View File

@@ -2,6 +2,7 @@ import {
fetchAlerts,
createAlert,
removeAlert,
fetchNotifications,
updateNotifications,
updateNotification,
} from '../alerts.js';
@@ -24,6 +25,14 @@ export async function removeAlertApi(req, res) {
res.status(204).send();
}
export async function fetchNotificationsApi(req, res) {
const notifications = await fetchNotifications(req.user, {
limit: req.query.limit || 10,
});
res.send(notifications);
}
export async function updateNotificationsApi(req, res) {
await updateNotifications(req.body, req.user);

View File

@@ -51,6 +51,8 @@ import {
updateNotificationsApi,
} from './alerts.js';
import { fetchNotifications } from '../alerts.js';
import initLogger from '../logger.js';
const logger = initLogger();
@@ -164,6 +166,8 @@ export default async function initServer() {
router.get('/api/tags', fetchTagsApi);
router.get('*', async (req, res, next) => {
const notifications = await fetchNotifications(req.user, { limit: 20 });
const pageContextInit = {
urlOriginal: req.originalUrl,
urlQuery: req.query, // vike's own query does not apply boolean parser
@@ -185,6 +189,7 @@ export default async function initServer() {
maxAggregateSize: config.database.manticore.maxAggregateSize,
media: config.media,
},
notifications,
};
const pageContext = await renderPage(pageContextInit);