Fixed alert tool to transfer combinations.

This commit is contained in:
DebaucheryLibrarian 2023-06-08 03:22:46 +02:00
parent 9bdd3ff2f3
commit 9c63b31dfa
2 changed files with 103 additions and 120 deletions

View File

@ -5,34 +5,57 @@ const fs = require('fs');
const knex = require('../knex');
const args = require('../argv');
async function getActorId(alert) {
if (alert.actorSlug) {
const actor = await knex('actors')
async function importActors(alert) {
await alert.actors.reduce(async (chain, actor) => {
await chain;
const actorEntry = await knex('actors')
.select('actors.id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.where('actors.slug', alert.actorSlug)
.where('actors.slug', actor.slug)
.where((builder) => {
if (alert.actorEntitySlug) {
if (actor.entitySlug) {
builder
.where('entities.slug', alert.actorEntitySlug)
.where('entities.type', alert.actorEntityType);
.where('entities.slug', actor.entitySlug)
.where('entities.type', actor.entityType);
}
})
.first();
if (actor) {
return actor.id;
if (!actorEntry) {
throw new Error(`No actor ${actor.slug} for ${alert.username}`);
}
throw new Error(`No actor ${alert.actorSlug} for ${alert.username}`);
}
return null;
await knex('alerts_actors').insert({
alert_id: alert.id,
actor_id: actorEntry.id,
});
}, Promise.resolve());
}
async function getEntityId(alert) {
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());
}
async function importEntity(alert) {
if (alert.entitySlug) {
const entity = await knex('entities')
const entityEntry = await knex('entities')
.select('entities.id')
.where({
slug: alert.entitySlug,
@ -40,88 +63,36 @@ async function getEntityId(alert) {
})
.first();
if (entity) {
return entity.id;
if (!entityEntry) {
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
}
throw new Error(`No entity ${alert.entitySlug} for ${alert.username}`);
}
return null;
}
async function getTagId(alert) {
if (alert.tagSlug) {
const tag = await knex('tags')
.select('tags.id')
.where('slug', alert.tagSlug)
.first();
if (tag) {
return tag.id;
}
throw new Error(`No tag ${alert.stashSlug} for ${alert.username}`);
}
return null;
}
async function getStashId(alert, userId) {
if (alert.stashSlug) {
const stash = await knex('stashes')
.select('stashes.id')
.where('stashes.slug', alert.stashSlug)
.where('stashes.user_id', userId)
.first();
if (stash) {
return stash.id;
}
throw new Error(`No stash ${alert.stashSlug} for ${alert.username}`);
}
return null;
}
async function createAlert(alert, { userId, actorId, entityId, tagId, stashId }) {
const [alertId] = await knex('alerts')
.insert({
user_id: userId,
notify: alert.notify,
email: alert.email,
created_at: alert.createdAt,
})
.returning('id');
if (actorId) {
await knex('alerts_actors').insert({
alert_id: alertId,
actor_id: actorId,
});
}
if (entityId) {
await knex('alerts_entities').insert({
alert_id: alertId,
entity_id: entityId,
alert_id: alert.id,
entity_id: entityEntry.id,
});
}
}
if (tagId) {
await knex('alerts_tags').insert({
alert_id: alertId,
tag_id: tagId,
});
}
async function importStashes(alert, user) {
await alert.stashes.reduce(async (chain, stash) => {
await chain;
const stashEntry = await knex('stashes')
.select('stashes.id')
.where('stashes.slug', stash)
.where('stashes.user_id', user.id)
.first();
if (!stashEntry) {
throw new Error(`No stash ${stash} for ${alert.username}`);
}
if (stashId) {
await knex('alerts_stashes').insert({
alert_id: alertId,
stash_id: stashId,
alert_id: alert.id,
stash_id: stashEntry.id,
});
}
}, Promise.resolve());
}
async function load() {
@ -145,18 +116,24 @@ async function load() {
throw new Error(`No user ${alert.username}`);
}
const actorId = await getActorId(alert);
const entityId = await getEntityId(alert);
const tagId = await getTagId(alert);
const stashId = await getStashId(alert, user.id);
const [alertId] = await knex('alerts')
.insert({
user_id: user.id,
notify: alert.notify,
email: alert.email,
created_at: alert.createdAt,
})
.returning('id');
await createAlert(alert, {
userId: user.id,
actorId,
entityId,
stashId,
tagId,
});
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;
}
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
}, Promise.resolve());

View File

@ -12,45 +12,51 @@ async function save() {
.select([
'alerts.*',
'users.username',
'actors.slug as actor_slug',
'actors_entities.slug as actor_entity_slug',
'actors_entities.type as actor_entity_type',
'entities.slug as entity_slug',
'entities.type as entity_type',
'stashes.slug as stash_slug',
'tags.slug as tag_slug',
])
.leftJoin('users', 'users.id', 'alerts.user_id')
.leftJoin('alerts_actors', 'alerts_actors.alert_id', 'alerts.id')
.leftJoin('alerts_entities', 'alerts_entities.alert_id', 'alerts.id')
.leftJoin('alerts_stashes', 'alerts_stashes.alert_id', 'alerts.id')
.leftJoin('alerts_tags', 'alerts_tags.alert_id', 'alerts.id')
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id')
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
.leftJoin('stashes', 'stashes.id', 'alerts_stashes.stash_id')
.leftJoin('entities as actors_entities', 'actors_entities.id', 'actors.entity_id');
.leftJoin('entities', 'entities.id', 'alerts_entities.entity_id');
let savedAlerts = 0;
await alerts.reduce(async (chain, alert) => {
await chain;
const curatedAlert = JSON.stringify({
const actors = await knex('alerts_actors')
.select('actors.slug', 'entities.slug as entity_slug', 'entities.type as entity_type')
.leftJoin('actors', 'actors.id', 'alerts_actors.actor_id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.where('alert_id', alert.id);
const tags = await knex('alerts_tags')
.select('tags.slug')
.leftJoin('tags', 'tags.id', 'alerts_tags.tag_id')
.where('alert_id', alert.id);
const stashes = await knex('alerts_stashes')
.select('stashes.slug')
.leftJoin('stashes', 'stashes.id', 'alerts_stashes.stash_id')
.where('alert_id', alert.id);
const curatedAlert = {
username: alert.username,
notify: alert.notify,
email: alert.email,
createdAt: alert.created_at,
actorSlug: alert.actor_slug,
actorEntitySlug: alert.actor_entity_slug,
actorEntityType: alert.actor_entity_type,
actors: actors.map((actor) => ({
slug: actor.slug,
entitySlug: actor.entity_slug,
entityType: actor.entity_type,
})),
tags: tags.map((tag) => tag.slug),
stashes: stashes.map((stash) => stash.slug),
entitySlug: alert.entity_slug,
entityType: alert.entity_type,
stashSlug: alert.stash_slug,
tagSlug: alert.tag_slug,
});
};
await fs.promises.appendFile(filename, `${curatedAlert}\n`);
await fs.promises.appendFile(filename, `${JSON.stringify(curatedAlert)}\n`);
console.log(`Saved ${alert.username} alert`);