Files
ChatApp/apps/desktop/src/lib/ringtoneStorage.ts
T
byGalax 672c8738c7 feat: file variety, user status, DND gate, drag-drop, mentions, link preview, emoji picker, soundboard, ringtone
Attachments
- Video: native <video controls>, decrypted blob, metadata preload
- PDF: collapsible <object> viewer with download + preview toggle
- Generic: file card with lazy decrypt, mime-to-extension map
- MessageBubble dispatch: audio/image/video/pdf/generic by mime
- Composer accept widened from image/* to any; AttachmentPreview renders
  non-images as file cards

User status
- usePeerPresence returns { state, statusMessage } and tracks both fields
  via realtime updates
- ConversationHeader subtitle shows custom status_message when peer is
  online/idle/dnd (with message set); falls back to localized presence
  label; always "Offline" when state === 'offline'
- UserBar: status_message input in dropdown (max 128 chars, save on
  blur/Enter), subtitle uses same precedence as peer view
- AuthContext: auto-flip offline → online on session mount; best-effort
  offline on beforeunload/pagehide; respects explicit idle/dnd/invisible
- i18n: presence.status_placeholder (DE + EN)

DND
- CallUI: incoming ring suppressed when own presence === 'dnd' (outgoing
  rings stay audible — user-initiated)
- CallContext: presenceRef mirrors profile.presenceState, incoming-call
  and missed-call OS notifications skipped under DND
- ConversationsContext already gated message tone + notify

Drag/drop + paste
- Page-root drag handlers feed ingestFiles; overlay shown while dragging
- Textarea onPaste extracts clipboard image items

@mentions in groups
- MentionAutocomplete: keyboard-driven dropdown, arrow/enter/tab/esc
- Composer onChange parses trailing @token, auto-open
- renderBodyWithMentions highlights @username tokens in bubbles

Link previews
- Migration 20260421000003_link_previews.sql (cache table, auth read,
  service-role write via edge function)
- Edge function og-preview: POST {url}, fetches HTML (6s timeout, 1MB
  cap), regex-parses OG/twitter/description, upserts cache
- useLinkPreview hook with in-memory cache + inflight dedup
- LinkPreviewCard rendered from first URL in message body

Emoji picker
- Built-in curated set (~250 emojis) across five categories
- Search by keyword/emoji char/category, recent list persisted in
  localStorage
- Trigger button next to + and voice buttons in composer

Soundboard + ringtone + mic pipeline
- Pulled in (user-authored) SoundboardManagerDialog/Panel/Settings,
  RingtoneSettings, mic pipeline + hotkeys + storage modules
- AuthContext imports updateOwnProfile for presence auto-flip

Fixes
- AttachmentAudio min-width so bubbles don't collapse to 0px on peer
  side
- Focus flicker: visibility/online wake refresh throttled to 30s,
  focus listener dropped, loading flag only on first fetch
2026-04-21 09:13:30 +02:00

109 lines
3.1 KiB
TypeScript

// IndexedDB-backed storage for the user's custom incoming ringtone.
//
// Only one slot is exposed (`incoming`) — outgoing ringtone stays tied to the
// bundled oscillator pattern. The blob is stored alongside its mime type +
// original filename so playback + UI can show what's currently in use.
const DB_NAME = 'netralax-ringtones';
const DB_VERSION = 1;
const STORE_NAME = 'ringtones';
const SLOT_INCOMING = 'incoming';
export interface StoredRingtone {
blob: Blob;
mime: string;
filename: string;
updatedAt: number;
}
export const MAX_RINGTONE_BYTES = 2 * 1024 * 1024; // 2 MB cap
export const SUPPORTED_RINGTONE_MIMES = [
'audio/mpeg',
'audio/mp3',
'audio/wav',
'audio/x-wav',
'audio/ogg',
'audio/webm',
'audio/mp4',
'audio/aac',
'audio/x-m4a',
'audio/m4a',
];
let dbPromise: Promise<IDBDatabase> | null = null;
function openDb(): Promise<IDBDatabase> {
if (dbPromise) return dbPromise;
dbPromise = new Promise<IDBDatabase>((resolve, reject) => {
const req = indexedDB.open(DB_NAME, DB_VERSION);
req.onupgradeneeded = () => {
const db = req.result;
if (!db.objectStoreNames.contains(STORE_NAME)) {
db.createObjectStore(STORE_NAME);
}
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error ?? new Error('indexedDB open failed'));
});
return dbPromise;
}
function runTx<T>(
mode: IDBTransactionMode,
fn: (store: IDBObjectStore) => IDBRequest<T> | void,
): Promise<T | undefined> {
return openDb().then(
(db) =>
new Promise<T | undefined>((resolve, reject) => {
const tx = db.transaction(STORE_NAME, mode);
const store = tx.objectStore(STORE_NAME);
let result: T | undefined = undefined;
const maybeReq = fn(store);
if (maybeReq) {
maybeReq.onsuccess = () => {
result = maybeReq.result;
};
maybeReq.onerror = () => reject(maybeReq.error);
}
tx.oncomplete = () => resolve(result);
tx.onerror = () => reject(tx.error);
tx.onabort = () => reject(tx.error);
}),
);
}
export async function saveIncomingRingtone(file: File): Promise<void> {
if (file.size === 0) throw new Error('empty file');
if (file.size > MAX_RINGTONE_BYTES) {
throw new Error('ringtone_too_large');
}
const mime = file.type || 'application/octet-stream';
if (!mime.startsWith('audio/')) {
throw new Error('ringtone_not_audio');
}
const stored: StoredRingtone = {
blob: file,
mime,
filename: file.name,
updatedAt: Date.now(),
};
await runTx('readwrite', (store) => store.put(stored, SLOT_INCOMING));
}
export async function getIncomingRingtone(): Promise<StoredRingtone | null> {
const result = await runTx<StoredRingtone | undefined>('readonly', (store) =>
store.get(SLOT_INCOMING) as IDBRequest<StoredRingtone | undefined>,
);
return result ?? null;
}
export async function clearIncomingRingtone(): Promise<void> {
await runTx('readwrite', (store) => store.delete(SLOT_INCOMING));
}
export async function hasIncomingRingtone(): Promise<boolean> {
const cur = await getIncomingRingtone();
return cur !== null;
}