mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
fix recent emojis and sliding sync display
This commit is contained in:
1 parent
8584b25e58
commit
2cfa5a7fab
4 files changed
+69
-25
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix format for recent emojis and related crash.
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix sliding sync debug saying sliding when it is in fact not sliding.
|
||||
@@ -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<IRecentEmojiContent>().recent_emoji;
|
||||
const raw = recentEmojiEvent?.getContent<IRecentEmojiContent>().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<IEmoji[]>((list, [unicode]) => {
|
||||
.reduce<IEmoji[]>((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<IRecentEmojiContent>();
|
||||
const recentEmoji =
|
||||
recentEmojiContent && Array.isArray(recentEmojiContent.recent_emoji)
|
||||
? structuredClone(recentEmojiContent.recent_emoji)
|
||||
: [];
|
||||
const raw = recentEmojiEvent?.getContent<IRecentEmojiContent>().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),
|
||||
});
|
||||
|
||||
@@ -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(),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user