Added AND/OR toggle to alerts.

This commit is contained in:
DebaucheryLibrarian
2023-11-24 02:10:03 +01:00
parent 238dce78b5
commit 0369446681
8 changed files with 97 additions and 40 deletions

View File

@@ -24,6 +24,7 @@ async function addAlert(alert, sessionUser) {
user_id: sessionUser.id,
notify: alert.notify,
email: alert.email,
all: alert.all,
})
.returning('id');
@@ -45,10 +46,10 @@ async function addAlert(alert, sessionUser) {
alert_id: alertId,
stash_id: stashId,
})), false),
alert.entity && bulkInsert('alerts_entities', [{
alert.entities && bulkInsert('alerts_entities', alert.entities.map((entityId) => ({
alert_id: alertId,
entity_id: alert.entity,
}], false),
entity_id: entityId,
})).slice(0, alert.all ? 1 : Infinity), false), // one scene can never match multiple entities in AND mode
]);
return alertId;
@@ -121,8 +122,6 @@ async function notify(scenes) {
return acc;
}, {});
console.log(alertsStashesByAlertId);
const alerts = rawAlerts.map((alert) => ({
id: alert.id,
userId: alert.user_id,
@@ -148,32 +147,45 @@ async function notify(scenes) {
const triggers = alerts.flatMap((alert) => {
const alertScenes = curatedScenes.filter((scene) => {
console.log(scene.title, alert.tags, scene.tagIds);
if (alert.all) {
if (alert.actors.length > 0 && !alert.actors.every((actorId) => scene.actorIds.includes(actorId))) {
return false;
}
if (alert.actors.length > 0 && !alert.actors.every((actorId) => scene.actorIds.includes(actorId))) {
console.log('THROW ACTORS');
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;
}
if (alert.matches.length > 0 && !alert.matches.every((match) => match.expression.test(scene[match.property]))) {
return false;
}
return true;
}
if (alert.tags.length > 0 && !alert.tags.every((tagId) => scene.tagIds.includes(tagId))) {
console.log('THROW TAGS');
return false;
if (alert.actors.some((actorId) => scene.actorIds.includes(actorId))) {
return true;
}
if (alert.tags.some((tagId) => scene.tagIds.includes(tagId))) {
return true;
}
// multiple entities can only be matched in OR mode
if (alert.entities.length > 0 && !alert.entities.some((alertEntityId) => alertEntityId === scene.entityId || alertEntityId === scene.parentEntityId)) {
console.log('THROW ENTITIES');
return false;
if (alert.entities.some((alertEntityId) => alertEntityId === scene.entityId || alertEntityId === scene.parentEntityId)) {
return true;
}
if (alert.matches.length > 0 && !alert.matches.every((match) => match.expression.test(scene[match.property]))) {
console.log('THROW MATCHES');
return false;
if (alert.matches.some((match) => match.expression.test(scene[match.property]))) {
return true;
}
console.log('OK');
return true;
return false;
});
return alertScenes.map((scene) => ({
@@ -191,15 +203,11 @@ async function notify(scenes) {
scene_id: trigger.sceneId,
}));
console.log('triggers', triggers);
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])));
console.log('stashes', stashes);
await Promise.all([
bulkInsert('notifications', notifications, false),
bulkInsert('stashes_scenes', stashes, false),