Added scene relations.
This commit is contained in:
+191
-1
@@ -438,6 +438,122 @@ export async function fetchScenesById(sceneIds, { reqUser, ...context } = {}) {
|
||||
}).filter(Boolean);
|
||||
}
|
||||
|
||||
// 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');
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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')
|
||||
.where((builder) => {
|
||||
builder
|
||||
.where('source_scene_id', sceneId)
|
||||
.orWhere('target_scene_id', sceneId);
|
||||
})
|
||||
.orderBy('created_at', 'asc')
|
||||
.modify((builder) => {
|
||||
if (trx) {
|
||||
builder.transacting(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',
|
||||
}));
|
||||
}
|
||||
|
||||
async function curateRelationsEdit(sceneId, relations) {
|
||||
if (!Array.isArray(relations)) {
|
||||
throw new HttpError('Relations must be a list', 400);
|
||||
}
|
||||
|
||||
const curatedRelations = Object.values(Object.fromEntries(relations.map((relation) => {
|
||||
const curatedRelation = {
|
||||
sceneId: Number(relation?.sceneId),
|
||||
type: relation?.type,
|
||||
direction: relation?.direction,
|
||||
};
|
||||
|
||||
return [getRelationKey(curatedRelation), curatedRelation];
|
||||
})));
|
||||
|
||||
if (curatedRelations.some((relation) => !Number.isInteger(relation.sceneId) || relation.sceneId === sceneId)) {
|
||||
throw new HttpError('Relations must refer to a different scene', 400);
|
||||
}
|
||||
|
||||
if (curatedRelations.some((relation) => !['source', 'target'].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 missingSceneIds = relatedSceneIds.filter((relatedSceneId) => !existingSceneIds.includes(relatedSceneId));
|
||||
|
||||
if (missingSceneIds.length > 0) {
|
||||
throw new HttpError(`Related scenes do not exist: ${missingSceneIds.join(', ')}`, 404);
|
||||
}
|
||||
|
||||
return curatedRelations;
|
||||
}
|
||||
|
||||
const sqlImplied = ['scenes_stashed'];
|
||||
|
||||
function curateOptions(options) {
|
||||
@@ -919,10 +1035,19 @@ export async function fetchSceneRevisions(revisionId, filters = {}, reqUser) {
|
||||
const tagIds = Array.from(new Set(revisions.flatMap((revision) => [...revision.base.tags, ...(revision.deltas.find((delta) => delta.key === 'tags')?.value || [])].map((tag) => tag.id))));
|
||||
const movieIds = Array.from(new Set(revisions.flatMap((revision) => [...revision.base.movies, ...(revision.deltas.find((delta) => delta.key === 'movies')?.value || [])])));
|
||||
|
||||
const [actors, tags, movies] = await Promise.all([
|
||||
// revisions from before relations existed have no base relations
|
||||
const relationSceneIds = Array.from(new Set(revisions.flatMap((revision) => [
|
||||
...(revision.base.relations || []),
|
||||
...(revision.deltas.find((delta) => delta.key === 'relations')?.value || []),
|
||||
].map((relation) => relation.sceneId))));
|
||||
|
||||
const [actors, tags, movies, relationScenes] = await Promise.all([
|
||||
fetchActorsById(actorIds),
|
||||
fetchTagsById(tagIds),
|
||||
fetchMoviesById(movieIds),
|
||||
relationSceneIds.length > 0
|
||||
? knexOwner('releases').select('id', 'title', 'slug').whereIn('id', relationSceneIds)
|
||||
: [],
|
||||
]);
|
||||
|
||||
const curatedRevisions = revisions.map((revision) => curateRevision(revision));
|
||||
@@ -934,6 +1059,7 @@ export async function fetchSceneRevisions(revisionId, filters = {}, reqUser) {
|
||||
actors,
|
||||
tags,
|
||||
movies,
|
||||
relationScenes,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1016,6 +1142,40 @@ async function applySceneMoviesDelta(sceneId, delta, trx) {
|
||||
}
|
||||
}
|
||||
|
||||
// apply the difference between the revision's base and delta, so relations added or removed since are left alone
|
||||
async function applySceneRelationsDelta(revision, delta, trx) {
|
||||
const baseKeys = new Set((revision.base.relations || []).map((relation) => getRelationKey(relation)));
|
||||
const deltaKeys = new Set(delta.value.map((relation) => getRelationKey(relation)));
|
||||
|
||||
const removedKeys = new Set(Array.from(baseKeys).filter((key) => !deltaKeys.has(key)));
|
||||
const addedRelations = delta.value.filter((relation) => !baseKeys.has(getRelationKey(relation)));
|
||||
|
||||
if (removedKeys.size > 0) {
|
||||
const currentRelations = await fetchRevisionRelations(revision.scene_id, trx);
|
||||
const removedIds = currentRelations.filter((relation) => removedKeys.has(getRelationKey(relation))).map((relation) => relation.id);
|
||||
|
||||
if (removedIds.length > 0) {
|
||||
await knexOwner('scenes_relations')
|
||||
.whereIn('id', removedIds)
|
||||
.delete()
|
||||
.transacting(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,
|
||||
})))
|
||||
.onConflict(['source_scene_id', 'target_scene_id', 'type'])
|
||||
.ignore()
|
||||
.transacting(trx);
|
||||
}
|
||||
}
|
||||
|
||||
async function applySceneDeleteDelta(sceneId, _delta, trx, reqUser) {
|
||||
if (!reqUser.abilities.some((ability) => ability.subject === 'scene' && ability.action === 'delete')) {
|
||||
throw new HttpError('You are not privileged to delete scenes', 400);
|
||||
@@ -1068,6 +1228,10 @@ async function applySceneRevision(revisionIds, reqUser) {
|
||||
return applySceneMoviesDelta(revision.scene_id, delta, trx);
|
||||
}
|
||||
|
||||
if (delta.key === 'relations') {
|
||||
return applySceneRelationsDelta(revision, delta, trx);
|
||||
}
|
||||
|
||||
return null;
|
||||
}));
|
||||
|
||||
@@ -1140,6 +1304,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
|
||||
const [
|
||||
[scene],
|
||||
openRevisions,
|
||||
baseRelations,
|
||||
] = await Promise.all([
|
||||
fetchScenesById([sceneId], {
|
||||
reqUser,
|
||||
@@ -1149,6 +1314,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
|
||||
knexOwner('scenes_revisions')
|
||||
.where('user_id', reqUser.id)
|
||||
.whereNull('approved'),
|
||||
fetchRevisionRelations(sceneId),
|
||||
]);
|
||||
|
||||
if (!scene) {
|
||||
@@ -1192,11 +1358,35 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
|
||||
return [key, values];
|
||||
}).filter(Boolean));
|
||||
|
||||
baseScene.relations = baseRelations.map((relation) => ({
|
||||
sceneId: relation.sceneId,
|
||||
type: relation.type,
|
||||
direction: relation.direction,
|
||||
}));
|
||||
|
||||
const curatedRelations = edits.relations
|
||||
? await curateRelationsEdit(scene.id, edits.relations)
|
||||
: null;
|
||||
|
||||
const deltas = Object.entries(edits).map(([key, value]) => {
|
||||
if (key === 'delete') {
|
||||
return { key: 'delete' };
|
||||
}
|
||||
|
||||
if (key === 'relations') {
|
||||
if (!curatedRelations) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const baseKeys = new Set(baseScene.relations.map((relation) => getRelationKey(relation)));
|
||||
|
||||
if (curatedRelations.length === baseKeys.size && curatedRelations.every((relation) => baseKeys.has(getRelationKey(relation)))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { key, value: curatedRelations };
|
||||
}
|
||||
|
||||
if (baseScene[key] === value || typeof value === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user