diff --git a/components/edit/revisions.vue b/components/edit/revisions.vue index 96e2e35..aaa1611 100644 --- a/components/edit/revisions.vue +++ b/components/edit/revisions.vue @@ -254,16 +254,7 @@ async function reviewRevision(revision, isApproved) { revisionId: revision.id, }); - // revisions generated by sharing a relation were reviewed along with this one - const childIds = new Set(updatedRevision.revision.childRevisions?.map((child) => child.id) || []); - const updatedChildren = await Promise.all(revisions.value - .filter((rev) => childIds.has(rev.id)) - .map(async (rev) => (await get(`/revisions/${domain}/${rev.id}`, { revisionId: rev.id })).revision)); - - const updatedById = Object.fromEntries([updatedRevision.revision, ...updatedChildren].map((rev) => [rev.id, rev])); - - childIds.forEach((childId) => reviewedRevisions.value.add(childId)); - revisions.value = revisions.value.map((rev) => updatedById[rev.id] || rev); + revisions.value = revisions.value.map((rev) => (rev.id === updatedRevision.revision.id ? updatedRevision.revision : rev)); } catch (error) { reviewedRevisions.value.delete(revision.id); @@ -360,11 +351,6 @@ async function banEditor(revision) { class="nolink" >{{ rev.review.username }} {{ format(rev.review.reviewedAt, 'yyyy-MM-dd hh:mm') }} - Reviewed with #{{ rev.parentRevisionId }} -
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, diff --git a/tools/import-revisions.js b/tools/import-revisions.js index f998665..345f113 100644 --- a/tools/import-revisions.js +++ b/tools/import-revisions.js @@ -229,11 +229,17 @@ async function importRevisions() { comment: entry.comment || argv.comment, }, reqUser); + // copies to related scenes are revisions of their own + const revisions = [revision, ...revision.children.map((child) => ({ ...child, affectedSceneIds: [] }))].filter((stored) => stored.id); + if (argv.apply) { - await reviewSceneRevision(revision.id, true, {}, reqUser, { sync: false }); + await revisions.reduce(async (reviewChain, stored) => { + await reviewChain; + await reviewSceneRevision(stored.id, true, {}, reqUser, { sync: false }); + }, Promise.resolve()); } - return acc.concat(revision); + return acc.concat(revisions); }, Promise.resolve([])); if (created.length === 0) {