Add pagination to message screen.
This commit is contained in:
parent
0b9ed3a68a
commit
cedbd2b3c0
5 changed files with 94 additions and 9 deletions
34
js/db.js
34
js/db.js
|
|
@ -30,6 +30,7 @@ function openDb() {
|
|||
msgStore = db.createObjectStore("messages", { keyPath: "id" });
|
||||
msgStore.createIndex("from", "from", { unique: false });
|
||||
msgStore.createIndex("fingerprint", ["from", "timestamp"], { unique: false });
|
||||
msgStore.createIndex("cidTimestampId", ["chatId", "timestamp", "id"], { unique: false });
|
||||
} else {
|
||||
msgStore = tx.objectStore("messages");
|
||||
}
|
||||
|
|
@ -172,6 +173,39 @@ export async function loadLatestMessages(chatId, limit = 50) {
|
|||
});
|
||||
}
|
||||
|
||||
export async function loadOlderMessages(chatId, oldestTimestamp, oldestId, limit = 50) {
|
||||
const db = await openDb();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const tx = db.transaction("messages", "readonly");
|
||||
const store = tx.objectStore("messages");
|
||||
const idx = store.index("cidTimestampId");
|
||||
|
||||
const out = [];
|
||||
|
||||
const parsedTimestamp = isNaN(oldestTimestamp) ? oldestTimestamp : Number(oldestTimestamp);
|
||||
|
||||
const range = IDBKeyRange.bound(
|
||||
[chatId, -Infinity, ""],
|
||||
[chatId, parsedTimestamp, oldestId],
|
||||
false,
|
||||
false
|
||||
);
|
||||
|
||||
idx.openCursor(range, "prev").onsuccess = (e) => {
|
||||
const cursor = e.target.result;
|
||||
if (!cursor) return resolve(out);
|
||||
|
||||
out.push(cursor.value);
|
||||
|
||||
if (out.length >= limit) resolve(out);
|
||||
else cursor.continue();
|
||||
};
|
||||
|
||||
tx.onerror = () => reject(tx.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function upsertMedia(reqId, blob, filename) {
|
||||
const db = await openDb();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue