2024-05-27 01:06:54 +00:00
|
|
|
import escapeRegexp from 'escape-string-regexp';
|
|
|
|
|
2024-05-19 03:07:35 +00:00
|
|
|
import { knexOwner as knex } from './knex.js';
|
2024-05-27 01:06:54 +00:00
|
|
|
import { fetchScenesById } from './scenes.js';
|
2024-05-19 03:07:35 +00:00
|
|
|
import promiseProps from '../utils/promise-props.js';
|
|
|
|
import { HttpError } from './errors.js';
|
|
|
|
|
|
|
|
function curateAlert(alert, context = {}) {
|
|
|
|
return {
|
|
|
|
id: alert.id,
|
|
|
|
notify: alert.notify,
|
|
|
|
email: alert.email,
|
|
|
|
createdAt: alert.created_at,
|
|
|
|
and: {
|
|
|
|
fields: alert.all,
|
|
|
|
actors: alert.all_actors,
|
|
|
|
tags: alert.all_tags,
|
|
|
|
entities: alert.all_entities,
|
|
|
|
matches: alert.all_tags,
|
|
|
|
},
|
|
|
|
actors: context.actors?.map((actor) => ({
|
|
|
|
id: actor.actor_id,
|
|
|
|
name: actor.actor_name,
|
|
|
|
slug: actor.actor_slug,
|
|
|
|
})) || [],
|
|
|
|
tags: context.tags?.map((tag) => ({
|
|
|
|
id: tag.tag_id,
|
|
|
|
name: tag.tag_name,
|
|
|
|
slug: tag.tag_slug,
|
|
|
|
})) || [],
|
|
|
|
entities: context.entities?.map((entity) => ({
|
|
|
|
id: entity.entity_id,
|
|
|
|
name: entity.entity_name,
|
|
|
|
slug: entity.entity_slug,
|
2024-05-27 01:06:54 +00:00
|
|
|
type: entity.entity_type,
|
2024-05-19 03:07:35 +00:00
|
|
|
})) || [],
|
|
|
|
matches: context.matches?.map((match) => ({
|
|
|
|
id: match.id,
|
|
|
|
property: match.property,
|
|
|
|
expression: match.expression,
|
|
|
|
})) || [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchAlerts(user) {
|
|
|
|
const {
|
|
|
|
alerts,
|
|
|
|
actors,
|
|
|
|
tags,
|
|
|
|
entities,
|
|
|
|
matches,
|
|
|
|
} = await promiseProps({
|
|
|
|
alerts: knex('alerts')
|
|
|
|
.where('user_id', user.id),
|
|
|
|
actors: knex('alerts_actors')
|
|
|
|
.select('alerts_actors.*', 'actors.name as actor_name', 'actors.slug as actor_slug')
|
|
|
|
.leftJoin('alerts', 'alerts.id', 'alerts_actors.alert_id')
|
|
|
|
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
|
|
|
|
.where('alerts.user_id', user.id),
|
|
|
|
tags: knex('alerts_tags')
|
|
|
|
.select('alerts_tags.*', 'tags.name as tag_name', 'tags.slug as tag_slug')
|
|
|
|
.leftJoin('alerts', 'alerts.id', 'alerts_tags.alert_id')
|
|
|
|
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
|
|
|
|
.where('alerts.user_id', user.id),
|
|
|
|
entities: knex('alerts_entities')
|
|
|
|
.select('alerts_entities.*', 'entities.name as entity_name', 'entities.slug as entity_slug', 'entities.type as entity_type')
|
|
|
|
.leftJoin('alerts', 'alerts.id', 'alerts_entities.alert_id')
|
|
|
|
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id')
|
|
|
|
.where('alerts.user_id', user.id),
|
|
|
|
matches: knex('alerts_matches')
|
|
|
|
.select('alerts_matches.*')
|
|
|
|
.leftJoin('alerts', 'alerts.id', 'alerts_matches.alert_id')
|
|
|
|
.where('alerts.user_id', user.id),
|
|
|
|
});
|
|
|
|
|
|
|
|
const curatedAlerts = alerts.map((alert) => curateAlert(alert, {
|
|
|
|
actors: actors.filter((actor) => actor.alert_id === alert.id),
|
|
|
|
tags: tags.filter((tag) => tag.alert_id === alert.id),
|
|
|
|
entities: entities.filter((entity) => entity.alert_id === alert.id),
|
|
|
|
matches: matches.filter((match) => match.alert_id === alert.id),
|
|
|
|
}));
|
|
|
|
|
|
|
|
return curatedAlerts;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createAlert(alert, reqUser) {
|
|
|
|
if (!reqUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!alert.actors || alert.actors.length === 0) && (!alert.tags || alert.tags.length === 0) && (!alert.entities || alert.entities.length === 0) && (!alert.matches || alert.matches.length === 0)) {
|
|
|
|
throw new HttpError('Alert must contain at least one actor, tag or entity', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alert.matches?.some((match) => !match.property || !match.expression)) {
|
|
|
|
throw new HttpError('Match must define a property and an expression', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
const [{ id: alertId }] = await knex('alerts')
|
|
|
|
.insert({
|
|
|
|
user_id: reqUser.id,
|
|
|
|
notify: alert.notify,
|
|
|
|
email: alert.email,
|
|
|
|
all: alert.all,
|
|
|
|
all_actors: alert.allActors,
|
|
|
|
all_entities: alert.allEntities,
|
|
|
|
all_tags: alert.allTags,
|
|
|
|
all_matches: alert.allMatches,
|
|
|
|
})
|
|
|
|
.returning('id');
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
alert.actors?.length > 0 && knex('alerts_actors').insert(alert.actors.map((actorId) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
actor_id: actorId,
|
|
|
|
}))),
|
|
|
|
alert.tags?.length > 0 && knex('alerts_tags').insert(alert.tags.map((tagId) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
tag_id: tagId,
|
|
|
|
}))),
|
|
|
|
alert.matches?.length > 0 && knex('alerts_matches').insert(alert.matches.map((match) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
property: match.property,
|
2024-05-27 01:06:54 +00:00
|
|
|
expression: /\/.*\//.test(match.expression)
|
|
|
|
? match.expression.slice(1, -1)
|
|
|
|
: escapeRegexp(match.expression),
|
2024-05-19 03:07:35 +00:00
|
|
|
}))),
|
|
|
|
alert.stashes?.length > 0 && knex('alerts_stashes').insert(alert.stashes.map((stashId) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
stash_id: stashId,
|
|
|
|
}))),
|
|
|
|
alert.entities?.length > 0 && knex('alerts_entities').insert(alert.entities.map((entityId) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
entity_id: entityId,
|
|
|
|
})).slice(0, alert.allEntities ? 1 : Infinity)), // one scene can never match multiple entities in AND mode
|
|
|
|
]);
|
|
|
|
|
|
|
|
return alertId;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function removeAlert(alertId, reqUser) {
|
|
|
|
await knex('alerts')
|
|
|
|
.where('id', alertId)
|
|
|
|
.where('user_id', reqUser.id)
|
|
|
|
.delete();
|
|
|
|
}
|
|
|
|
|
2024-05-20 04:29:10 +00:00
|
|
|
export async function fetchNotifications(reqUser, options = {}) {
|
2024-05-27 01:06:54 +00:00
|
|
|
if (!reqUser) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:12:33 +00:00
|
|
|
const [notifications, rawUnseen] = await Promise.all([
|
|
|
|
knex('notifications')
|
|
|
|
.select(
|
|
|
|
'notifications.*',
|
|
|
|
'alerts.id as alert_id',
|
|
|
|
knex.raw('coalesce(array_agg(alerts_actors.actor_id) filter (where alerts_actors.id is not null), \'{}\') as alert_actors'),
|
|
|
|
knex.raw('coalesce(array_agg(alerts_tags.tag_id) filter (where alerts_tags.id is not null), \'{}\') as alert_tags'),
|
|
|
|
knex.raw('coalesce(array_agg(alerts_entities.entity_id) filter (where alerts_entities.id is not null), \'{}\') as alert_entities'),
|
|
|
|
knex.raw('coalesce(json_agg(alerts_matches) filter (where alerts_matches.id is not null), \'[]\') as alert_matches'),
|
|
|
|
)
|
|
|
|
.leftJoin('alerts', 'alerts.id', 'notifications.alert_id')
|
|
|
|
.leftJoin('alerts_actors', 'alerts_actors.alert_id', 'alerts.id')
|
|
|
|
.leftJoin('alerts_tags', 'alerts_tags.alert_id', 'alerts.id')
|
|
|
|
.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'),
|
|
|
|
knex('notifications')
|
|
|
|
.select(knex.raw('count(id)'))
|
|
|
|
.where('user_id', reqUser.id)
|
|
|
|
.where('seen', false)
|
|
|
|
.first(),
|
|
|
|
]);
|
2024-05-20 04:29:10 +00:00
|
|
|
|
2024-05-27 01:06:54 +00:00
|
|
|
const scenes = await fetchScenesById(notifications.map((notification) => notification.scene_id));
|
2024-05-28 01:12:33 +00:00
|
|
|
const unseen = Number(rawUnseen.count);
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
});
|
2024-05-27 01:06:54 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
notifications: curatedNotifications,
|
|
|
|
unseen,
|
|
|
|
};
|
2024-05-20 04:29:10 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 03:07:35 +00:00
|
|
|
export async function updateNotification(notificationId, updatedNotification, reqUser) {
|
|
|
|
await knex('notifications')
|
|
|
|
.where('id', notificationId)
|
|
|
|
.where('user_id', reqUser.id)
|
|
|
|
.update({
|
|
|
|
seen: updatedNotification.seen,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateNotifications(updatedNotification, reqUser) {
|
|
|
|
await knex('notifications')
|
|
|
|
.where('user_id', reqUser.id)
|
|
|
|
.update({
|
|
|
|
seen: updatedNotification.seen,
|
|
|
|
});
|
|
|
|
}
|