From 2cfa5a7fab960952c9cbbd05e7ae0e00a4b21f17 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 9 Jul 2026 10:16:58 -0500 Subject: [PATCH] fix recent emojis and sliding sync display --- .changeset/fix-recent-emoji-format.md | 5 ++ .changeset/fix-sliding-sync-debug-display.md | 5 ++ src/app/plugins/recent-emoji.ts | 77 ++++++++++++++------ src/client/initMatrix.ts | 7 +- 4 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 .changeset/fix-recent-emoji-format.md create mode 100644 .changeset/fix-sliding-sync-debug-display.md diff --git a/.changeset/fix-recent-emoji-format.md b/.changeset/fix-recent-emoji-format.md new file mode 100644 index 000000000..f007a65bf --- /dev/null +++ b/.changeset/fix-recent-emoji-format.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix format for recent emojis and related crash. diff --git a/.changeset/fix-sliding-sync-debug-display.md b/.changeset/fix-sliding-sync-debug-display.md new file mode 100644 index 000000000..db67b62e0 --- /dev/null +++ b/.changeset/fix-sliding-sync-debug-display.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix sliding sync debug saying sliding when it is in fact not sliding. diff --git a/src/app/plugins/recent-emoji.ts b/src/app/plugins/recent-emoji.ts index 84653a90a..580e02ef5 100644 --- a/src/app/plugins/recent-emoji.ts +++ b/src/app/plugins/recent-emoji.ts @@ -8,10 +8,40 @@ import { CustomAccountDataEvent } from '$types/matrix/accountData'; type EmojiUnicode = string; type EmojiUsageCount = number; +type RecentEmojiEntry = + | { emoji: EmojiUnicode; total: EmojiUsageCount } + | [EmojiUnicode, EmojiUsageCount]; + export type IRecentEmojiContent = { - recent_emoji?: [EmojiUnicode, EmojiUsageCount][]; + recent_emoji?: { emoji: EmojiUnicode; total: EmojiUsageCount }[]; }; +const isTuple = (entry: RecentEmojiEntry): entry is [EmojiUnicode, EmojiUsageCount] => + Array.isArray(entry) && + entry.length === 2 && + typeof entry[0] === 'string' && + typeof entry[1] === 'number'; + +const isValidEntry = ( + entry: RecentEmojiEntry +): entry is { emoji: EmojiUnicode; total: EmojiUsageCount } => + typeof entry === 'object' && + entry !== null && + !Array.isArray(entry) && + typeof (entry as { emoji?: unknown }).emoji === 'string' && + typeof (entry as { total?: unknown }).total === 'number'; + +const normalizeEntries = ( + raw: RecentEmojiEntry[] +): { emoji: EmojiUnicode; total: EmojiUsageCount }[] => + raw + .map((entry) => { + if (isTuple(entry)) return { emoji: entry[0], total: entry[1] }; + if (isValidEntry(entry)) return { emoji: entry.emoji, total: entry.total }; + return null; + }) + .filter((e): e is { emoji: EmojiUnicode; total: EmojiUsageCount } => e !== null); + export const getRecentEmojis = (mx: MatrixClient, limit?: number): IEmoji[] => { let recentEmojiEvent = getAccountData(mx, CustomAccountDataEvent.RecentEmoji); let isLegacy = false; @@ -19,20 +49,23 @@ export const getRecentEmojis = (mx: MatrixClient, limit?: number): IEmoji[] => { recentEmojiEvent = getAccountData(mx, CustomAccountDataEvent.LegacyElementRecentEmoji); isLegacy = true; } - const recentEmoji = recentEmojiEvent?.getContent().recent_emoji; + const raw = recentEmojiEvent?.getContent().recent_emoji; - if (isLegacy && Array.isArray(recentEmoji)) { + if (!Array.isArray(raw)) return []; + + const hasTuples = raw.some((e) => isTuple(e as RecentEmojiEntry)); + const entries = normalizeEntries(raw as RecentEmojiEntry[]); + + if ((isLegacy || hasTuples) && entries.length > 0) { mx.setAccountData(CustomAccountDataEvent.RecentEmoji, { - recent_emoji: recentEmoji, + recent_emoji: entries, }).catch(() => {}); } - if (!Array.isArray(recentEmoji)) return []; - - return recentEmoji - .toSorted((e1, e2) => e2[1] - e1[1]) + return entries + .toSorted((a, b) => b.total - a.total) .slice(0, limit) - .reduce((list, [unicode]) => { + .reduce((list, { emoji: unicode }) => { const emoji = emojis.find((e) => e.unicode === unicode); if (emoji) list.push(emoji); return list; @@ -44,22 +77,22 @@ export function addRecentEmoji(mx: MatrixClient, unicode: string) { if (!recentEmojiEvent) { recentEmojiEvent = getAccountData(mx, CustomAccountDataEvent.LegacyElementRecentEmoji); } - const recentEmojiContent = recentEmojiEvent?.getContent(); - const recentEmoji = - recentEmojiContent && Array.isArray(recentEmojiContent.recent_emoji) - ? structuredClone(recentEmojiContent.recent_emoji) - : []; + const raw = recentEmojiEvent?.getContent().recent_emoji; + const recentEmoji = Array.isArray(raw) ? normalizeEntries(raw as RecentEmojiEntry[]) : []; - const emojiIndex = recentEmoji.findIndex(([u]) => u === unicode); - let entry: [EmojiUnicode, EmojiUsageCount]; - if (emojiIndex < 0) { - entry = [unicode, 1]; + const existingIndex = recentEmoji.findIndex((e) => e.emoji === unicode); + if (existingIndex < 0) { + recentEmoji.unshift({ emoji: unicode, total: 1 }); } else { - const spliced = recentEmoji.splice(emojiIndex, 1); - entry = spliced[0] ?? [unicode, 1]; - entry[1] += 1; + const [entry] = recentEmoji.splice(existingIndex, 1); + if (entry) { + entry.total += 1; + recentEmoji.unshift(entry); + } else { + recentEmoji.unshift({ emoji: unicode, total: 1 }); + } } - recentEmoji.unshift(entry); + mx.setAccountData(CustomAccountDataEvent.RecentEmoji, { recent_emoji: recentEmoji.slice(0, 100), }); diff --git a/src/client/initMatrix.ts b/src/client/initMatrix.ts index 61766bc19..f7f9edd9e 100644 --- a/src/client/initMatrix.ts +++ b/src/client/initMatrix.ts @@ -373,7 +373,7 @@ export type StartClientConfig = { }; export type ClientSyncDiagnostics = { - transport: 'sliding'; + transport: 'sliding' | 'classic'; syncState: string | null; sliding?: SlidingSyncDiagnostics; }; @@ -468,10 +468,11 @@ export const clearCacheAndReload = async (mx: MatrixClient) => { }; export const getClientSyncDiagnostics = (mx: MatrixClient): ClientSyncDiagnostics => { + const slidingManager = slidingSyncByClient.get(mx); return { - transport: 'sliding', + transport: slidingManager ? 'sliding' : 'classic', syncState: mx.getSyncState(), - sliding: slidingSyncByClient.get(mx)?.getDiagnostics(), + sliding: slidingManager?.getDiagnostics(), }; };