Compare commits

..

No commits in common. "d0b19752e1166b10805bcf5221d33f8eaa10a98f" and "9bdd3ff2f3db650ebd999b5c48530e39451c11ee" have entirely different histories.

4 changed files with 122 additions and 105 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "traxxx", "name": "traxxx",
"version": "1.227.2", "version": "1.227.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "traxxx", "name": "traxxx",
"version": "1.227.2", "version": "1.227.1",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@casl/ability": "^5.2.2", "@casl/ability": "^5.2.2",

View File

@ -1,6 +1,6 @@
{ {
"name": "traxxx", "name": "traxxx",
"version": "1.227.2", "version": "1.227.1",
"description": "All the latest porn releases in one place", "description": "All the latest porn releases in one place",
"main": "src/app.js", "main": "src/app.js",
"scripts": { "scripts": {

View File

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

View File

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