traxxx/src/tools/alerts-load.js

145 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-06-08 00:37:33 +00:00
'use strict';
const fs = require('fs');
const knex = require('../knex');
const args = require('../argv');
async function importActors(alert) {
await alert.actors.reduce(async (chain, actor) => {
await chain;
const actorEntry = await knex('actors')
2023-06-08 00:37:33 +00:00
.select('actors.id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.where('actors.slug', actor.slug)
2023-06-08 00:37:33 +00:00
.where((builder) => {
if (actor.entitySlug) {
2023-06-08 00:37:33 +00:00
builder
.where('entities.slug', actor.entitySlug)
.where('entities.type', actor.entityType);
2023-06-08 00:37:33 +00:00
}
})
.first();
if (!actorEntry) {
throw new Error(`No actor ${actor.slug} for ${alert.username}`);
2023-06-08 00:37:33 +00:00
}
await knex('alerts_actors').insert({
alert_id: alert.id,
actor_id: actorEntry.id,
});
}, Promise.resolve());
}
2023-06-08 00:37:33 +00:00
async function importTags(alert) {
await alert.tags.reduce(async (chain, tag) => {
await chain;
const tagEntry = await knex('tags')
.select('tags.id')
.where('slug', tag)
.first();
if (!tag) {
throw new Error(`No tag ${tag} for ${alert.username}`);
}
await knex('alerts_tags').insert({
alert_id: alert.id,
tag_id: tagEntry.id,
});
}, Promise.resolve());
2023-06-08 00:37:33 +00:00
}
async function importEntity(alert) {
2023-06-08 00:37:33 +00:00
if (alert.entitySlug) {
const entityEntry = await knex('entities')
2023-06-08 00:37:33 +00:00
.select('entities.id')
.where({
slug: alert.entitySlug,
type: alert.entityType,
})
.first();
if (!entityEntry) {
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
2023-06-08 00:37:33 +00:00
}
await knex('alerts_entities').insert({
alert_id: alert.id,
entity_id: entityEntry.id,
});
2023-06-08 00:37:33 +00:00
}
}
async function importStashes(alert, user) {
await alert.stashes.reduce(async (chain, stash) => {
await chain;
2023-06-08 00:37:33 +00:00
const stashEntry = await knex('stashes')
2023-06-08 00:37:33 +00:00
.select('stashes.id')
.where('stashes.slug', stash)
.where('stashes.user_id', user.id)
2023-06-08 00:37:33 +00:00
.first();
if (!stashEntry) {
throw new Error(`No stash ${stash} for ${alert.username}`);
2023-06-08 00:37:33 +00:00
}
await knex('alerts_stashes').insert({
alert_id: alert.id,
stash_id: stashEntry.id,
2023-06-08 00:37:33 +00:00
});
}, Promise.resolve());
2023-06-08 00:37:33 +00:00
}
async function load() {
const filename = process.argv[2];
const file = await fs.promises.readFile(filename, 'utf8');
const alerts = file.split('\n')
.filter(Boolean)
.map((data) => JSON.parse(data))
.filter((alert) => !args.username || alert.username === args.username);
await alerts.reduce(async (chain, alert, index) => {
await chain;
const user = await knex('users')
.select('id')
.where('username', alert.username)
.first();
if (!user) {
throw new Error(`No user ${alert.username}`);
}
const [{ id: alertId }] = await knex('alerts')
.insert({
user_id: user.id,
notify: alert.notify,
email: alert.email,
created_at: alert.createdAt,
})
.returning('id');
try {
await importActors({ ...alert, id: alertId }, user);
await importTags({ ...alert, id: alertId }, user);
await importEntity({ ...alert, id: alertId }, user);
await importStashes({ ...alert, id: alertId }, user);
} catch (error) {
await knex('alerts').where('id', alertId).delete();
throw error;
}
2023-06-08 00:37:33 +00:00
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
}, Promise.resolve());
process.exit();
}
load();