Added variant relation type.

This commit is contained in:
2026-11-21 03:07:32 +01:00
parent 37309473f8
commit 25c68f96a9
3 changed files with 98 additions and 76 deletions
+2 -1
View File
@@ -36,7 +36,8 @@ let searchTimeout = null;
let searchCount = 0;
// each type can be read from either side, e.g. this scene is a 'Remaster of' or 'Remastered as' the selected scene
const options = computed(() => props.types.flatMap((type) => ['target', 'source'].map((direction) => ({
// symmetric types like variants read the same both ways
const options = computed(() => props.types.flatMap(({ type, symmetric }) => (symmetric ? ['both'] : ['target', 'source']).map((direction) => ({
key: `${type}-${direction}`,
type,
direction,
+91 -72
View File
@@ -439,60 +439,72 @@ export async function fetchScenesById(sceneIds, { reqUser, ...context } = {}) {
}
// relations point from the derivation or compilation (source) to the original or compilation part (target)
export async function fetchSceneRelations(sceneId, { reqUser, ...context } = {}) {
const relations = await knex('scenes_relations')
.select('id', 'source_scene_id', 'target_scene_id', 'type')
.where('source_scene_id', sceneId)
.orWhere('target_scene_id', sceneId)
.orderBy('created_at', 'asc');
// symmetric types such as variants have no meaningful direction, they're stored with the lowest scene ID as the source
function getRelatedScene(relation, sceneId) {
const isSource = relation.source_scene_id === sceneId;
if (relations.length === 0) {
return [];
}
const relatedSceneIds = Array.from(new Set(relations.map((relation) => (relation.source_scene_id === sceneId
? relation.target_scene_id
: relation.source_scene_id))));
const relatedScenes = await fetchScenesById(relatedSceneIds, { reqUser, ...context });
const relatedScenesById = Object.fromEntries(relatedScenes.map((scene) => [scene.id, scene]));
return relations.map((relation) => {
const isSource = relation.source_scene_id === sceneId;
const relatedScene = relatedScenesById[isSource ? relation.target_scene_id : relation.source_scene_id];
if (!relatedScene) {
return null;
}
return {
id: relation.id,
type: relation.type,
direction: isSource ? 'target' : 'source', // which side of the relation the related scene is on
scene: relatedScene,
};
}).filter(Boolean);
return {
sceneId: isSource ? relation.target_scene_id : relation.source_scene_id,
direction: (relation.symmetric && 'both') || (isSource ? 'target' : 'source'), // which side of the relation the related scene is on
};
}
export async function fetchRelationTypes() {
return knex('relations_types').orderBy('type').pluck('type');
}
// relations in revisions are stored relative to the revised scene: { sceneId, type, direction }
// sceneId is the related scene, direction is the side of the relation the related scene is on
function getRelationKey(relation) {
return `${relation.direction}:${relation.type}:${relation.sceneId}`;
}
async function fetchRevisionRelations(sceneId, trx) {
const relations = await knexOwner('scenes_relations')
.select('id', 'source_scene_id', 'target_scene_id', 'type')
function queryRelations(queryKnex, sceneId) {
return queryKnex('scenes_relations')
.select('scenes_relations.id', 'source_scene_id', 'target_scene_id', 'scenes_relations.type', 'relations_types.symmetric')
.leftJoin('relations_types', 'relations_types.type', 'scenes_relations.type')
.where((builder) => {
builder
.where('source_scene_id', sceneId)
.orWhere('target_scene_id', sceneId);
})
.orderBy('created_at', 'asc')
.orderBy('scenes_relations.created_at', 'asc');
}
export async function fetchSceneRelations(sceneId, { reqUser, ...context } = {}) {
const relations = await queryRelations(knex, sceneId);
if (relations.length === 0) {
return [];
}
const relatedSceneIds = Array.from(new Set(relations.map((relation) => getRelatedScene(relation, sceneId).sceneId)));
const relatedScenes = await fetchScenesById(relatedSceneIds, { reqUser, ...context });
const relatedScenesById = Object.fromEntries(relatedScenes.map((scene) => [scene.id, scene]));
// a symmetric relation stored both ways round is a curation error, but should only show once
return Object.values(Object.fromEntries(relations.map((relation) => {
const { sceneId: relatedSceneId, direction } = getRelatedScene(relation, sceneId);
const relatedScene = relatedScenesById[relatedSceneId];
if (!relatedScene) {
return null;
}
return [getRelationKey({ sceneId: relatedSceneId, type: relation.type, direction }), {
id: relation.id,
type: relation.type,
direction,
scene: relatedScene,
}];
}).filter(Boolean).reverse())).reverse(); // keep the oldest of any duplicates
}
export async function fetchRelationTypes() {
return knex('relations_types')
.select('type', 'symmetric')
.orderBy('type');
}
// relations in revisions are stored relative to the revised scene: { sceneId, type, direction }
// sceneId is the related scene, direction is the side of the relation the related scene is on, or 'both' for symmetric types
function getRelationKey(relation) {
return `${relation.direction}:${relation.type}:${relation.sceneId}`;
}
async function fetchRevisionRelations(sceneId, trx) {
const relations = await queryRelations(knexOwner, sceneId)
.modify((builder) => {
if (trx) {
builder.transacting(trx);
@@ -501,9 +513,8 @@ async function fetchRevisionRelations(sceneId, trx) {
return relations.map((relation) => ({
id: relation.id,
sceneId: relation.source_scene_id === sceneId ? relation.target_scene_id : relation.source_scene_id,
type: relation.type,
direction: relation.source_scene_id === sceneId ? 'target' : 'source',
...getRelatedScene(relation, sceneId),
}));
}
@@ -512,11 +523,20 @@ async function curateRelationsEdit(sceneId, relations) {
throw new HttpError('Relations must be a list', 400);
}
const types = await fetchRelationTypes();
const typesByName = Object.fromEntries(types.map((type) => [type.type, type]));
const invalidTypes = relations.filter((relation) => !typesByName[relation?.type]).map((relation) => relation?.type);
if (invalidTypes.length > 0) {
throw new HttpError(`Invalid relation types: ${Array.from(new Set(invalidTypes)).join(', ')}`, 400);
}
const curatedRelations = Object.values(Object.fromEntries(relations.map((relation) => {
const curatedRelation = {
sceneId: Number(relation?.sceneId),
type: relation?.type,
direction: relation?.direction,
sceneId: Number(relation.sceneId),
type: relation.type,
direction: typesByName[relation.type].symmetric ? 'both' : relation.direction,
};
return [getRelationKey(curatedRelation), curatedRelation];
@@ -526,24 +546,15 @@ async function curateRelationsEdit(sceneId, relations) {
throw new HttpError('Relations must refer to a different scene', 400);
}
if (curatedRelations.some((relation) => !['source', 'target'].includes(relation.direction))) {
if (curatedRelations.some((relation) => !['source', 'target', 'both'].includes(relation.direction))) {
throw new HttpError('Invalid relation direction', 400);
}
const relatedSceneIds = Array.from(new Set(curatedRelations.map((relation) => relation.sceneId)));
const [types, existingSceneIds] = await Promise.all([
fetchRelationTypes(),
relatedSceneIds.length > 0
? knexOwner('releases').whereIn('id', relatedSceneIds).pluck('id')
: [],
]);
const invalidTypes = curatedRelations.filter((relation) => !types.includes(relation.type)).map((relation) => relation.type);
if (invalidTypes.length > 0) {
throw new HttpError(`Invalid relation types: ${Array.from(new Set(invalidTypes)).join(', ')}`, 400);
}
const existingSceneIds = relatedSceneIds.length > 0
? await knexOwner('releases').whereIn('id', relatedSceneIds).pluck('id')
: [];
const missingSceneIds = relatedSceneIds.filter((relatedSceneId) => !existingSceneIds.includes(relatedSceneId));
@@ -1164,12 +1175,19 @@ async function applySceneRelationsDelta(revision, delta, trx) {
if (addedRelations.length > 0) {
await knexOwner('scenes_relations')
.insert(addedRelations.map((relation) => ({
source_scene_id: relation.direction === 'target' ? revision.scene_id : relation.sceneId,
target_scene_id: relation.direction === 'target' ? relation.sceneId : revision.scene_id,
type: relation.type,
created_by: revision.user_id,
})))
.insert(addedRelations.map((relation) => {
// symmetric relations are stored with the lowest scene ID as the source
const isSource = relation.direction === 'both'
? revision.scene_id < relation.sceneId
: relation.direction === 'target';
return {
source_scene_id: isSource ? revision.scene_id : relation.sceneId,
target_scene_id: isSource ? relation.sceneId : revision.scene_id,
type: relation.type,
created_by: revision.user_id,
};
}))
.onConflict(['source_scene_id', 'target_scene_id', 'type'])
.ignore()
.transacting(trx);
@@ -1358,11 +1376,12 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
return [key, values];
}).filter(Boolean));
baseScene.relations = baseRelations.map((relation) => ({
// symmetric relations stored both ways round share a key
baseScene.relations = Object.values(Object.fromEntries(baseRelations.map((relation) => [getRelationKey(relation), {
sceneId: relation.sceneId,
type: relation.type,
direction: relation.direction,
}));
}])));
const curatedRelations = edits.relations
? await curateRelationsEdit(scene.id, edits.relations)
+5 -3
View File
@@ -1,9 +1,11 @@
// labels per scene relation type, keyed by which side of the relation the related scene is on
// relations point from the derivation or compilation (source) to the original or compilation part (target)
// symmetric types like variants have no direction, and are keyed 'both'
const relationLabels = {
rerelease: { target: 'Rerelease of', source: 'Rereleased as' },
remaster: { target: 'Remaster of', source: 'Remastered as' },
compilation: { target: 'Compilation features', source: 'Featured in compilation' },
variant: { both: 'Variant of' },
};
export function getRelationLabel(type, direction) {
@@ -12,9 +14,9 @@ export function getRelationLabel(type, direction) {
}
// fallback for types added later without a label
return direction === 'target'
? `${type.charAt(0).toUpperCase()}${type.slice(1)} of`
: `Has ${type}`;
return direction === 'source'
? `Has ${type}`
: `${type.charAt(0).toUpperCase()}${type.slice(1)} of`;
}
export default relationLabels;