Added variant relation type.
This commit is contained in:
@@ -36,7 +36,8 @@ let searchTimeout = null;
|
|||||||
let searchCount = 0;
|
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
|
// 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}`,
|
key: `${type}-${direction}`,
|
||||||
type,
|
type,
|
||||||
direction,
|
direction,
|
||||||
|
|||||||
+91
-72
@@ -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)
|
// relations point from the derivation or compilation (source) to the original or compilation part (target)
|
||||||
export async function fetchSceneRelations(sceneId, { reqUser, ...context } = {}) {
|
// symmetric types such as variants have no meaningful direction, they're stored with the lowest scene ID as the source
|
||||||
const relations = await knex('scenes_relations')
|
function getRelatedScene(relation, sceneId) {
|
||||||
.select('id', 'source_scene_id', 'target_scene_id', 'type')
|
const isSource = relation.source_scene_id === sceneId;
|
||||||
.where('source_scene_id', sceneId)
|
|
||||||
.orWhere('target_scene_id', sceneId)
|
|
||||||
.orderBy('created_at', 'asc');
|
|
||||||
|
|
||||||
if (relations.length === 0) {
|
return {
|
||||||
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
|
||||||
|
};
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchRelationTypes() {
|
function queryRelations(queryKnex, sceneId) {
|
||||||
return knex('relations_types').orderBy('type').pluck('type');
|
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')
|
||||||
// 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')
|
|
||||||
.where((builder) => {
|
.where((builder) => {
|
||||||
builder
|
builder
|
||||||
.where('source_scene_id', sceneId)
|
.where('source_scene_id', sceneId)
|
||||||
.orWhere('target_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) => {
|
.modify((builder) => {
|
||||||
if (trx) {
|
if (trx) {
|
||||||
builder.transacting(trx);
|
builder.transacting(trx);
|
||||||
@@ -501,9 +513,8 @@ async function fetchRevisionRelations(sceneId, trx) {
|
|||||||
|
|
||||||
return relations.map((relation) => ({
|
return relations.map((relation) => ({
|
||||||
id: relation.id,
|
id: relation.id,
|
||||||
sceneId: relation.source_scene_id === sceneId ? relation.target_scene_id : relation.source_scene_id,
|
|
||||||
type: relation.type,
|
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);
|
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 curatedRelations = Object.values(Object.fromEntries(relations.map((relation) => {
|
||||||
const curatedRelation = {
|
const curatedRelation = {
|
||||||
sceneId: Number(relation?.sceneId),
|
sceneId: Number(relation.sceneId),
|
||||||
type: relation?.type,
|
type: relation.type,
|
||||||
direction: relation?.direction,
|
direction: typesByName[relation.type].symmetric ? 'both' : relation.direction,
|
||||||
};
|
};
|
||||||
|
|
||||||
return [getRelationKey(curatedRelation), curatedRelation];
|
return [getRelationKey(curatedRelation), curatedRelation];
|
||||||
@@ -526,24 +546,15 @@ async function curateRelationsEdit(sceneId, relations) {
|
|||||||
throw new HttpError('Relations must refer to a different scene', 400);
|
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);
|
throw new HttpError('Invalid relation direction', 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
const relatedSceneIds = Array.from(new Set(curatedRelations.map((relation) => relation.sceneId)));
|
const relatedSceneIds = Array.from(new Set(curatedRelations.map((relation) => relation.sceneId)));
|
||||||
|
|
||||||
const [types, existingSceneIds] = await Promise.all([
|
const existingSceneIds = relatedSceneIds.length > 0
|
||||||
fetchRelationTypes(),
|
? await knexOwner('releases').whereIn('id', relatedSceneIds).pluck('id')
|
||||||
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 missingSceneIds = relatedSceneIds.filter((relatedSceneId) => !existingSceneIds.includes(relatedSceneId));
|
const missingSceneIds = relatedSceneIds.filter((relatedSceneId) => !existingSceneIds.includes(relatedSceneId));
|
||||||
|
|
||||||
@@ -1164,12 +1175,19 @@ async function applySceneRelationsDelta(revision, delta, trx) {
|
|||||||
|
|
||||||
if (addedRelations.length > 0) {
|
if (addedRelations.length > 0) {
|
||||||
await knexOwner('scenes_relations')
|
await knexOwner('scenes_relations')
|
||||||
.insert(addedRelations.map((relation) => ({
|
.insert(addedRelations.map((relation) => {
|
||||||
source_scene_id: relation.direction === 'target' ? revision.scene_id : relation.sceneId,
|
// symmetric relations are stored with the lowest scene ID as the source
|
||||||
target_scene_id: relation.direction === 'target' ? relation.sceneId : revision.scene_id,
|
const isSource = relation.direction === 'both'
|
||||||
type: relation.type,
|
? revision.scene_id < relation.sceneId
|
||||||
created_by: revision.user_id,
|
: 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'])
|
.onConflict(['source_scene_id', 'target_scene_id', 'type'])
|
||||||
.ignore()
|
.ignore()
|
||||||
.transacting(trx);
|
.transacting(trx);
|
||||||
@@ -1358,11 +1376,12 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
|
|||||||
return [key, values];
|
return [key, values];
|
||||||
}).filter(Boolean));
|
}).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,
|
sceneId: relation.sceneId,
|
||||||
type: relation.type,
|
type: relation.type,
|
||||||
direction: relation.direction,
|
direction: relation.direction,
|
||||||
}));
|
}])));
|
||||||
|
|
||||||
const curatedRelations = edits.relations
|
const curatedRelations = edits.relations
|
||||||
? await curateRelationsEdit(scene.id, edits.relations)
|
? await curateRelationsEdit(scene.id, edits.relations)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
// labels per scene relation type, keyed by which side of the relation the related scene is on
|
// 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)
|
// 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 = {
|
const relationLabels = {
|
||||||
rerelease: { target: 'Rerelease of', source: 'Rereleased as' },
|
rerelease: { target: 'Rerelease of', source: 'Rereleased as' },
|
||||||
remaster: { target: 'Remaster of', source: 'Remastered as' },
|
remaster: { target: 'Remaster of', source: 'Remastered as' },
|
||||||
compilation: { target: 'Compilation features', source: 'Featured in compilation' },
|
compilation: { target: 'Compilation features', source: 'Featured in compilation' },
|
||||||
|
variant: { both: 'Variant of' },
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getRelationLabel(type, direction) {
|
export function getRelationLabel(type, direction) {
|
||||||
@@ -12,9 +14,9 @@ export function getRelationLabel(type, direction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fallback for types added later without a label
|
// fallback for types added later without a label
|
||||||
return direction === 'target'
|
return direction === 'source'
|
||||||
? `${type.charAt(0).toUpperCase()}${type.slice(1)} of`
|
? `Has ${type}`
|
||||||
: `Has ${type}`;
|
: `${type.charAt(0).toUpperCase()}${type.slice(1)} of`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default relationLabels;
|
export default relationLabels;
|
||||||
|
|||||||
Reference in New Issue
Block a user