SHA256
UI: исправить автообновление и хронологию диалогов; обновить деплой-цели
This commit is contained in:
+89
-3
@@ -263,8 +263,69 @@ export const authService = new AuthService(state.entrySettings.shineServer);
|
||||
let onSessionReset = null;
|
||||
let onSessionAuthorized = null;
|
||||
|
||||
function parseMessageTimeFromKey(rawKey) {
|
||||
const value = String(rawKey || '').trim();
|
||||
if (!value) return 0;
|
||||
const parts = value.split('|');
|
||||
if (parts.length < 4) return 0;
|
||||
const timeMs = Number(parts[2] || 0);
|
||||
if (!Number.isFinite(timeMs) || timeMs <= 0) return 0;
|
||||
return Math.trunc(timeMs);
|
||||
}
|
||||
|
||||
function resolveChatMessageTimeMs(row) {
|
||||
const fromBaseKey = parseMessageTimeFromKey(row?.baseKey);
|
||||
if (fromBaseKey > 0) return fromBaseKey;
|
||||
|
||||
const fromMessageKey = parseMessageTimeFromKey(row?.messageKey);
|
||||
if (fromMessageKey > 0) return fromMessageKey;
|
||||
|
||||
const fromCreatedAt = Number(row?.createdAtMs || row?.ts || 0);
|
||||
if (Number.isFinite(fromCreatedAt) && fromCreatedAt > 0) return Math.trunc(fromCreatedAt);
|
||||
|
||||
const tempId = String(row?.tempId || '').trim();
|
||||
if (tempId.startsWith('tmp-')) {
|
||||
const parts = tempId.split('-');
|
||||
const ts = Number(parts[1] || 0);
|
||||
if (Number.isFinite(ts) && ts > 0) return Math.trunc(ts);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function stableMessageOrderKey(row) {
|
||||
const key = String(row?.messageKey || '').trim();
|
||||
if (key) return `mk:${key}`;
|
||||
const tmp = String(row?.tempId || '').trim();
|
||||
if (tmp) return `tmp:${tmp}`;
|
||||
const base = String(row?.baseKey || '').trim();
|
||||
if (base) return `bk:${base}`;
|
||||
const text = String(row?.text || '').trim();
|
||||
return `txt:${text}`;
|
||||
}
|
||||
|
||||
function sortChatMessagesInPlace(chatId) {
|
||||
const list = getChatMessages(chatId);
|
||||
list.sort((a, b) => {
|
||||
const ta = resolveChatMessageTimeMs(a);
|
||||
const tb = resolveChatMessageTimeMs(b);
|
||||
if (ta !== tb) return ta - tb;
|
||||
|
||||
const ka = stableMessageOrderKey(a);
|
||||
const kb = stableMessageOrderKey(b);
|
||||
const byKey = ka.localeCompare(kb, 'ru');
|
||||
if (byKey !== 0) return byKey;
|
||||
|
||||
if ((a?.from || '') !== (b?.from || '')) {
|
||||
return (a?.from === 'in') ? -1 : 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
function persistMessageRecord(chatId, row) {
|
||||
if (!chatId || !row?.messageKey) return;
|
||||
const resolvedTs = resolveChatMessageTimeMs(row);
|
||||
void putStoredMessage({
|
||||
messageKey: row.messageKey,
|
||||
chatId,
|
||||
@@ -278,13 +339,14 @@ function persistMessageRecord(chatId, row) {
|
||||
secondTick: Boolean(row.secondTick),
|
||||
readReceiptSent: Boolean(row.readReceiptSent),
|
||||
refBaseKey: String(row.refBaseKey || ''),
|
||||
ts: Date.now(),
|
||||
ts: resolvedTs > 0 ? resolvedTs : Date.now(),
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
export async function hydrateMessagesFromStore() {
|
||||
try {
|
||||
const rows = await listStoredMessages();
|
||||
const touchedChats = new Set();
|
||||
rows
|
||||
.sort((a, b) => Number(a?.ts || 0) - Number(b?.ts || 0))
|
||||
.forEach((row) => {
|
||||
@@ -293,6 +355,7 @@ export async function hydrateMessagesFromStore() {
|
||||
if (!chatId || !messageKey) return;
|
||||
if (state.knownMessageKeys[messageKey]) return;
|
||||
state.knownMessageKeys[messageKey] = true;
|
||||
touchedChats.add(chatId);
|
||||
getChatMessages(chatId).push({
|
||||
from: row.from === 'out' ? 'out' : 'in',
|
||||
text: String(row.text || ''),
|
||||
@@ -305,8 +368,10 @@ export async function hydrateMessagesFromStore() {
|
||||
secondTick: Boolean(row.secondTick),
|
||||
readReceiptSent: Boolean(row.readReceiptSent),
|
||||
refBaseKey: String(row.refBaseKey || ''),
|
||||
createdAtMs: Number(row.ts || 0),
|
||||
});
|
||||
});
|
||||
touchedChats.forEach((chatId) => sortChatMessagesInPlace(chatId));
|
||||
} catch {
|
||||
// ignore broken storage
|
||||
}
|
||||
@@ -322,7 +387,15 @@ export function getChatMessages(chatId) {
|
||||
export function addChatMessage(chatId, text) {
|
||||
const message = text.trim();
|
||||
if (!message) return;
|
||||
getChatMessages(chatId).push({ from: 'out', text: message, firstTick: false, secondTick: false, unread: false });
|
||||
getChatMessages(chatId).push({
|
||||
from: 'out',
|
||||
text: message,
|
||||
firstTick: false,
|
||||
secondTick: false,
|
||||
unread: false,
|
||||
createdAtMs: Date.now(),
|
||||
});
|
||||
sortChatMessagesInPlace(chatId);
|
||||
}
|
||||
|
||||
export function addSystemChatMessage(chatId, text, { from = 'out', kind = 'system' } = {}) {
|
||||
@@ -335,7 +408,9 @@ export function addSystemChatMessage(chatId, text, { from = 'out', kind = 'syste
|
||||
unread: from === 'in',
|
||||
firstTick: from !== 'in',
|
||||
secondTick: false,
|
||||
createdAtMs: Date.now(),
|
||||
});
|
||||
sortChatMessagesInPlace(chatId);
|
||||
}
|
||||
|
||||
|
||||
@@ -344,7 +419,14 @@ export function addIncomingMessage(chatId, text, messageId = '') {
|
||||
if (!msg) return false;
|
||||
if (messageId && state.incomingDedup[messageId]) return false;
|
||||
if (messageId) state.incomingDedup[messageId] = true;
|
||||
getChatMessages(chatId).push({ from: 'in', text: msg, messageId, unread: true });
|
||||
getChatMessages(chatId).push({
|
||||
from: 'in',
|
||||
text: msg,
|
||||
messageId,
|
||||
unread: true,
|
||||
createdAtMs: Date.now(),
|
||||
});
|
||||
sortChatMessagesInPlace(chatId);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -359,7 +441,9 @@ export function addOutgoingPendingMessage(chatId, text) {
|
||||
firstTick: false,
|
||||
secondTick: false,
|
||||
unread: false,
|
||||
createdAtMs: Date.now(),
|
||||
});
|
||||
sortChatMessagesInPlace(chatId);
|
||||
return tempId;
|
||||
}
|
||||
|
||||
@@ -377,6 +461,7 @@ export function markOutgoingSent(tempId, { messageKey = '', baseKey = '' } = {})
|
||||
state.knownMessageKeys[messageKey] = true;
|
||||
persistMessageRecord(chatId, row);
|
||||
}
|
||||
sortChatMessagesInPlace(chatId);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -455,6 +540,7 @@ export function addSignedMessageToChat({
|
||||
secondTick: false,
|
||||
};
|
||||
getChatMessages(chatId).push(row);
|
||||
sortChatMessagesInPlace(chatId);
|
||||
persistMessageRecord(chatId, row);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user