ImportEtherpad: Use a real ueberdb object for the temp Pad

Now plugin authors have access to the full set of DB operations.
pull/5513/head
Richard Hansen 2022-04-19 16:23:56 -04:00
parent 6a183db850
commit 10117bc988
1 changed files with 48 additions and 54 deletions

View File

@ -24,6 +24,7 @@ const db = require('../db/DB');
const hooks = require('../../static/js/pluginfw/hooks'); const hooks = require('../../static/js/pluginfw/hooks');
const log4js = require('log4js'); const log4js = require('log4js');
const supportedElems = require('../../static/js/contentcollector').supportedElems; const supportedElems = require('../../static/js/contentcollector').supportedElems;
const ueberdb = require('ueberdb2');
const logger = log4js.getLogger('ImportEtherpad'); const logger = log4js.getLogger('ImportEtherpad');
@ -55,18 +56,19 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
// First validate and transform values. Do not commit any records to the database yet in case // First validate and transform values. Do not commit any records to the database yet in case
// there is a problem with the data. // there is a problem with the data.
const dbRecords = new Map(); const data = new Map();
const existingAuthors = new Set(); const existingAuthors = new Set();
const padDb = new ueberdb.Database('memory', {data});
await padDb.init();
try {
await Promise.all(Object.entries(records).map(([key, value]) => q.pushAsync(async () => { await Promise.all(Object.entries(records).map(([key, value]) => q.pushAsync(async () => {
if (!value) { if (!value) return;
return;
}
const keyParts = key.split(':'); const keyParts = key.split(':');
const [prefix, id] = keyParts; const [prefix, id] = keyParts;
if (prefix === 'globalAuthor' && keyParts.length === 2) { if (prefix === 'globalAuthor' && keyParts.length === 2) {
// In the database, the padIDs subkey is an object (which is used as a set) that records every // In the database, the padIDs subkey is an object (which is used as a set) that records
// pad the author has worked on. When exported, that object becomes a single string containing // every pad the author has worked on. When exported, that object becomes a single string
// the exported pad's ID. // containing the exported pad's ID.
if (typeof value.padIDs !== 'string') { if (typeof value.padIDs !== 'string') {
throw new TypeError('globalAuthor padIDs subkey is not a string'); throw new TypeError('globalAuthor padIDs subkey is not a string');
} }
@ -95,26 +97,18 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
logger.warn(`(pad ${padId}) Ignoring record with unsupported key: ${key}`); logger.warn(`(pad ${padId}) Ignoring record with unsupported key: ${key}`);
return; return;
} }
dbRecords.set(key, value); await padDb.set(key, value);
}))); })));
const pad = new Pad(padId, { const pad = new Pad(padId, padDb);
// Only fetchers are needed to check the pad's integrity.
get: async (k) => dbRecords.get(k),
getSub: async (k, sub) => {
let v = dbRecords.get(k);
for (const sk of sub) {
if (v == null) return null;
v = v[sk];
}
return v;
},
});
await pad.init(null, authorId); await pad.init(null, authorId);
await pad.check(); await pad.check();
} finally {
await padDb.close();
}
await Promise.all([ await Promise.all([
...[...dbRecords].map(([k, v]) => q.pushAsync(() => db.set(k, v))), ...[...data].map(([k, v]) => q.pushAsync(() => db.set(k, v))),
...[...existingAuthors].map((a) => q.pushAsync(() => authorManager.addPad(a, padId))), ...[...existingAuthors].map((a) => q.pushAsync(() => authorManager.addPad(a, padId))),
]); ]);
}; };