import m, {MithrilComponent} from '@garage44/mithril' import {Button, Card, CloseButton, Dialog, FieldCheckbox, FieldSegmented, FieldSelect, FieldText, FieldTextarea, ThemeSwitch} from '@garage44/mithril/components' import {required, validation, validations} from '@garage44/mithril/validation' import {$s, signupApi} from '@/app' import { ChatClient, type CommunityListing, type CommunityPlace, type CommunitySettings, type CommunityView, type Message, type Role, } from '@/lib/chat' import {type CallMember, callName, readGuestLink} from '@/lib/call' import {CAPTION_LANGUAGES} from '@/lib/call-captions' import {inviteCode, inviteLink} from '@/lib/invite' import { type AnswerFor, type Meetings, type MeetingObject, type NoteKind, type Plan, type Poll, type Rsvp, NOTE_WORDS, RSVP_WORDS, agendaLine, changeWords, dateCount, dates, durationMs, ics, isSkipped, localZone, newId, nextDate, planChange, planNow, readMeetings, recordText, rsvpsFor, tally, } from '@/lib/meeting' import {type Meetup, dayStart, longDate, readMeetups, ruleWords} from '@/lib/calendar' import {bind, local} from '@/lib/reactive' import {ADMIN, COMMUNITY} from '@/lib/base' import {playerSignIn, playerSignOut} from '@/lib/signup' import {adoptSession, dropOperator} from '@/lib/accounts' import {layout} from '@/lib/layout' import {WorldSession, worldGpu} from '@/lib/world-frame' import {loadSiteMenu, setSiteLang, site} from '@/lib/site-menu' import {SiteBar, type Step} from '@/cms/sitebar' import {siteText} from '@/cms/tree' import {AdminArea, adminCrumbs} from '@/components/elements/admin/admin' import {Icon} from '@/lib/icons' import {ProviderChoices} from '@/components/elements/providers/providers' import {CommunityStats} from './community-stats' import {LinkedGroups} from './linked-groups' import {Notices, openPost, Posts, Tabs, tabOf, unreadNotices} from './posts' import {Calendar, type CalendarCtx, openCalendar, todayCount} from './calendar' /** * Chat: cartopolis.org's front door (`docs/mockups/communities/`). * * One list down the side — communities with their topics, then people and * groups — and one chat in the middle, with a thread beside it. A community is * a named set of people; its topics are encrypted chats that every member is * in. Everything said here is end-to-end encrypted; this page holds no keys * and no history, only what the chat worker (`src/worker/chat-worker.ts`) * tells it. */ /** The switch's three positions, and the path each one lives at. */ type Mode = 'community' | 'world' | 'admin' const PATHS: Record = {admin: ADMIN, community: COMMUNITY, world: '/world'} /** Which position the address is at: `/world`, `/admin`, or Community for the rest. */ function mode(): Mode { const path = (m.route.get() ?? '/').split('?')[0] if (path === '/world' || path.startsWith('/world/')) return 'world' if (path === ADMIN || path.startsWith(`${ADMIN}/`)) return 'admin' return 'community' } /** One client per page, made the first time the chat is shown. */ let client: ChatClient | null = null function chat(): ChatClient { client ??= new ChatClient(() => m.redraw()) return client } /** * Stand a client in for the real one, before the page is first drawn: how a * preview draws a call with several people in it on a machine with nobody to * call. */ export function standInClient(stand: ChatClient): void { client = stand } /** The reactions offered on a message, in this order. */ const QUICK = ['👍', '❤️', '😂', '🎉', '👀', '✅'] /** Where "set up a recovery key later" is kept: a choice about this browser. */ const RECOVERY_LATER_KEY = 'cartopolis.chat.recovery-later' /** The reasons a report can give, as the server names them. */ const REASONS = [ {label: 'Spam or a scam', value: 'spam'}, {label: 'Harassment or bullying', value: 'harassment'}, {label: 'Hate speech or a slur', value: 'hate_speech'}, {label: 'Sexual content', value: 'sexual_content'}, {label: 'Threat or self-harm', value: 'threat'}, {label: 'Something else', value: 'other'}, ] /** Whether typing is on a touch screen's keyboard, which has no Shift+Enter. */ function touchKeyboard(): boolean { return matchMedia('(pointer: coarse)').matches } /** How someone is shown: the bare name for a neighbour, else the full address. */ function shown(address: string, me: string | null): string { const [name, server] = address.split('@') const home = me?.split('@')[1] return server && server === home ? name : address } function hue(s: string): number { let h = 0 for (const c of s) h = (h * 31 + c.charCodeAt(0)) % 360 return h } /** * A person's avatar with a dot for whether they are signed in: green when * they are, a hollow ring when not, and nothing when nobody may know (not a * contact, or they hide it — `ContactSettings::show_online`). */ function Face(who: string, size = 32) { const online = chat().presence(who) return ( {Avatar(who, size)} {online !== null && ( )} ) } /** " · online" or " · offline" after a header's other facts, or nothing when unknown. */ function presenceWords(online: boolean | null): string { return online === null ? '' : online ? ' · online' : ' · offline' } /** "14:05" today, "Tue" this week, "3 Oct" before: a list row's when. */ function shortWhen(ms: number): string { if (!ms) return '' const d = new Date(ms) const now = new Date() if (d.toDateString() === now.toDateString()) return time(ms) if (now.getTime() - ms < 6 * 86400000) return d.toLocaleDateString([], {weekday: 'short'}) return d.toLocaleDateString([], {day: 'numeric', month: 'short'}) } function Avatar(who: string, size = 32) { const name = who.split('@')[0] const url = pictureUrl(faceHash(who)) if (url) { return } return ( {name.slice(0, 1).toUpperCase()} ) } /** * A call's audio or video element, its stream set on the element itself * (`srcObject` is a property, not an attribute) whenever it changes. */ function Media(stream: MediaStream, kind: 'audio' | 'video', muted: boolean) { const attach = (v: m.VnodeDOM) => { const el = v.dom as HTMLMediaElement if (el.srcObject !== stream) el.srcObject = stream } return kind === 'video' ?