Added GraphQL queries for alerts and notifications.
This commit is contained in:
@@ -195,6 +195,7 @@ export async function createAlert(alert, reqUser) {
|
||||
export async function removeAlert(alertId, reqUser) {
|
||||
const alert = await knex('alerts')
|
||||
.where('alerts.id', alertId)
|
||||
.where('alerts.user_id', reqUser.id)
|
||||
.select(
|
||||
'alerts.id',
|
||||
knex.raw('coalesce(array_agg(distinct alerts_actors.actor_id) filter (where alerts_actors.actor_id is not null), \'{}\') as actor_ids'),
|
||||
@@ -208,16 +209,51 @@ export async function removeAlert(alertId, reqUser) {
|
||||
.groupBy('alerts.id')
|
||||
.first();
|
||||
|
||||
await knex('alerts')
|
||||
if (!alert) {
|
||||
throw new HttpError(`Could not find alert ${alertId}`, 404);
|
||||
}
|
||||
|
||||
const [removed] = await knex('alerts')
|
||||
.where('id', alertId)
|
||||
.where('user_id', reqUser.id)
|
||||
.delete();
|
||||
.delete()
|
||||
.returning('*');
|
||||
|
||||
await Promise.all([
|
||||
if (!removed) {
|
||||
throw new HttpError(`Could not remove alert ${alertId}`, 404);
|
||||
}
|
||||
|
||||
// slow not critical for response, don't await
|
||||
Promise.all([
|
||||
alert.actor_ids.length > 0 && knex.schema.refreshMaterializedView('alerts_users_actors'),
|
||||
alert.tag_ids.length > 0 && knex.schema.refreshMaterializedView('alerts_users_tags'),
|
||||
alert.entity_ids?.length > 0 && knex.schema.refreshMaterializedView('alerts_users_entities'),
|
||||
]);
|
||||
|
||||
return curateAlert(removed);
|
||||
}
|
||||
|
||||
function curateNotification(notification, scenes) {
|
||||
const scene = scenes.find((sceneX) => sceneX.id === notification.scene_id);
|
||||
|
||||
return {
|
||||
id: notification.id,
|
||||
sceneId: notification.scene_id,
|
||||
scene,
|
||||
alertId: notification.alert_id,
|
||||
matchedActors: scene.actors.filter((actor) => notification.alert_actors.includes(actor.id)),
|
||||
matchedTags: scene.tags.filter((tag) => notification.alert_tags.includes(tag.id)),
|
||||
matchedEntity: [scene.channel, scene.network].find((entity) => notification.alert_entities.includes(entity?.id)) || null,
|
||||
matchedExpressions: notification.alert_matches
|
||||
.filter((match) => new RegExp(match.expression, 'ui').test(scene[match.property]))
|
||||
.map((match) => ({
|
||||
id: match.id,
|
||||
property: match.property,
|
||||
expression: match.expression,
|
||||
})),
|
||||
isSeen: notification.seen,
|
||||
createdAt: notification.created_at,
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchUnseenNotificationsCount(reqUser) {
|
||||
@@ -262,29 +298,7 @@ export async function fetchNotifications(reqUser, options = {}) {
|
||||
]);
|
||||
|
||||
const scenes = await fetchScenesById(notifications.map((notification) => notification.scene_id));
|
||||
|
||||
const curatedNotifications = notifications.map((notification) => {
|
||||
const scene = scenes.find((sceneX) => sceneX.id === notification.scene_id);
|
||||
|
||||
return {
|
||||
id: notification.id,
|
||||
sceneId: notification.scene_id,
|
||||
scene,
|
||||
alertId: notification.alert_id,
|
||||
matchedActors: scene.actors.filter((actor) => notification.alert_actors.includes(actor.id)),
|
||||
matchedTags: scene.tags.filter((tag) => notification.alert_tags.includes(tag.id)),
|
||||
matchedEntity: [scene.channel, scene.network].find((entity) => notification.alert_entities.includes(entity?.id)) || null,
|
||||
matchedExpressions: notification.alert_matches
|
||||
.filter((match) => new RegExp(match.expression, 'ui').test(scene[match.property]))
|
||||
.map((match) => ({
|
||||
id: match.id,
|
||||
property: match.property,
|
||||
expression: match.expression,
|
||||
})),
|
||||
isSeen: notification.seen,
|
||||
createdAt: notification.created_at,
|
||||
};
|
||||
});
|
||||
const curatedNotifications = notifications.map((notification) => curateNotification(notification, scenes));
|
||||
|
||||
return {
|
||||
notifications: curatedNotifications,
|
||||
@@ -293,18 +307,27 @@ export async function fetchNotifications(reqUser, options = {}) {
|
||||
}
|
||||
|
||||
export async function updateNotification(notificationId, updatedNotification, reqUser) {
|
||||
await knex('notifications')
|
||||
const [updated] = await knex('notifications')
|
||||
.where('id', notificationId)
|
||||
.where('user_id', reqUser.id)
|
||||
.update({
|
||||
seen: updatedNotification.seen,
|
||||
});
|
||||
})
|
||||
.returning('*');
|
||||
|
||||
if (!updated) {
|
||||
throw new HttpError(`No notification ${notificationId} found to update`, 404);
|
||||
}
|
||||
|
||||
return updated.id;
|
||||
}
|
||||
|
||||
export async function updateNotifications(updatedNotification, reqUser) {
|
||||
await knex('notifications')
|
||||
const updatedCount = await knex('notifications')
|
||||
.where('user_id', reqUser.id)
|
||||
.update({
|
||||
seen: updatedNotification.seen,
|
||||
});
|
||||
|
||||
return updatedCount;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user