Added actor edit to revision import tool.
This commit is contained in:
@@ -3,14 +3,15 @@ import process from 'node:process';
|
||||
import yargs from 'yargs';
|
||||
|
||||
import { knexOwner as knex } from '../src/knex.js';
|
||||
import { createSceneRevision, fetchRelationTypes, fetchRevisionRelations, reviewSceneRevision } from '../src/scenes.js';
|
||||
import { createSceneRevision, fetchRelationTypes, fetchRevisionRelations, fetchScenesById, reviewSceneRevision } from '../src/scenes.js';
|
||||
import { syncScenes } from '../src/sync.js';
|
||||
import { fetchUser } from '../src/users.js';
|
||||
import runTool from './run-tool.js';
|
||||
|
||||
// bulk scene curation, i.e. seeding relations, as revisions so each change is attributed and shows in the scene history
|
||||
// the file holds an array of { sceneId, edits, comment, addRelations }, edits as submitted by the scene edit page
|
||||
// the file holds an array of { sceneId, edits, comment, addRelations, addActors }, edits as submitted by the scene edit page
|
||||
// addRelations: [{ sceneId, type, direction, comment }] are added to the scene's current relations, rather than replacing them like edits.relations
|
||||
// addActors: [actorId] are added to the scene's current cast, rather than replacing it like edits.actors
|
||||
// validates every entry without storing anything unless --write or --apply is given
|
||||
const { argv } = yargs(process.argv.slice(2))
|
||||
.command('$0 <file>', 'import scene revisions from a JSON file')
|
||||
@@ -53,16 +54,21 @@ async function readEntries(file) {
|
||||
const invalidIndex = entries.findIndex((entry) => !Number.isInteger(entry?.sceneId)
|
||||
|| (entry.edits !== undefined && (typeof entry.edits !== 'object' || Array.isArray(entry.edits)))
|
||||
|| (entry.addRelations !== undefined && !Array.isArray(entry.addRelations))
|
||||
|| (!entry.edits && !entry.addRelations));
|
||||
|| (entry.addActors !== undefined && (!Array.isArray(entry.addActors) || !entry.addActors.every(Number.isInteger)))
|
||||
|| (!entry.edits && !entry.addRelations && !entry.addActors));
|
||||
|
||||
if (invalidIndex > -1) {
|
||||
throw new Error(`Entry ${invalidIndex} needs a numeric sceneId, and edits or addRelations: ${JSON.stringify(entries[invalidIndex])}`);
|
||||
throw new Error(`Entry ${invalidIndex} needs a numeric sceneId, and edits, addRelations or numeric addActors: ${JSON.stringify(entries[invalidIndex])}`);
|
||||
}
|
||||
|
||||
if (entries.some((entry) => entry.addRelations && entry.edits?.relations)) {
|
||||
throw new Error('An entry can have addRelations or edits.relations, not both');
|
||||
}
|
||||
|
||||
if (entries.some((entry) => entry.addActors && entry.edits?.actors)) {
|
||||
throw new Error('An entry can have addActors or edits.actors, not both');
|
||||
}
|
||||
|
||||
// a second revision would be based on the scene before the first applies, merge them instead
|
||||
const sceneIds = entries.map((entry) => entry.sceneId);
|
||||
const duplicateIds = Array.from(new Set(sceneIds.filter((sceneId, index) => sceneIds.indexOf(sceneId) !== index)));
|
||||
@@ -103,7 +109,7 @@ async function skipMissing(entries) {
|
||||
|
||||
omittedRelations += entry.addRelations.length - addRelations.length;
|
||||
|
||||
if (addRelations.length === 0 && Object.keys(entry.edits || {}).length === 0) {
|
||||
if (addRelations.length === 0 && !entry.addActors && Object.keys(entry.edits || {}).length === 0) {
|
||||
skipped += 1;
|
||||
return null;
|
||||
}
|
||||
@@ -125,9 +131,25 @@ async function getRelationKey(relation) {
|
||||
return `${symmetricTypes.has(relation.type) ? 'both' : relation.direction}:${relation.type}:${relation.sceneId}`;
|
||||
}
|
||||
|
||||
async function curateEdits(entry) {
|
||||
// the cast as the scene edit page submits it, unidentified actors included
|
||||
async function curateActors(entry, reqUser) {
|
||||
if (!entry.addActors) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const [scene] = await fetchScenesById([entry.sceneId], { reqUser });
|
||||
const currentActorIds = scene?.actors.map((actor) => actor.id) || [];
|
||||
|
||||
return {
|
||||
actors: Array.from(new Set([...currentActorIds, ...entry.addActors])),
|
||||
};
|
||||
}
|
||||
|
||||
async function curateEdits(entry, reqUser) {
|
||||
const actorEdits = await curateActors(entry, reqUser);
|
||||
|
||||
if (!entry.addRelations) {
|
||||
return entry.edits;
|
||||
return { ...entry.edits, ...actorEdits };
|
||||
}
|
||||
|
||||
const currentRelations = await fetchRevisionRelations(entry.sceneId);
|
||||
@@ -138,6 +160,7 @@ async function curateEdits(entry) {
|
||||
|
||||
return {
|
||||
...entry.edits,
|
||||
...actorEdits,
|
||||
relations: [
|
||||
...currentRelations.map((relation) => ({
|
||||
sceneId: relation.sceneId,
|
||||
@@ -167,7 +190,7 @@ async function importRevisions() {
|
||||
|
||||
try {
|
||||
const revision = await createSceneRevision(entry.sceneId, {
|
||||
edits: await curateEdits(entry),
|
||||
edits: await curateEdits(entry, reqUser),
|
||||
comment: entry.comment || argv.comment,
|
||||
}, reqUser, { dryRun: true });
|
||||
|
||||
@@ -202,7 +225,7 @@ async function importRevisions() {
|
||||
const acc = await chain;
|
||||
|
||||
const revision = await createSceneRevision(entry.sceneId, {
|
||||
edits: await curateEdits(entry),
|
||||
edits: await curateEdits(entry, reqUser),
|
||||
comment: entry.comment || argv.comment,
|
||||
}, reqUser);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user