Added alert transfer tools to repo.

This commit is contained in:
DebaucheryLibrarian 2023-06-08 02:37:33 +02:00
parent 4429169166
commit 9bdd3ff2f3
2 changed files with 232 additions and 0 deletions

167
src/tools/alerts-load.js Normal file
View File

@ -0,0 +1,167 @@
'use strict';
const fs = require('fs');
const knex = require('../knex');
const args = require('../argv');
async function getActorId(alert) {
if (alert.actorSlug) {
const actor = await knex('actors')
.select('actors.id')
.leftJoin('entities', 'entities.id', 'actors.entity_id')
.where('actors.slug', alert.actorSlug)
.where((builder) => {
if (alert.actorEntitySlug) {
builder
.where('entities.slug', alert.actorEntitySlug)
.where('entities.type', alert.actorEntityType);
}
})
.first();
if (actor) {
return actor.id;
}
throw new Error(`No actor ${alert.actorSlug} for ${alert.username}`);
}
return null;
}
async function getEntityId(alert) {
if (alert.entitySlug) {
const entity = await knex('entities')
.select('entities.id')
.where({
slug: alert.entitySlug,
type: alert.entityType,
})
.first();
if (entity) {
return entity.id;
}
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,
});
}
if (tagId) {
await knex('alerts_tags').insert({
alert_id: alertId,
tag_id: tagId,
});
}
if (stashId) {
await knex('alerts_stashes').insert({
alert_id: alertId,
stash_id: stashId,
});
}
}
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 actorId = await getActorId(alert);
const entityId = await getEntityId(alert);
const tagId = await getTagId(alert);
const stashId = await getStashId(alert, user.id);
await createAlert(alert, {
userId: user.id,
actorId,
entityId,
stashId,
tagId,
});
console.log(`Loaded alert ${index + 1}/${alerts.length} from ${alert.username}`);
}, Promise.resolve());
process.exit();
}
load();

65
src/tools/alerts-save.js Normal file
View File

@ -0,0 +1,65 @@
'use strict';
const fs = require('fs');
const moment = require('moment');
const knex = require('../knex');
async function save() {
const filename = `alerts-${moment().format('YYYY-MM-DD_hh_mm')}.json`;
const alerts = await knex('alerts')
.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');
let savedAlerts = 0;
await alerts.reduce(async (chain, alert) => {
await chain;
const curatedAlert = JSON.stringify({
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,
entitySlug: alert.entity_slug,
entityType: alert.entity_type,
stashSlug: alert.stash_slug,
tagSlug: alert.tag_slug,
});
await fs.promises.appendFile(filename, `${curatedAlert}\n`);
console.log(`Saved ${alert.username} alert`);
savedAlerts += 1;
}, Promise.resolve());
console.log(`Saved ${savedAlerts} alerts to ${filename}`);
process.exit();
}
save();