2021-11-25 01:43:46 -05:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-29 21:13:49 +01:00
|
|
|
/**
|
2014-12-30 00:12:26 +01:00
|
|
|
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
|
2014-12-29 21:13:49 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-11-27 17:49:25 -05:00
|
|
|
const {Pad} = require('../db/Pad');
|
2021-11-25 01:43:46 -05:00
|
|
|
const authorManager = require('../db/AuthorManager');
|
2020-11-23 13:24:19 -05:00
|
|
|
const db = require('../db/DB');
|
2021-01-21 21:06:52 +00:00
|
|
|
const hooks = require('../../static/js/pluginfw/hooks');
|
2021-03-29 20:06:33 -04:00
|
|
|
const log4js = require('log4js');
|
2021-03-02 17:14:47 +00:00
|
|
|
const supportedElems = require('../../static/js/contentcollector').supportedElems;
|
2014-12-29 21:13:49 +01:00
|
|
|
|
2021-03-29 20:06:33 -04:00
|
|
|
const logger = log4js.getLogger('ImportEtherpad');
|
|
|
|
|
2021-11-23 00:34:59 -05:00
|
|
|
exports.setPadRaw = async (padId, r) => {
|
2021-01-21 21:06:52 +00:00
|
|
|
const records = JSON.parse(r);
|
2014-12-30 00:01:15 +01:00
|
|
|
|
2021-02-21 11:07:13 +00:00
|
|
|
// get supported block Elements from plugins, we will use this later.
|
|
|
|
hooks.callAll('ccRegisterBlockElements').forEach((element) => {
|
2021-03-29 19:40:03 -04:00
|
|
|
supportedElems.add(element);
|
2021-02-21 11:07:13 +00:00
|
|
|
});
|
|
|
|
|
2021-11-25 01:19:00 -05:00
|
|
|
// DB key prefixes for pad records. Each key is expected to have the form `${prefix}:${padId}` or
|
|
|
|
// `${prefix}:${padId}:${otherstuff}`.
|
|
|
|
const padKeyPrefixes = [
|
|
|
|
...await hooks.aCallAll('exportEtherpadAdditionalContent'),
|
|
|
|
'pad',
|
|
|
|
];
|
|
|
|
|
2021-11-25 18:14:38 -05:00
|
|
|
let originalPadId = null;
|
|
|
|
const checkOriginalPadId = (padId) => {
|
|
|
|
if (originalPadId == null) originalPadId = padId;
|
|
|
|
if (originalPadId !== padId) throw new Error('unexpected pad ID in record');
|
|
|
|
};
|
|
|
|
|
2021-11-26 01:35:02 -05:00
|
|
|
// First validate and transform values. Do not commit any records to the database yet in case
|
|
|
|
// there is a problem with the data.
|
|
|
|
|
|
|
|
const dbRecords = new Map();
|
|
|
|
const existingAuthors = new Set();
|
2021-11-23 00:34:59 -05:00
|
|
|
await Promise.all(Object.entries(records).map(async ([key, value]) => {
|
2019-02-08 23:20:57 +01:00
|
|
|
if (!value) {
|
2019-01-31 08:55:36 +00:00
|
|
|
return;
|
2015-02-21 12:33:30 +00:00
|
|
|
}
|
2021-11-25 01:43:46 -05:00
|
|
|
const keyParts = key.split(':');
|
|
|
|
const [prefix, id] = keyParts;
|
|
|
|
if (prefix === 'globalAuthor' && keyParts.length === 2) {
|
2021-11-25 18:14:38 -05:00
|
|
|
// In the database, the padIDs subkey is an object (which is used as a set) that records every
|
|
|
|
// pad the author has worked on. When exported, that object becomes a single string containing
|
|
|
|
// the exported pad's ID.
|
|
|
|
if (typeof value.padIDs !== 'string') {
|
|
|
|
throw new TypeError('globalAuthor padIDs subkey is not a string');
|
|
|
|
}
|
|
|
|
checkOriginalPadId(value.padIDs);
|
2021-11-25 01:43:46 -05:00
|
|
|
if (await authorManager.doesAuthorExist(id)) {
|
2021-11-26 01:35:02 -05:00
|
|
|
existingAuthors.add(id);
|
2021-11-25 01:43:46 -05:00
|
|
|
return;
|
2019-01-31 08:55:36 +00:00
|
|
|
}
|
2021-11-25 01:43:46 -05:00
|
|
|
value.padIDs = {[padId]: 1};
|
2021-11-25 02:05:09 -05:00
|
|
|
} else if (padKeyPrefixes.includes(prefix)) {
|
2021-11-25 18:14:38 -05:00
|
|
|
checkOriginalPadId(id);
|
2021-11-25 01:10:04 -05:00
|
|
|
if (prefix === 'pad' && keyParts.length === 2 && value.pool) {
|
2021-11-25 01:13:31 -05:00
|
|
|
const unsupportedElements = new Set();
|
2021-11-25 18:45:46 -05:00
|
|
|
for (const [k] of Object.values(value.pool.numToAttrib)) {
|
|
|
|
if (!supportedElems.has(k)) unsupportedElements.add(k);
|
2021-02-21 11:07:13 +00:00
|
|
|
}
|
2021-11-25 01:13:31 -05:00
|
|
|
if (unsupportedElements.size) {
|
|
|
|
logger.warn(`(pad ${padId}) unsupported attributes (try installing a plugin): ` +
|
|
|
|
`${[...unsupportedElements].join(', ')}`);
|
|
|
|
}
|
2021-02-21 11:07:13 +00:00
|
|
|
}
|
2021-11-25 02:05:09 -05:00
|
|
|
keyParts[1] = padId;
|
|
|
|
key = keyParts.join(':');
|
|
|
|
} else {
|
|
|
|
logger.warn(`(pad ${padId}) Ignoring record with unsupported key: ${key}`);
|
|
|
|
return;
|
2014-12-30 00:01:15 +01:00
|
|
|
}
|
2021-11-26 01:35:02 -05:00
|
|
|
dbRecords.set(key, value);
|
2021-11-23 00:34:59 -05:00
|
|
|
}));
|
2021-03-29 20:06:33 -04:00
|
|
|
|
2021-11-27 17:49:25 -05:00
|
|
|
const pad = new Pad(padId, {
|
|
|
|
// 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();
|
|
|
|
await pad.check();
|
|
|
|
|
2021-11-26 01:35:02 -05:00
|
|
|
await Promise.all([
|
|
|
|
...[...dbRecords].map(async ([k, v]) => await db.set(k, v)),
|
|
|
|
...[...existingAuthors].map(async (authorId) => await authorManager.addPad(authorId, padId)),
|
|
|
|
]);
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|