c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 1 | import type { RoomSummary } from './types'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 2 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 3 | export type MessageSoundMode = 'all' | 'mentions_dms' | 'off'; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 4 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 5 | export interface MessageSoundSettings { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 6 | mode: MessageSoundMode; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 7 | sound: string | null; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 8 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 9 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 10 | export const DEFAULT_MESSAGE_SOUND: MessageSoundSettings = { mode: 'mentions_dms', sound: null }; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 11 | export const SOUND_MAX_BYTES = 1024 * 1024; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 12 | export const SOUND_THROTTLE_MS = 1500; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 13 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 14 | export function parseMessageSound(raw: unknown): MessageSoundSettings { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 15 | if (typeof raw !== 'object' || raw === null) return DEFAULT_MESSAGE_SOUND; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 16 | const { mode, sound } = raw as Record<string, unknown>; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 17 | return { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 18 | mode: mode === 'all' || mode === 'mentions_dms' || mode === 'off' ? mode : DEFAULT_MESSAGE_SOUND.mode, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 19 | sound: typeof sound === 'string' && sound.startsWith('mxc://') ? sound : null |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 20 | }; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 21 | } |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 22 | |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 23 | export function shouldPlaySound( |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 24 | mode: MessageSoundMode, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 25 | room: Pick<RoomSummary, 'direct'>, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 26 | arrival: { mention: boolean }, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 27 | lastPlayedAt: number, |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 28 | now: number |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 29 | ): boolean { |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 30 | if (mode === 'off' || now - lastPlayedAt < SOUND_THROTTLE_MS) return false; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 31 | return mode === 'all' || arrival.mention || room.direct; |
c8d898e6eb59 | atagen | 2026-10-27 03:55 UTC | 32 | } |