Improve direct message history and SQLite resilience

This commit is contained in:
AidarKC
2026-07-22 13:09:42 +04:00
parent 2f3b1571e5
commit 93673e5786
18 changed files with 908 additions and 233 deletions
+23 -2
View File
@@ -420,6 +420,7 @@ function persistMessageRecord(chatId, row) {
unread: Boolean(row.unread),
firstTick: Boolean(row.firstTick),
secondTick: Boolean(row.secondTick),
readAtMs: Number(row.readAtMs || 0),
readReceiptSent: Boolean(row.readReceiptSent),
refBaseKey: String(row.refBaseKey || ''),
ts: resolvedTs > 0 ? resolvedTs : Date.now(),
@@ -455,6 +456,7 @@ export async function hydrateMessagesFromStore() {
unread: Boolean(row.unread),
firstTick: Boolean(row.firstTick),
secondTick: Boolean(row.secondTick),
readAtMs: Number(row.readAtMs || 0),
readReceiptSent: Boolean(row.readReceiptSent),
refBaseKey: String(row.refBaseKey || ''),
createdAtMs: Number(row.ts || 0),
@@ -556,8 +558,9 @@ export function markOutgoingSent(tempId, { messageKey = '', baseKey = '' } = {})
});
}
export function markOutgoingReadByBaseKey(baseKey) {
export function markOutgoingReadByBaseKey(baseKey, readAtMs = 0) {
if (!baseKey) return;
const normalizedReadAtMs = Number(readAtMs || 0);
const keys = Object.keys(state.chats || {});
let matched = false;
keys.forEach((chatId) => {
@@ -567,6 +570,9 @@ export function markOutgoingReadByBaseKey(baseKey) {
if (row.baseKey === baseKey) {
matched = true;
row.secondTick = true;
if (Number.isFinite(normalizedReadAtMs) && normalizedReadAtMs > 0) {
row.readAtMs = normalizedReadAtMs;
}
persistMessageRecord(chatId, row);
}
});
@@ -574,7 +580,9 @@ export function markOutgoingReadByBaseKey(baseKey) {
if (matched) {
delete state.pendingOutgoingReadByBaseKey[baseKey];
} else {
state.pendingOutgoingReadByBaseKey[baseKey] = true;
state.pendingOutgoingReadByBaseKey[baseKey] = (Number.isFinite(normalizedReadAtMs) && normalizedReadAtMs > 0)
? normalizedReadAtMs
: true;
}
return matched;
}
@@ -626,6 +634,7 @@ export function addSignedMessageToChat({
text = '',
messageType = 1,
unread = false,
readAtMs = 0,
rawBlobB64 = '',
refBaseKey = '',
revisionTimeMs = 0,
@@ -667,9 +676,14 @@ export function addSignedMessageToChat({
row.refBaseKey = String(refBaseKey || '');
row.firstTick = row.from === 'out';
row.secondTick = Boolean(existing?.secondTick);
row.readAtMs = Number(existing?.readAtMs || 0);
row.readReceiptSent = Boolean(existing?.readReceiptSent);
if (row.baseKey && row.from === 'out' && state.pendingOutgoingReadByBaseKey[row.baseKey]) {
row.secondTick = true;
const pendingReadAtMs = Number(state.pendingOutgoingReadByBaseKey[row.baseKey] || 0);
if (Number.isFinite(pendingReadAtMs) && pendingReadAtMs > 0) {
row.readAtMs = pendingReadAtMs;
}
delete state.pendingOutgoingReadByBaseKey[row.baseKey];
}
if (row.baseKey && row.from === 'in' && state.pendingIncomingReadByBaseKey[row.baseKey]) {
@@ -681,6 +695,13 @@ export function addSignedMessageToChat({
if (existingIndex < 0) {
list.push(row);
}
const nextReadAtMs = Number(readAtMs || 0);
if (Number.isFinite(nextReadAtMs) && nextReadAtMs > 0) {
row.readAtMs = nextReadAtMs;
if (row.from === 'out') {
row.secondTick = true;
}
}
sortChatMessagesInPlace(normalizedChatId);
persistMessageRecord(normalizedChatId, row);
return true;