Files
client/app/renderer/js/chat-options.js
T

69 lines
3.4 KiB
JavaScript

// Pure chat-option semantics (official Settings > Chats, see app/main/chat-prefs.js). No DOM or IPC here, so
// chat.js, notifications and tests share one definition of what each preference does.
// Settings > Chats > Markdown Help entries in the official syntaxIds order (client_ui/main.js module 71415);
// texts are settings.chat.markdown_help.syntax.<id>.{name,syntax,example,notes}.
export const MARKDOWN_HELP_IDS = [
'heading', 'setext_heading', 'emphasis_italic', 'strong_bold', 'strikethrough', 'underline', 'highlight',
'superscript', 'subscript', 'inline_code', 'inline_math', 'link', 'reference_link', 'link_reference_definition',
'sized_image_pipe', 'sized_image_equals', 'autolink', 'plain_url', 'blockquote', 'callout', 'unordered_list',
'ordered_list', 'task_list_item', 'fenced_code_block', 'indented_code_block', 'fenced_math_block',
'mermaid_diagram', 'table', 'thematic_break', 'details_summary', 'align', 'footnote_reference',
'footnote_definition', 'spoiler', 'escape_sequence', 'html_entity', 'emoji',
];
/** Official appearanceSettings.chat_show_system_messages: hide moderation events only; text and pokes always show. */
export function showsMessage(msg, prefs) {
return !msg.moderation || prefs?.chatShowModerationEvents !== false;
}
/** Official use24hrTime (default on): "HH:mm" when on, the locale's 12-hour clock ("h:mm A") when off. */
export function formatClock(date, locale, prefs) {
return prefs?.chatUse24hTime === false
? date.toLocaleTimeString(locale, { hour: 'numeric', minute: '2-digit', hourCycle: 'h12' })
: date.toLocaleTimeString(locale, { hour: '2-digit', minute: '2-digit', hourCycle: 'h23' });
}
/** Official openIncomingChats (default on): an incoming private message opens its chat in the sidebar list. */
export function opensIncoming(prefs) {
return prefs?.chatOpenIncoming !== false;
}
export function ignoredUsers(prefs) {
return Array.isArray(prefs?.chatIgnoredUsers) ? prefs.chatIgnoredUsers : [];
}
export function isIgnoredUid(prefs, uid) {
return typeof uid === 'string' && uid.length > 0 && ignoredUsers(prefs).some((entry) => entry.uid === uid);
}
/**
* True when an inbound `text` core event (channel/server/client message or poke) comes from an ignored client.
* Only the server-reported invoker UID counts; our own messages and events without a sender UID (server
* notices) are never suppressed. The event object is not modified.
*/
export function isIgnoredText(evt, conn, prefs) {
if (!evt || evt.from_client_id === conn?.snap?.own_client_id) return false;
const uid = typeof evt.from_uid === 'string' ? evt.from_uid : null;
if (!uid || uid === ownUid(conn)) return false;
return isIgnoredUid(prefs, uid);
}
function ownUid(conn) {
const own = conn?.snap?.own_client_id;
return conn?.snap?.clients?.find((client) => client.id === own)?.uid ?? null;
}
const CONTROL = /[\u0000-\u001f\u007f]/g;
/** New ignore list with `uid` added (or its stored name refreshed in place); never mutates `prefs`. */
export function withIgnored(prefs, uid, name) {
const entry = { uid, name: String(name ?? '').replace(CONTROL, '').slice(0, 128) };
const list = ignoredUsers(prefs);
return list.some((item) => item.uid === uid) ? list.map((item) => (item.uid === uid ? entry : item)) : [...list, entry];
}
export function withoutIgnored(prefs, uid) {
return ignoredUsers(prefs).filter((entry) => entry.uid !== uid);
}