Pad: Simplify `Pad.copy()` logic

pull/5106/head
Richard Hansen 2021-09-17 00:30:25 -04:00
parent 0040f5984e
commit 7572040836
1 changed files with 11 additions and 31 deletions

View File

@ -376,38 +376,18 @@ Pad.prototype.copy = async function (destinationID, force) {
// if force is true and already exists a Pad with the same id, remove that Pad // if force is true and already exists a Pad with the same id, remove that Pad
await this.removePadIfForceIsTrueAndAlreadyExist(destinationID, force); await this.removePadIfForceIsTrueAndAlreadyExist(destinationID, force);
// copy all records in parallel const copyRecord = async (keySuffix) => {
const promises = [ const val = await this._db.get(`pad:${this.id}${keySuffix}`);
// Copy the 'pad' entry. This is wrapped in an IIFE so that this._db.get() can run in parallel await db.set(`pad:${destinationID}${keySuffix}`, val);
// with the other record copies done below. };
(async () => await db.set(`pad:${destinationID}`, await this._db.get(`pad:${this.id}`)))(),
];
// copy all chat messages await Promise.all((function* () {
const chatHead = this.chatHead; yield copyRecord('');
for (let i = 0; i <= chatHead; ++i) { for (let i = 0; i <= this.head; ++i) yield copyRecord(`:revs:${i}`);
const p = this._db.get(`pad:${this.id}:chat:${i}`) for (let i = 0; i <= this.chatHead; ++i) yield copyRecord(`:chat:${i}`);
.then((chat) => db.set(`pad:${destinationID}:chat:${i}`, chat)); yield this.copyAuthorInfoToDestinationPad(destinationID);
promises.push(p); if (destGroupID) yield db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
} }).call(this));
// copy all revisions
const revHead = this.head;
for (let i = 0; i <= revHead; ++i) {
const p = this._db.get(`pad:${this.id}:revs:${i}`)
.then((rev) => db.set(`pad:${destinationID}:revs:${i}`, rev));
promises.push(p);
}
promises.push(this.copyAuthorInfoToDestinationPad(destinationID));
// wait for the above to complete
await Promise.all(promises);
// Group pad? Add it to the group's list
if (destGroupID) {
await db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
}
// Initialize the new pad (will update the listAllPads cache) // Initialize the new pad (will update the listAllPads cache)
await padManager.getPad(destinationID, null); await padManager.getPad(destinationID, null);