48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { MatrixClient } from 'matrix-js-sdk';
|
|
import { EMOJI_REG_G, JUMBO_EMOJI_REG } from '../../../utils/regex';
|
|
import { trimReplyFromBody } from '../../../utils/room';
|
|
import { emojis } from '../../../plugins/emoji';
|
|
import { EMOJI_CONFETTI_EVENT_TYPE, EmojiConfettiContent } from './types';
|
|
|
|
const SHORTCODE_PATTERN = /:([^\s:]+):/g;
|
|
|
|
export function extractJumboEmojis(body: string): string[] {
|
|
const trimmedBody = trimReplyFromBody(body).trim();
|
|
if (!JUMBO_EMOJI_REG.test(trimmedBody)) return [];
|
|
|
|
const found: string[] = [];
|
|
|
|
const emojiMatches = Array.from(trimmedBody.matchAll(EMOJI_REG_G));
|
|
for (let i = 0; i < emojiMatches.length; i += 1) {
|
|
const match = emojiMatches[i];
|
|
const emoji = match[1];
|
|
if (emoji) found.push(emoji);
|
|
}
|
|
|
|
const shortcodeMatches = Array.from(trimmedBody.matchAll(SHORTCODE_PATTERN));
|
|
for (let i = 0; i < shortcodeMatches.length; i += 1) {
|
|
const match = shortcodeMatches[i];
|
|
const shortcode = match[1];
|
|
const resolved = emojis.find((emoji) => emoji.shortcode === shortcode);
|
|
if (resolved) found.push(resolved.unicode);
|
|
}
|
|
|
|
return found.length > 0 ? found : ['🎉'];
|
|
}
|
|
|
|
export function sendEmojiConfettiEvent(
|
|
mx: MatrixClient,
|
|
roomId: string,
|
|
targetEventId: string,
|
|
emojiList: string[],
|
|
) {
|
|
const content: EmojiConfettiContent = {
|
|
// emojis is the event content wire field
|
|
emojis: emojiList,
|
|
msgtype: EMOJI_CONFETTI_EVENT_TYPE,
|
|
target_event_id: targetEventId,
|
|
};
|
|
|
|
return mx.sendEvent(roomId, EMOJI_CONFETTI_EVENT_TYPE as never, content);
|
|
}
|