Unlinked relation and copy revisions.

This commit is contained in:
2026-11-26 01:27:25 +01:00
parent 207fb21cf3
commit 8b89bd5c30
4 changed files with 34 additions and 125 deletions
-1
View File
@@ -3,7 +3,6 @@ export function curateRevision(revision) {
id: revision.id,
sceneId: revision.scene_id,
affectedSceneIds: revision.affected_scene_ids || [], // actor revisions have none
parentRevisionId: revision.parent_revision_id || null, // generated by sharing a relation, reviewed with the parent
actorId: revision.actor_id,
base: revision.base,
deltas: revision.deltas,
+25 -75
View File
@@ -1353,20 +1353,7 @@ export async function fetchSceneRevisions(revisionId, filters = {}, reqUser) {
: [],
]);
// revisions generated for related scenes by sharing a relation, reviewed and applied with their parent
const childRevisions = revisions.length > 0
? await knexOwner('scenes_revisions')
.select('id', 'scene_id', 'parent_revision_id')
.whereIn('parent_revision_id', revisions.map((revision) => revision.id))
.orderBy('id')
: [];
const curatedRevisions = revisions.map((revision) => ({
...curateRevision(revision),
childRevisions: childRevisions
.filter((child) => child.parent_revision_id === revision.id)
.map((child) => ({ id: child.id, sceneId: child.scene_id })),
}));
const curatedRevisions = revisions.map((revision) => curateRevision(revision));
return {
revisions: curatedRevisions,
@@ -1620,25 +1607,11 @@ async function applySceneRevision(revisionIds, reqUser, { sync = true } = {}) {
.whereIn('id', revisionIds)
.whereNull('applied_at'); // should not re-apply revision that was already applied
// revisions generated by sharing a relation are applied together with their parent
const children = await knexOwner('scenes_revisions')
.whereIn('parent_revision_id', revisions.map((revision) => revision.id))
.whereNotIn('id', revisions.map((revision) => revision.id))
.whereNull('applied_at')
.orderBy('id');
await revisions.reduce(async (chain, revision) => {
await chain;
await knexOwner.transaction(async (trx) => {
await applySceneRevisionDeltas(revision, trx, reqUser);
await children
.filter((child) => child.parent_revision_id === revision.id)
.reduce(async (childChain, child) => {
await childChain;
await applySceneRevisionDeltas(child, trx, reqUser);
}, Promise.resolve());
}).catch(async (error) => {
logger.error(`Failed to apply revision ${revision.id} on scene ${revision.scene_id}: ${error.message}`);
throw error;
@@ -1646,7 +1619,7 @@ async function applySceneRevision(revisionIds, reqUser, { sync = true } = {}) {
}, Promise.resolve());
if (sync) {
const sceneIds = Array.from(new Set([...revisions, ...children].map((revision) => revision.scene_id)));
const sceneIds = Array.from(new Set(revisions.map((revision) => revision.scene_id)));
await syncScenes(sceneIds);
}
@@ -1661,15 +1634,6 @@ export async function reviewSceneRevision(revisionId, isApproved, { feedback },
throw new HttpError('You must either approve or reject the revision', 400);
}
const revision = await knexOwner('scenes_revisions')
.select('id', 'parent_revision_id')
.where('id', revisionId)
.first();
if (revision?.parent_revision_id) {
throw new HttpError(`This revision was generated by sharing a relation, review revision #${revision.parent_revision_id} instead`, 400);
}
const updated = await knexOwner('scenes_revisions')
.where('id', revisionId)
.whereNull('approved') // don't rerun reviewed revision, must be forked into new revision instead
@@ -1685,16 +1649,6 @@ export async function reviewSceneRevision(revisionId, isApproved, { feedback },
throw new HttpError('This revision was already reviewed', 409);
}
// revisions generated by sharing a relation share their parent's review
await knexOwner('scenes_revisions')
.where('parent_revision_id', revisionId)
.whereNull('approved')
.update({
approved: isApproved,
reviewed_at: knex.fn.now(),
reviewed_by: reqUser.id,
});
if (isApproved) {
try {
await applySceneRevision([revisionId], reqUser, options);
@@ -1702,7 +1656,6 @@ export async function reviewSceneRevision(revisionId, isApproved, { feedback },
catch (error) {
await knexOwner('scenes_revisions')
.where('id', revisionId)
.orWhere('parent_revision_id', revisionId)
.update({
approved: null,
reviewed_at: null,
@@ -1715,7 +1668,7 @@ export async function reviewSceneRevision(revisionId, isApproved, { feedback },
}
// dryRun validates the edits and returns the deltas without storing a revision
// isChild is used internally for revisions generated by sharing a relation, they return their entry instead of storing it
// isChild is used internally for the copies to related scenes when sharing a relation, they return their entry instead of storing it
export async function createSceneRevision(sceneId, { edits, comment, apply }, reqUser, { dryRun = false, isChild = false } = {}) {
if (!reqUser) {
throw new HttpError('Must be authenticated to create scene revision', 401);
@@ -1735,8 +1688,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
}),
knexOwner('scenes_revisions')
.where('user_id', reqUser.id)
.whereNull('approved')
.whereNull('parent_revision_id'), // revisions generated by sharing are reviewed with their parent
.whereNull('approved'),
fetchRevisionRelations(sceneId),
]);
@@ -1744,6 +1696,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
throw new HttpError(`No scene with ID ${sceneId} found to update`, 404);
}
// checked per submission, the copies to related scenes count towards the limit but don't need room of their own, so a submission can go over the limit
if (!isChild && openRevisions.length >= config.revisions.unapprovedLimit && reqUser.role !== 'admin') {
throw new HttpError(`You have ${config.revisions.unapprovedLimit} unapproved revisions, please wait for approval before submitting another revision.`, 429);
}
@@ -1865,10 +1818,7 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
throw new HttpError('No effective changes provided', 400);
}
const affectedSceneIds = Array.from(new Set([
...getAffectedSceneIds(baseScene, deltas),
...children.map((child) => child.sceneId),
]));
const affectedSceneIds = getAffectedSceneIds(baseScene, deltas);
const entry = {
user_id: reqUser.id,
@@ -1902,34 +1852,34 @@ export async function createSceneRevision(sceneId, { edits, comment, apply }, re
};
}
const revisionEntry = await knexOwner.transaction(async (trx) => {
const [parentEntry] = await trx('scenes_revisions')
.insert(entry)
.returning('id');
// the copies to related scenes are separate revisions, reviewed on their own, stored together so a submission is never left half done
// a submission that only copies, i.e. re-adding an existing relation, has no revision of its own
const entries = (deltas.length > 0 ? [entry] : []).concat(children.map((child) => child.entry));
if (children.length > 0) {
const childEntries = await trx('scenes_revisions')
.insert(children.map((child) => ({
...child.entry,
parent_revision_id: parentEntry.id,
})))
.returning(['id', 'scene_id']);
const storedEntries = await knexOwner('scenes_revisions')
.insert(entries)
.returning('id');
children.forEach((child, index) => {
curatedChildren[index].id = childEntries[index].id;
});
}
const ownId = deltas.length > 0 ? storedEntries[0].id : null;
const childIds = storedEntries.slice(deltas.length > 0 ? 1 : 0).map((storedEntry) => storedEntry.id);
return parentEntry;
children.forEach((child, index) => {
curatedChildren[index].id = childIds[index];
});
const revisionIds = storedEntries.map((storedEntry) => storedEntry.id);
if (['admin', 'editor'].includes(reqUser.role) && apply) {
// don't keep the editor waiting for the revision to apply
reviewSceneRevision(revisionEntry.id, true, {}, reqUser).catch(() => {});
// one after the other, don't keep the editor waiting for the revisions to apply
revisionIds.reduce(async (chain, revisionId) => {
await chain;
await reviewSceneRevision(revisionId, true, {}, reqUser).catch(() => {});
}, Promise.resolve());
}
return {
id: revisionEntry.id,
id: ownId,
revisionIds,
sceneId: scene.id,
deltas,
affectedSceneIds,