Fix recent emoji are not getting saved Refactor recent emoji retrieval to ensure structured cloning and proper type checking. The sdk was not updating account data because we are mutating the original and it compare and early return if found same.
48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
import { MatrixClient } from 'matrix-js-sdk';
|
|
import { getAccountData } from '../utils/room';
|
|
import { IEmoji, emojis } from './emoji';
|
|
import { AccountDataEvent } from '../../types/matrix/accountData';
|
|
|
|
type EmojiUnicode = string;
|
|
type EmojiUsageCount = number;
|
|
|
|
export type IRecentEmojiContent = {
|
|
recent_emoji?: [EmojiUnicode, EmojiUsageCount][];
|
|
};
|
|
|
|
export const getRecentEmojis = (mx: MatrixClient, limit?: number): IEmoji[] => {
|
|
const recentEmojiEvent = getAccountData(mx, AccountDataEvent.ElementRecentEmoji);
|
|
const recentEmoji = recentEmojiEvent?.getContent<IRecentEmojiContent>().recent_emoji;
|
|
if (!Array.isArray(recentEmoji)) return [];
|
|
|
|
return recentEmoji
|
|
.sort((e1, e2) => e2[1] - e1[1])
|
|
.slice(0, limit)
|
|
.reduce<IEmoji[]>((list, [unicode]) => {
|
|
const emoji = emojis.find((e) => e.unicode === unicode);
|
|
if (emoji) list.push(emoji);
|
|
return list;
|
|
}, []);
|
|
};
|
|
|
|
export function addRecentEmoji(mx: MatrixClient, unicode: string) {
|
|
const recentEmojiEvent = getAccountData(mx, AccountDataEvent.ElementRecentEmoji);
|
|
const recentEmojiContent = recentEmojiEvent?.getContent<IRecentEmojiContent>();
|
|
const recentEmoji =
|
|
recentEmojiContent && Array.isArray(recentEmojiContent.recent_emoji)
|
|
? structuredClone(recentEmojiContent.recent_emoji)
|
|
: [];
|
|
|
|
const emojiIndex = recentEmoji.findIndex(([u]) => u === unicode);
|
|
let entry: [EmojiUnicode, EmojiUsageCount];
|
|
if (emojiIndex < 0) {
|
|
entry = [unicode, 1];
|
|
} else {
|
|
[entry] = recentEmoji.splice(emojiIndex, 1);
|
|
entry[1] += 1;
|
|
}
|
|
recentEmoji.unshift(entry);
|
|
mx.setAccountData(AccountDataEvent.ElementRecentEmoji, {
|
|
recent_emoji: recentEmoji.slice(0, 100),
|
|
});
|
|
}
|