mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-08 03:02:03 +01:00
Pad: Simplify getChatMessages()
This commit is contained in:
parent
e471cb12e6
commit
23037280a8
1 changed files with 10 additions and 15 deletions
|
@ -295,32 +295,27 @@ Pad.prototype.getChatMessage = async function (entryNum) {
|
||||||
return entry;
|
return entry;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} start - ID of the first desired chat message.
|
||||||
|
* @param {number} end - ID of the last desired chat message.
|
||||||
|
* @returns {object[]} Any existing messages with IDs between `start` (inclusive) and `end`
|
||||||
|
* (inclusive), in order. Note: `start` and `end` form a closed interval, not a half-open
|
||||||
|
* interval as is typical in code.
|
||||||
|
*/
|
||||||
Pad.prototype.getChatMessages = async function (start, end) {
|
Pad.prototype.getChatMessages = async function (start, end) {
|
||||||
// collect the numbers of chat entries and in which order we need them
|
const entries = await Promise.all(
|
||||||
const neededEntries = [];
|
[...Array(end + 1 - start).keys()].map((i) => this.getChatMessage(start + i)));
|
||||||
for (let order = 0, entryNum = start; entryNum <= end; ++order, ++entryNum) {
|
|
||||||
neededEntries.push({entryNum, order});
|
|
||||||
}
|
|
||||||
|
|
||||||
// get all entries out of the database
|
|
||||||
const entries = [];
|
|
||||||
await Promise.all(
|
|
||||||
neededEntries.map((entryObject) => this.getChatMessage(entryObject.entryNum).then((entry) => {
|
|
||||||
entries[entryObject.order] = entry;
|
|
||||||
})));
|
|
||||||
|
|
||||||
// sort out broken chat entries
|
// sort out broken chat entries
|
||||||
// it looks like in happened in the past that the chat head was
|
// it looks like in happened in the past that the chat head was
|
||||||
// incremented, but the chat message wasn't added
|
// incremented, but the chat message wasn't added
|
||||||
const cleanedEntries = entries.filter((entry) => {
|
return entries.filter((entry) => {
|
||||||
const pass = (entry != null);
|
const pass = (entry != null);
|
||||||
if (!pass) {
|
if (!pass) {
|
||||||
console.warn(`WARNING: Found broken chat entry in pad ${this.id}`);
|
console.warn(`WARNING: Found broken chat entry in pad ${this.id}`);
|
||||||
}
|
}
|
||||||
return pass;
|
return pass;
|
||||||
});
|
});
|
||||||
|
|
||||||
return cleanedEntries;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Pad.prototype.init = async function (text) {
|
Pad.prototype.init = async function (text) {
|
||||||
|
|
Loading…
Reference in a new issue