export type ContentMode = 'account' | 'hide' | 'blur' | 'show'; export type ContentSettings = { adultContent: 'account' | 'allow' | 'disallow'; labels: Record; includeReposts?: boolean; repostCooldownHours?: number; }; type SettingsStorage = Pick; const modes = new Set(['account', 'hide', 'blur', 'show']); const key = (did: string) => `mural:content-settings:v2:${did}`; export function defaultContentSettings(): ContentSettings { return { adultContent: 'account', labels: {} }; } export function parseContentSettings(value: unknown): ContentSettings { const result = defaultContentSettings(); if (!value || typeof value !== 'object') return result; const input = value as Record; if (input.includeReposts === true) result.includeReposts = true; if ( typeof input.repostCooldownHours === 'number' && Number.isFinite(input.repostCooldownHours) && input.repostCooldownHours > 0 && input.repostCooldownHours <= 720 ) result.repostCooldownHours = input.repostCooldownHours; if (input.adultContent === 'allow' || input.adultContent === 'disallow') result.adultContent = input.adultContent; if (input.labels && typeof input.labels === 'object' && !Array.isArray(input.labels)) { for (const [name, mode] of Object.entries(input.labels).slice(0, 200)) { if ( name.length > 256 || name === '__proto__' || name === 'constructor' || name === 'prototype' ) continue; if (typeof mode === 'string' && modes.has(mode) && mode !== 'account') result.labels[name] = mode as ContentMode; } } return result; } function browserStorage(): SettingsStorage | undefined { try { return typeof localStorage === 'undefined' ? undefined : localStorage; } catch { return undefined; } } export function readContentSettings(did?: string, storage = browserStorage()): ContentSettings { if (!did?.startsWith('did:')) return defaultContentSettings(); try { const raw = storage?.getItem(key(did)); return raw && raw.length <= 65_536 ? parseContentSettings(JSON.parse(raw)) : defaultContentSettings(); } catch { return defaultContentSettings(); } } export function saveContentSettings( did: string | undefined, settings: ContentSettings, storage = browserStorage() ): boolean { try { if (!did?.startsWith('did:') || !storage) return false; storage.setItem(key(did), JSON.stringify(parseContentSettings(settings))); return true; } catch { return false; } }