etherpad-lite/src/node/utils/ImportEtherpad.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

'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.
*/
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');
const log4js = require('log4js');
const supportedElems = require('../../static/js/contentcollector').supportedElems;
2014-12-29 21:13:49 +01: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
// get supported block Elements from plugins, we will use this later.
hooks.callAll('ccRegisterBlockElements').forEach((element) => {
supportedElems.add(element);
});
const unsupportedElements = new Set();
// 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-23 00:34:59 -05:00
await Promise.all(Object.entries(records).map(async ([key, value]) => {
if (!value) {
return;
}
const keyParts = key.split(':');
const [prefix, id] = keyParts;
if (prefix === 'globalAuthor' && keyParts.length === 2) {
if (await authorManager.doesAuthorExist(id)) {
await authorManager.addPad(id, padId);
return;
}
value.padIDs = {[padId]: 1};
} else if (padKeyPrefixes.includes(prefix)) {
if (prefix === 'pad' && keyParts.length === 2 && value.pool) {
for (const attrib of Object.keys(value.pool.numToAttrib)) {
const attribName = value.pool.numToAttrib[attrib][0];
if (!supportedElems.has(attribName)) unsupportedElements.add(attribName);
}
}
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
}
await db.set(key, value);
2021-11-23 00:34:59 -05:00
}));
if (unsupportedElements.size) {
logger.warn('Ignoring unsupported elements (you might want to install a plugin): ' +
`${[...unsupportedElements].join(', ')}`);
}
2020-11-23 13:24:19 -05:00
};