Syncing alert stash with manticore.

This commit is contained in:
DebaucheryLibrarian
2024-04-29 03:53:17 +02:00
parent db1b72f95f
commit 6339a253c0
3 changed files with 105 additions and 5 deletions

View File

@@ -3,6 +3,7 @@
const escapeRegexp = require('escape-string-regexp');
const knex = require('./knex');
const { indexApi } = require('./manticore');
const bulkInsert = require('./utils/bulk-insert');
const { HttpError } = require('./errors');
@@ -203,16 +204,45 @@ async function notify(scenes) {
scene_id: trigger.sceneId,
}));
const stashes = Object.values(Object.fromEntries(triggers.flatMap((trigger) => trigger.alert.stashes.map((stashId) => ({
scene_id: trigger.sceneId,
stash_id: stashId,
const uniqueStashes = Object.values(Object.fromEntries(triggers.flatMap((trigger) => trigger.alert.stashes.map((stashId) => ({
sceneId: trigger.sceneId,
stashId,
userId: trigger.alert.userId,
}))).map((stash) => [`${stash.stash_id}:${stash.scene_id}`, stash])));
await Promise.all([
const stashEntries = uniqueStashes.map((stash) => ({
scene_id: stash.sceneId,
stash_id: stash.stashId,
}));
const [stashed] = await Promise.all([
bulkInsert('stashes_scenes', stashEntries, false),
bulkInsert('notifications', notifications, false),
bulkInsert('stashes_scenes', stashes, false),
]);
// we need created_at from the databased, but user_id is not returned. it's easier to query it than to try and merge it with the input data
const stashedEntries = await knex('stashes_scenes')
.select('stashes_scenes.*', 'stashes.user_id')
.leftJoin('stashes', 'stashes.id', 'stashes_scenes.stash_id')
.whereIn('stashes_scenes.id', stashed.map((stash) => stash.id));
const docs = stashedEntries.map((stash) => ({
replace: {
index: 'scenes_stashed',
id: stash.id,
doc: {
scene_id: stash.scene_id,
user_id: stash.user_id,
stash_id: stash.stash_id,
created_at: Math.round(stash.created_at.getTime() / 1000),
},
},
}));
if (docs.length > 0) {
await indexApi.bulk(docs.map((doc) => JSON.stringify(doc)).join('\n'));
}
return triggers;
}