Updated profile aliasing tool to flatten chained aliases.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
/*
|
||||
* backfills actors_profiles.alias_id, the alias name a profile was found under
|
||||
* 0. aliases of aliases, left by merges before they repointed aliases, point to their main actor directly, and so do scenes linked to an alias
|
||||
* 1. profiles still held by an alias actor move to its main actor, unless the main actor already has a profile for that entity
|
||||
* 2. main actor profiles get the name their entity uses, compared by slug so e.g. LaSirena69 and Lasirena69 are one name
|
||||
* decided by a cut-off where the other names were retired, else the profile URL containing the name, else a dominant share of scene credits
|
||||
@@ -12,6 +13,7 @@ const yargs = require('yargs');
|
||||
const { hideBin } = require('yargs/helpers');
|
||||
|
||||
const knex = require('../knex');
|
||||
const { resyncSceneSearch } = require('../update-search');
|
||||
const { fetchActorAliases, getEntityNames, groupEntityNames, resolveEntityName } = require('../actor-aliases');
|
||||
|
||||
const args = yargs(hideBin(process.argv))
|
||||
@@ -47,6 +49,115 @@ function getProfileLabel(profile) {
|
||||
return `#${profile.id} ${profile.actor_name} on ${profile.entity_slug}`;
|
||||
}
|
||||
|
||||
// the main actor at the end of each alias chain, a chain that loops back on itself has no main actor
|
||||
async function fetchAliasRoots() {
|
||||
const { rows } = await knex.raw(`
|
||||
WITH RECURSIVE alias_chains AS (
|
||||
SELECT id, alias_for AS root_id, ARRAY[id, alias_for] AS path
|
||||
FROM actors
|
||||
WHERE alias_for IS NOT NULL
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT alias_chains.id, actors.alias_for, alias_chains.path || actors.alias_for
|
||||
FROM alias_chains
|
||||
JOIN actors ON actors.id = alias_chains.root_id
|
||||
WHERE actors.alias_for IS NOT NULL
|
||||
AND NOT actors.alias_for = ANY(alias_chains.path)
|
||||
)
|
||||
SELECT DISTINCT ON (alias_chains.id) alias_chains.id, alias_chains.root_id, alias_chains.path, roots.alias_for IS NOT NULL AS is_cycle
|
||||
FROM alias_chains
|
||||
JOIN actors AS roots ON roots.id = alias_chains.root_id
|
||||
ORDER BY alias_chains.id, array_length(alias_chains.path, 1) DESC
|
||||
`);
|
||||
|
||||
return Object.fromEntries(rows.map((row) => [row.id, {
|
||||
rootId: row.root_id,
|
||||
path: row.path,
|
||||
isCycle: row.is_cycle,
|
||||
}]));
|
||||
}
|
||||
|
||||
async function flattenAliasChains(aliasRoots) {
|
||||
const chains = Object.entries(aliasRoots).filter(([, root]) => root.path.length > 2);
|
||||
const flattenable = chains.filter(([, root]) => !root.isCycle);
|
||||
const cycles = chains.filter(([, root]) => root.isCycle);
|
||||
|
||||
const actorNames = Object.fromEntries((await knex('actors')
|
||||
.select('id', 'name')
|
||||
.whereIn('id', Array.from(new Set(chains.flatMap(([, root]) => root.path)))))
|
||||
.map((actor) => [actor.id, actor.name]));
|
||||
|
||||
// scenes are linked to the main actor of a credited alias, which used to be an alias itself in a chain
|
||||
const sceneLinks = await knex('releases_actors')
|
||||
.select('releases_actors.actor_id', knex.raw('count(*)::integer as scenes'))
|
||||
.join('actors', 'actors.id', 'releases_actors.actor_id')
|
||||
.whereNotNull('actors.alias_for')
|
||||
.groupBy('releases_actors.actor_id');
|
||||
|
||||
const sceneLinkCount = sceneLinks.reduce((acc, link) => acc + link.scenes, 0);
|
||||
|
||||
console.log(`Step 0: ${flattenable.length} aliases of aliases point to their main actor, ${cycles.length} alias loops need fixing by hand, ${sceneLinkCount} scene links on ${sceneLinks.length} aliases move to their main actor`);
|
||||
|
||||
const formatPath = (root) => root.path.map((actorId) => `${actorNames[actorId]} #${actorId}`).join(' > ');
|
||||
|
||||
flattenable.forEach(([, root]) => console.log(` flatten ${formatPath(root)}`));
|
||||
cycles.forEach(([, root]) => console.log(` loop ${formatPath(root)}`));
|
||||
|
||||
sceneLinks.forEach((link) => {
|
||||
const root = aliasRoots[link.actor_id];
|
||||
|
||||
console.log(` relink ${link.scenes} scenes of ${actorNames[link.actor_id] || `#${link.actor_id}`} to ${root.isCycle ? 'nothing, loop' : `#${root.rootId}`}`);
|
||||
});
|
||||
|
||||
if (!args.apply) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return knex.transaction(async (trx) => {
|
||||
await Promise.all(flattenable.map(async ([aliasId, root]) => trx('actors')
|
||||
.where('id', aliasId)
|
||||
.update('alias_for', root.rootId)));
|
||||
|
||||
const linkedAliasIds = sceneLinks.map((link) => link.actor_id).filter((actorId) => !aliasRoots[actorId].isCycle);
|
||||
|
||||
if (linkedAliasIds.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// a scene may already credit the main actor, or several aliases of the same main actor, like a merge we keep one
|
||||
await trx.raw(`
|
||||
DELETE FROM releases_actors
|
||||
USING actors
|
||||
WHERE actors.id = releases_actors.actor_id
|
||||
AND releases_actors.actor_id = ANY(:linkedAliasIds)
|
||||
AND (EXISTS (
|
||||
SELECT 1 FROM releases_actors AS main_links
|
||||
WHERE main_links.release_id = releases_actors.release_id
|
||||
AND main_links.actor_id = actors.alias_for
|
||||
) OR EXISTS (
|
||||
SELECT 1 FROM releases_actors AS sibling_links
|
||||
JOIN actors AS siblings ON siblings.id = sibling_links.actor_id
|
||||
WHERE sibling_links.release_id = releases_actors.release_id
|
||||
AND siblings.alias_for = actors.alias_for
|
||||
AND sibling_links.id < releases_actors.id
|
||||
))
|
||||
`, { linkedAliasIds });
|
||||
|
||||
const relinked = await trx.raw(`
|
||||
UPDATE releases_actors
|
||||
SET actor_id = actors.alias_for,
|
||||
alias_id = coalesce(releases_actors.alias_id, releases_actors.actor_id)
|
||||
FROM actors
|
||||
WHERE actors.id = releases_actors.actor_id
|
||||
AND releases_actors.actor_id = ANY(:linkedAliasIds)
|
||||
RETURNING releases_actors.release_id
|
||||
`, { linkedAliasIds });
|
||||
|
||||
return Array.from(new Set(relinked.rows.map((row) => row.release_id)));
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchAliasHeldProfiles() {
|
||||
return knex('actors_profiles')
|
||||
.select(
|
||||
@@ -55,20 +166,42 @@ async function fetchAliasHeldProfiles() {
|
||||
'actors_profiles.alias_id',
|
||||
'aliases.id as held_by_id',
|
||||
'aliases.name as alias_name',
|
||||
'aliases.alias_for as main_actor_id',
|
||||
'actors.name as actor_name',
|
||||
'entities.slug as entity_slug',
|
||||
knex.raw('EXISTS (SELECT 1 FROM actors_profiles AS main_profiles WHERE main_profiles.actor_id = aliases.alias_for AND main_profiles.entity_id = actors_profiles.entity_id) as main_has_profile'),
|
||||
)
|
||||
.join('actors as aliases', 'aliases.id', 'actors_profiles.actor_id')
|
||||
.join('actors', 'actors.id', 'aliases.alias_for')
|
||||
.leftJoin('entities', 'entities.id', 'actors_profiles.entity_id')
|
||||
.whereNotNull('aliases.alias_for')
|
||||
.whereNotNull('actors_profiles.entity_id'); // an alias' main profile holds manual edits, it's not a source profile
|
||||
}
|
||||
|
||||
async function moveAliasHeldProfiles() {
|
||||
const profiles = await fetchAliasHeldProfiles();
|
||||
async function moveAliasHeldProfiles(aliasRoots) {
|
||||
const heldProfiles = (await fetchAliasHeldProfiles()).filter((profile) => !aliasRoots[profile.held_by_id]?.isCycle);
|
||||
const mainActorIds = Array.from(new Set(heldProfiles.map((profile) => aliasRoots[profile.held_by_id].rootId)));
|
||||
|
||||
const [mainActors, mainProfiles] = await Promise.all([
|
||||
knex('actors').select('id', 'name').whereIn('id', mainActorIds),
|
||||
knex('actors_profiles').select('actor_id', 'entity_id').whereIn('actor_id', mainActorIds).whereNotNull('entity_id'),
|
||||
]);
|
||||
|
||||
const mainActorNames = Object.fromEntries(mainActors.map((actor) => [actor.id, actor.name]));
|
||||
const takenEntities = new Set(mainProfiles.map((profile) => `${profile.actor_id}:${profile.entity_id}`));
|
||||
|
||||
// two aliases may hold a profile for the same entity, only the first can move
|
||||
const profiles = heldProfiles.map((profile) => {
|
||||
const mainActorId = aliasRoots[profile.held_by_id].rootId;
|
||||
const key = `${mainActorId}:${profile.entity_id}`;
|
||||
const mainHasProfile = takenEntities.has(key);
|
||||
|
||||
takenEntities.add(key);
|
||||
|
||||
return {
|
||||
...profile,
|
||||
main_actor_id: mainActorId,
|
||||
actor_name: mainActorNames[mainActorId],
|
||||
main_has_profile: mainHasProfile,
|
||||
};
|
||||
});
|
||||
|
||||
const movable = profiles.filter((profile) => !profile.main_has_profile);
|
||||
const conflicting = profiles.filter((profile) => profile.main_has_profile);
|
||||
|
||||
@@ -212,7 +345,9 @@ async function assignAliases() {
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
const movedActorIds = await moveAliasHeldProfiles();
|
||||
const aliasRoots = await fetchAliasRoots();
|
||||
const relinkedSceneIds = await flattenAliasChains(aliasRoots);
|
||||
const movedActorIds = await moveAliasHeldProfiles(aliasRoots);
|
||||
|
||||
await assignAliases();
|
||||
|
||||
@@ -221,6 +356,12 @@ async function init() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (relinkedSceneIds.length > 0) {
|
||||
await resyncSceneSearch(relinkedSceneIds);
|
||||
|
||||
console.log(`Resyncing ${relinkedSceneIds.length} relinked scenes`);
|
||||
}
|
||||
|
||||
if (movedActorIds.length > 0) {
|
||||
// moved profiles now count towards their main actor
|
||||
const { interpolateProfiles } = require('../actors'); // eslint-disable-line global-require
|
||||
|
||||
Reference in New Issue
Block a user