Added relation comment. Changed relation dialog order.

This commit is contained in:
2026-11-21 04:23:09 +01:00
parent 206fe91e27
commit 6176007b06
8 changed files with 164 additions and 35 deletions
+55 -18
View File
@@ -451,7 +451,7 @@ function getRelatedScene(relation, sceneId) {
function queryRelations(queryKnex, sceneId) {
return queryKnex('scenes_relations')
.select('scenes_relations.id', 'source_scene_id', 'target_scene_id', 'scenes_relations.type', 'relations_types.symmetric')
.select('scenes_relations.id', 'source_scene_id', 'target_scene_id', 'scenes_relations.type', 'scenes_relations.comment', 'relations_types.symmetric')
.leftJoin('relations_types', 'relations_types.type', 'scenes_relations.type')
.where((builder) => {
builder
@@ -486,6 +486,7 @@ export async function fetchSceneRelations(sceneId, { reqUser, ...context } = {})
id: relation.id,
type: relation.type,
direction,
comment: relation.comment,
scene: relatedScene,
}];
}).filter(Boolean).reverse())).reverse(); // keep the oldest of any duplicates
@@ -497,12 +498,35 @@ export async function fetchRelationTypes() {
.orderBy('type');
}
// relations in revisions are stored relative to the revised scene: { sceneId, type, direction }
// relations in revisions are stored relative to the revised scene: { sceneId, type, direction, comment }
// sceneId is the related scene, direction is the side of the relation the related scene is on, or 'both' for symmetric types
// the comment is not part of the key, changing it changes the existing relation
function getRelationKey(relation) {
return `${relation.direction}:${relation.type}:${relation.sceneId}`;
}
const relationCommentLimit = 500;
// revisions from before relation comments existed have none
function getRelationComment(relation) {
return relation.comment || null;
}
function diffRelations(baseRelations, deltaRelations) {
const baseByKey = Object.fromEntries(baseRelations.map((relation) => [getRelationKey(relation), relation]));
const deltaKeys = new Set(deltaRelations.map((relation) => getRelationKey(relation)));
return {
added: deltaRelations.filter((relation) => !baseByKey[getRelationKey(relation)]),
removed: baseRelations.filter((relation) => !deltaKeys.has(getRelationKey(relation))),
changed: deltaRelations.filter((relation) => {
const baseRelation = baseByKey[getRelationKey(relation)];
return baseRelation && getRelationComment(baseRelation) !== getRelationComment(relation);
}),
};
}
async function fetchRevisionRelations(sceneId, trx) {
const relations = await queryRelations(knexOwner, sceneId)
.modify((builder) => {
@@ -514,6 +538,7 @@ async function fetchRevisionRelations(sceneId, trx) {
return relations.map((relation) => ({
id: relation.id,
type: relation.type,
comment: relation.comment,
...getRelatedScene(relation, sceneId),
}));
}
@@ -537,11 +562,18 @@ async function curateRelationsEdit(sceneId, relations) {
sceneId: Number(relation.sceneId),
type: relation.type,
direction: typesByName[relation.type].symmetric ? 'both' : relation.direction,
comment: typeof relation.comment === 'string' && relation.comment.trim().length > 0
? relation.comment.trim()
: null,
};
return [getRelationKey(curatedRelation), curatedRelation];
})));
if (curatedRelations.some((relation) => relation.comment?.length > relationCommentLimit)) {
throw new HttpError(`Relation comments may be at most ${relationCommentLimit} characters`, 400);
}
if (curatedRelations.some((relation) => !Number.isInteger(relation.sceneId) || relation.sceneId === sceneId)) {
throw new HttpError('Relations must refer to a different scene', 400);
}
@@ -1158,7 +1190,7 @@ async function applySceneMoviesDelta(sceneId, delta, trx) {
}
}
// other scenes a revision changes, only the scenes whose relation is added or removed, not those it leaves as is
// other scenes a revision changes, only the scenes whose relation is added, removed or re-commented, not those it leaves as is
function getAffectedSceneIds(base, deltas) {
const relationsDelta = deltas.find((delta) => delta.key === 'relations');
@@ -1166,26 +1198,18 @@ function getAffectedSceneIds(base, deltas) {
return [];
}
const baseRelations = base.relations || [];
const baseKeys = new Set(baseRelations.map((relation) => getRelationKey(relation)));
const deltaKeys = new Set(relationsDelta.value.map((relation) => getRelationKey(relation)));
const { added, removed, changed } = diffRelations(base.relations || [], relationsDelta.value);
return Array.from(new Set([
...baseRelations.filter((relation) => !deltaKeys.has(getRelationKey(relation))),
...relationsDelta.value.filter((relation) => !baseKeys.has(getRelationKey(relation))),
].map((relation) => relation.sceneId)));
return Array.from(new Set([...added, ...removed, ...changed].map((relation) => relation.sceneId)));
}
// 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 { added: addedRelations, removed, changed } = diffRelations(revision.base.relations || [], delta.value);
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) {
if (removed.length > 0 || changed.length > 0) {
const currentRelations = await fetchRevisionRelations(revision.scene_id, trx);
const removedKeys = new Set(removed.map((relation) => getRelationKey(relation)));
const removedIds = currentRelations.filter((relation) => removedKeys.has(getRelationKey(relation))).map((relation) => relation.id);
if (removedIds.length > 0) {
@@ -1194,6 +1218,17 @@ async function applySceneRelationsDelta(revision, delta, trx) {
.delete()
.transacting(trx);
}
await Promise.all(changed.map(async (relation) => {
const ids = currentRelations.filter((currentRelation) => getRelationKey(currentRelation) === getRelationKey(relation)).map((currentRelation) => currentRelation.id);
if (ids.length > 0) {
await knexOwner('scenes_relations')
.whereIn('id', ids)
.update({ comment: getRelationComment(relation) })
.transacting(trx);
}
}));
}
if (addedRelations.length > 0) {
@@ -1208,6 +1243,7 @@ async function applySceneRelationsDelta(revision, delta, trx) {
source_scene_id: isSource ? revision.scene_id : relation.sceneId,
target_scene_id: isSource ? relation.sceneId : revision.scene_id,
type: relation.type,
comment: getRelationComment(relation),
created_by: revision.user_id,
};
}))
@@ -1404,6 +1440,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
sceneId: relation.sceneId,
type: relation.type,
direction: relation.direction,
comment: relation.comment,
}])));
const curatedRelations = edits.relations
@@ -1420,9 +1457,9 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
return null;
}
const baseKeys = new Set(baseScene.relations.map((relation) => getRelationKey(relation)));
const { added, removed, changed } = diffRelations(baseScene.relations, curatedRelations);
if (curatedRelations.length === baseKeys.size && curatedRelations.every((relation) => baseKeys.has(getRelationKey(relation)))) {
if (added.length === 0 && removed.length === 0 && changed.length === 0) {
return null;
}