2021-04-04 19:52:19 +00:00
|
|
|
'use strict';
|
|
|
|
|
2023-11-24 00:29:22 +00:00
|
|
|
const escapeRegexp = require('escape-string-regexp');
|
|
|
|
|
2021-04-04 19:52:19 +00:00
|
|
|
const knex = require('./knex');
|
2021-06-04 01:10:41 +00:00
|
|
|
const bulkInsert = require('./utils/bulk-insert');
|
2021-04-04 20:52:54 +00:00
|
|
|
const { HttpError } = require('./errors');
|
2021-04-04 19:52:19 +00:00
|
|
|
|
2021-04-04 20:52:54 +00:00
|
|
|
async function addAlert(alert, sessionUser) {
|
|
|
|
if (!sessionUser) {
|
|
|
|
throw new HttpError('You are not authenthicated', 401);
|
|
|
|
}
|
2021-04-04 19:52:19 +00:00
|
|
|
|
2024-03-28 00:19:13 +00:00
|
|
|
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)) {
|
2021-04-04 20:52:54 +00:00
|
|
|
throw new HttpError('Alert must contain at least one actor, tag or entity', 400);
|
|
|
|
}
|
|
|
|
|
2023-11-24 00:29:22 +00:00
|
|
|
if (alert.matches?.some((match) => !match.property || !match.expression)) {
|
|
|
|
throw new HttpError('Match must define a property and an expression', 400);
|
|
|
|
}
|
|
|
|
|
2023-11-30 02:12:47 +00:00
|
|
|
const [{ id: alertId }] = await knex('alerts')
|
2021-04-04 20:52:54 +00:00
|
|
|
.insert({
|
|
|
|
user_id: sessionUser.id,
|
|
|
|
notify: alert.notify,
|
|
|
|
email: alert.email,
|
2023-11-24 01:10:03 +00:00
|
|
|
all: alert.all,
|
2021-04-04 20:52:54 +00:00
|
|
|
})
|
|
|
|
.returning('id');
|
|
|
|
|
|
|
|
await Promise.all([
|
2021-11-20 22:59:15 +00:00
|
|
|
alert.actors?.length > 0 && bulkInsert('alerts_actors', alert.actors.map((actorId) => ({
|
2021-04-04 20:52:54 +00:00
|
|
|
alert_id: alertId,
|
|
|
|
actor_id: actorId,
|
2021-06-04 01:10:41 +00:00
|
|
|
})), false),
|
2021-11-20 22:59:15 +00:00
|
|
|
alert.tags?.length > 0 && bulkInsert('alerts_tags', alert.tags.map((tagId) => ({
|
2021-04-04 20:52:54 +00:00
|
|
|
alert_id: alertId,
|
|
|
|
tag_id: tagId,
|
2021-06-04 01:10:41 +00:00
|
|
|
})), false),
|
2023-11-24 00:29:22 +00:00
|
|
|
alert.matches?.length > 0 && bulkInsert('alerts_matches', alert.matches.map((match) => ({
|
|
|
|
alert_id: alertId,
|
|
|
|
property: match.property,
|
|
|
|
expression: match.expression,
|
|
|
|
})), false),
|
2021-11-20 22:59:15 +00:00
|
|
|
alert.stashes?.length > 0 && bulkInsert('alerts_stashes', alert.stashes.map((stashId) => ({
|
2021-04-04 20:52:54 +00:00
|
|
|
alert_id: alertId,
|
|
|
|
stash_id: stashId,
|
2021-06-04 01:10:41 +00:00
|
|
|
})), false),
|
2023-11-24 01:10:03 +00:00
|
|
|
alert.entities && bulkInsert('alerts_entities', alert.entities.map((entityId) => ({
|
2021-04-04 20:52:54 +00:00
|
|
|
alert_id: alertId,
|
2023-11-24 01:10:03 +00:00
|
|
|
entity_id: entityId,
|
|
|
|
})).slice(0, alert.all ? 1 : Infinity), false), // one scene can never match multiple entities in AND mode
|
2021-04-04 20:52:54 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return alertId;
|
2021-04-04 19:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeAlert(alertId) {
|
|
|
|
await knex('alerts').where('id', alertId).delete();
|
|
|
|
}
|
|
|
|
|
2021-04-25 02:20:38 +00:00
|
|
|
async function notify(scenes) {
|
2023-11-24 00:29:22 +00:00
|
|
|
const sceneIds = scenes.map((scene) => scene.id);
|
|
|
|
|
|
|
|
const [
|
|
|
|
releasesActors,
|
|
|
|
releasesTags,
|
|
|
|
rawAlerts,
|
|
|
|
alertsActors,
|
|
|
|
alertsTags,
|
|
|
|
alertsEntities,
|
|
|
|
alertsMatches,
|
|
|
|
alertsStashes,
|
|
|
|
] = await Promise.all([
|
|
|
|
knex('releases_actors').whereIn('release_id', sceneIds),
|
|
|
|
knex('releases_tags').whereIn('release_id', sceneIds),
|
|
|
|
knex('alerts'),
|
|
|
|
knex('alerts_actors'),
|
|
|
|
knex('alerts_tags'),
|
|
|
|
knex('alerts_entities'),
|
|
|
|
knex('alerts_matches'),
|
|
|
|
knex('alerts_stashes'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const actorIdsByReleaseId = releasesActors.reduce((acc, releaseActor) => {
|
|
|
|
if (!acc[releaseActor.release_id]) {
|
|
|
|
acc[releaseActor.release_id] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[releaseActor.release_id].push(releaseActor.actor_id);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const tagIdsByReleaseId = releasesTags.reduce((acc, releaseTag) => {
|
|
|
|
if (!acc[releaseTag.release_id]) {
|
|
|
|
acc[releaseTag.release_id] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[releaseTag.release_id].push(releaseTag.tag_id);
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const alertsActorsByAlertId = alertsActors.reduce((acc, alertActor) => { if (!acc[alertActor.alert_id]) { acc[alertActor.alert_id] = []; } acc[alertActor.alert_id].push(alertActor.actor_id); return acc; }, {});
|
|
|
|
const alertsTagsByAlertId = alertsTags.reduce((acc, alertTag) => { if (!acc[alertTag.alert_id]) { acc[alertTag.alert_id] = []; } acc[alertTag.alert_id].push(alertTag.tag_id); return acc; }, {});
|
|
|
|
const alertsEntitiesByAlertId = alertsEntities.reduce((acc, alertEntity) => { if (!acc[alertEntity.alert_id]) { acc[alertEntity.alert_id] = []; } acc[alertEntity.alert_id].push(alertEntity.entity_id); return acc; }, {});
|
|
|
|
const alertsStashesByAlertId = alertsStashes.reduce((acc, alertStash) => { if (!acc[alertStash.alert_id]) { acc[alertStash.alert_id] = []; } acc[alertStash.alert_id].push(alertStash.stash_id); return acc; }, {});
|
|
|
|
|
|
|
|
const alertsMatchesByAlertId = alertsMatches.reduce((acc, alertMatch) => {
|
|
|
|
if (!acc[alertMatch.alert_id]) {
|
|
|
|
acc[alertMatch.alert_id] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[alertMatch.alert_id].push({
|
|
|
|
property: alertMatch.property,
|
|
|
|
expression: /\/.*\//.test(alertMatch.expression)
|
|
|
|
? new RegExp(alertMatch.expression.slice(1, -1), 'ui')
|
|
|
|
: new RegExp(escapeRegexp(alertMatch.expression), 'ui'),
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const alerts = rawAlerts.map((alert) => ({
|
|
|
|
id: alert.id,
|
|
|
|
userId: alert.user_id,
|
|
|
|
notify: alert.notify,
|
|
|
|
email: alert.email,
|
|
|
|
all: alert.all,
|
|
|
|
actors: alertsActorsByAlertId[alert.id] || [],
|
|
|
|
tags: alertsTagsByAlertId[alert.id] || [],
|
|
|
|
entities: alertsEntitiesByAlertId[alert.id] || [],
|
|
|
|
matches: alertsMatchesByAlertId[alert.id] || [],
|
|
|
|
stashes: alertsStashesByAlertId[alert.id] || [],
|
|
|
|
}));
|
|
|
|
|
|
|
|
const curatedScenes = scenes.map((scene) => ({
|
|
|
|
id: scene.id,
|
|
|
|
title: scene.title,
|
|
|
|
description: scene.description,
|
|
|
|
actorIds: actorIdsByReleaseId[scene.id] || [],
|
|
|
|
tagIds: tagIdsByReleaseId[scene.id] || [],
|
|
|
|
entityId: scene.entity.id,
|
|
|
|
parentEntityId: scene.entity.parent?.id,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const triggers = alerts.flatMap((alert) => {
|
|
|
|
const alertScenes = curatedScenes.filter((scene) => {
|
2023-11-24 01:10:03 +00:00
|
|
|
if (alert.all) {
|
|
|
|
if (alert.actors.length > 0 && !alert.actors.every((actorId) => scene.actorIds.includes(actorId))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alert.tags.length > 0 && !alert.tags.every((tagId) => scene.tagIds.includes(tagId))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiple entities can only be matched in OR mode
|
|
|
|
if (alert.entities.length > 0 && !alert.entities.some((alertEntityId) => alertEntityId === scene.entityId || alertEntityId === scene.parentEntityId)) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-24 00:29:22 +00:00
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
if (alert.matches.length > 0 && !alert.matches.every((match) => match.expression.test(scene[match.property]))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-11-24 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
if (alert.actors.some((actorId) => scene.actorIds.includes(actorId))) {
|
|
|
|
return true;
|
2023-11-24 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
if (alert.tags.some((tagId) => scene.tagIds.includes(tagId))) {
|
|
|
|
return true;
|
2023-11-24 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
// multiple entities can only be matched in OR mode
|
|
|
|
if (alert.entities.some((alertEntityId) => alertEntityId === scene.entityId || alertEntityId === scene.parentEntityId)) {
|
|
|
|
return true;
|
2023-11-24 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
if (alert.matches.some((match) => match.expression.test(scene[match.property]))) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-24 00:29:22 +00:00
|
|
|
|
2023-11-24 01:10:03 +00:00
|
|
|
return false;
|
2023-11-24 00:29:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return alertScenes.map((scene) => ({
|
|
|
|
sceneId: scene.id,
|
|
|
|
alert,
|
|
|
|
}));
|
2021-04-25 01:08:50 +00:00
|
|
|
});
|
|
|
|
|
2023-11-24 00:29:22 +00:00
|
|
|
const notifications = Object.values(Object.fromEntries(triggers // prevent multiple notifications for the same scene
|
|
|
|
.filter((trigger) => trigger.alert.notify)
|
|
|
|
.map((trigger) => [`${trigger.alert.userId}:${trigger.sceneId}`, trigger])))
|
|
|
|
.map((trigger) => ({
|
|
|
|
user_id: trigger.alert.userId,
|
|
|
|
alert_id: trigger.alert.id,
|
|
|
|
scene_id: trigger.sceneId,
|
2021-04-28 23:45:01 +00:00
|
|
|
}));
|
|
|
|
|
2023-11-24 00:29:22 +00:00
|
|
|
const stashes = Object.values(Object.fromEntries(triggers.flatMap((trigger) => trigger.alert.stashes.map((stashId) => ({
|
|
|
|
scene_id: trigger.sceneId,
|
|
|
|
stash_id: stashId,
|
|
|
|
}))).map((stash) => [`${stash.stash_id}:${stash.scene_id}`, stash])));
|
|
|
|
|
2021-04-28 23:45:01 +00:00
|
|
|
await Promise.all([
|
2021-06-04 01:22:40 +00:00
|
|
|
bulkInsert('notifications', notifications, false),
|
|
|
|
bulkInsert('stashes_scenes', stashes, false),
|
2021-04-28 23:45:01 +00:00
|
|
|
]);
|
|
|
|
|
2023-11-24 00:29:22 +00:00
|
|
|
return triggers;
|
2021-04-25 01:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotification(notificationId, notification, sessionUser) {
|
|
|
|
await knex('notifications')
|
|
|
|
.where('user_id', sessionUser.id)
|
|
|
|
.where('id', notificationId)
|
|
|
|
.update({
|
|
|
|
seen: notification.seen,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateNotifications(notification, sessionUser) {
|
|
|
|
await knex('notifications')
|
|
|
|
.where('user_id', sessionUser.id)
|
|
|
|
.update({
|
|
|
|
seen: notification.seen,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-04 19:52:19 +00:00
|
|
|
module.exports = {
|
|
|
|
addAlert,
|
|
|
|
removeAlert,
|
2021-04-25 01:08:50 +00:00
|
|
|
notify,
|
|
|
|
updateNotification,
|
|
|
|
updateNotifications,
|
2021-04-04 19:52:19 +00:00
|
|
|
};
|