forked from DebaucheryLibrarian/traxxx
168 lines
3.2 KiB
JavaScript
168 lines
3.2 KiB
JavaScript
|
'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();
|