garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /crates/atlas/web/src/components/pages/chat/posts.tsx

raw

import m from '@garage44/mithril'
import {Button, FieldSegmented, FieldText} from '@garage44/mithril/components'

import {signupApi} from '@/app'
import {ApiError} from '@/lib/api'
import type {CommunityView} from '@/lib/chat'
import {Icon} from '@/lib/icons'
import {parsePlace, placeParam, suggestHandle, webLink} from '@/lib/posts'
import type {
    FollowLink,
    NoticeKind,
    PostDto,
    PostNotices,
    PostPlace,
    PostReport,
    PostThread,
    PublicInfo,
    WhoPosts,
} from '@/types'

/**
 * A community's public posts (`docs/notes/fediverse.md`,
 * `docs/mockups/communities/v4-public-posts.html`).
 *
 * The other half of a community page from its topics: public where they are
 * encrypted, and on the fediverse where they never are. Plain HTTP against the
 * simulator (`/api/communities/{id}/posts`), not the chat worker, because there
 * is nothing to seal: the server stores posts readable on purpose, to send
 * them to other servers.
 *
 * Laid out as a feed, as Mastodon's is: a box to write in at the top, then
 * each post as a card with its author, its words, its picture and its place,
 * and its replies opening underneath it rather than on a page of their own.
 * What is done rarely (hide, block, delete) sits behind a ⋯ menu.
 *
 * A post always has a place. The app takes it from the camera; here it is the
 * community's own place, where this browser is, or a Cartopolis place link
 * pasted in (Copy link in the app, or any `/go?place=` link). A post's place
 * opens in World, flown to the view it was written from.
 */

export type Tab = 'posts' | 'topics'

interface Draft {
    community: string
    text: string
    /** What was typed into the place field: a link or "lat, lng". */
    where: string
    place: PostPlace | null
    picture: string | null
    /** What the picture shows, in words, for somebody who cannot see it. */
    alt: string
    /** Blur the picture until a reader asks to see it. */
    sensitive: boolean
    /** A content warning, shown in place of the post until it is opened; null when off. */
    warning: string | null
    /** Whether the place field is showing. */
    placing: boolean
}

/**
 * Where a card is drawn: which community, and whether the reader moderates it
 * and is signed in. A community page passes its own; the public pages
 * (`pages/public`) a reader who may be nobody.
 */
export interface Where {
    id: string
    mod: boolean
    signedIn: boolean
}

/** What the page lends this view: pictures, faces, and the World frame. */
export interface PostsCtx {
    pictureUrl: (hash: string | null | undefined) => string | null
    avatar: (address: string, size: number) => m.Children
    /** Open World at a place, given as a link's `place=` value. */
    onMap: (place: string) => void
}

/** Everything the posts half holds, across communities. */
const st = {
    busy: false,
    draft: null as Draft | null,
    /** The post being edited, and its words so far. */
    editing: null as {id: string; text: string} | null,
    error: '',
    feed: new Map<string, PostDto[]>(),
    follow: '',
    handle: '',
    info: new Map<string, PublicInfo>(),
    loaded: null as string | null,
    reply: '',
    /** The reply being answered, when it is one rather than the post. */
    replyTo: null as PostDto | null,
    /** The post being reported, and what the reporter says. */
    reporting: null as {id: string; reason: string; forward: boolean} | null,
    /** Posts behind a content warning, or with a sensitive picture, that the reader opened. */
    shown: new Set<string>(),
    /** Reports from other servers, per community, for its moderators. */
    reports: new Map<string, PostReport[]>(),
    settingsOpen: false,
    tab: new Map<string, Tab>(),
    thread: null as PostThread | null,
    threadOpen: null as string | null,
}

function failed(e: unknown): void {
    st.busy = false
    st.error = e instanceof ApiError ? e.message : `Not done: ${(e as Error).message}`
    m.redraw()
}

/** What has been read of a community's public half, for the public pages. */
export function readOf(id: string): {info: PublicInfo | undefined; feed: PostDto[]} {
    return {feed: st.feed.get(id) ?? [], info: st.info.get(id)}
}

/** Read a community's public half and its newest posts. */
export async function load(id: string): Promise<void> {
    try {
        const [info, feed] = await Promise.all([
            signupApi.get<PublicInfo>(`/api/communities/${id}/public`),
            signupApi.get<PostDto[]>(`/api/communities/${id}/posts`),
        ])
        st.info.set(id, info)
        st.feed.set(id, feed)
        if (!st.handle && info.settings.handle) st.handle = info.settings.handle
        if (info.open_reports > 0) st.reports.set(id, await signupApi.get<PostReport[]>(`/api/communities/${id}/reports`))
        else st.reports.delete(id)
    } catch (e) {
        failed(e)
    }
    m.redraw()
}

/** Open (or close) a post's replies under it. */
export async function toggleThread(post: string): Promise<void> {
    if (st.threadOpen === post) {
        st.threadOpen = null
        st.thread = null
        return
    }
    st.threadOpen = post
    st.thread = null
    st.reply = ''
    st.replyTo = null
    try {
        st.thread = await signupApi.get<PostThread>(`/api/posts/${post}`)
    } catch (e) {
        failed(e)
    }
    m.redraw()
}

export function ago(ms: number): string {
    const s = Math.max(0, (Date.now() - ms) / 1000)
    if (s < 60) return 'now'
    if (s < 3600) return `${Math.floor(s / 60)} min`
    if (s < 86400) return `${Math.floor(s / 3600)} h`
    if (s < 604800) return `${Math.floor(s / 86400)} d`
    return new Date(ms).toLocaleDateString()
}

/** A picked picture, at most 1600 px on its longest edge, as a JPEG in the picture store. */
async function uploadPicture(file: File, d: Draft): Promise<void> {
    st.busy = true
    m.redraw()
    try {
        const bitmap = await createImageBitmap(file)
        const scale = Math.min(1, 1600 / Math.max(bitmap.width, bitmap.height))
        const canvas = document.createElement('canvas')
        canvas.width = Math.round(bitmap.width * scale)
        canvas.height = Math.round(bitmap.height * scale)
        canvas.getContext('2d')!.drawImage(bitmap, 0, 0, canvas.width, canvas.height)
        const blob = await new Promise<Blob>((ok, fail) =>
            canvas.toBlob((b) => (b ? ok(b) : fail(new Error('could not encode it'))), 'image/jpeg', 0.85))
        const {hash} = await signupApi.upload<{hash: string}>('/api/pictures', await blob.arrayBuffer(), 'application/octet-stream')
        d.picture = hash
        st.busy = false
    } catch (e) {
        failed(e)
    }
    m.redraw()
}

/** Which half of a community page is showing; the posts, unless chosen otherwise. */
export function tabOf(id: string): Tab {
    return st.tab.get(id) ?? 'posts'
}

/** The two halves of a community page, side by side. */
export function Tabs(id: string, topics: number) {
    if (st.loaded !== id) {
        st.loaded = id
        st.threadOpen = null
        st.thread = null
        st.settingsOpen = false
        st.handle = ''
        st.error = ''
        st.draft = null
        void load(id)
    }
    return (
        <div class="post-tabs">
            <FieldSegmented
                model={{
                    get value() {
                        return tabOf(id)
                    },
                    set value(t: Tab) {
                        st.tab.set(id, t)
                    },
                }}
                options={[
                    {label: 'Posts · public', value: 'posts' as Tab},
                    {label: `Topics · ${topics} · encrypted`, value: 'topics' as Tab},
                ]}
                size="sm"
            />
        </div>
    )
}

/** The public half of a community page. */
export function Posts(community: CommunityView, me: string, ctx: PostsCtx) {
    const id = community.id
    const mod = community.role === 'owner' || community.role === 'moderator'
    const info = st.info.get(id)
    const error = st.error && (
        <p class="post-error">
            {st.error}
            <button aria-label="Dismiss" onclick={() => (st.error = '')} type="button">{Icon('close')}</button>
        </p>
    )
    if (!info) return [error, <p class="chat-hint">Reading the posts…</p>]
    if (!info.settings.enabled) {
        return [
            error,
            <div class="post-empty">
                <b>No public posts yet</b>
                <p>
                    With them on, anybody can read what members post here: on the map, and on the fediverse (Mastodon, Lemmy
                    and others), where people can follow the community and reply.
                    {mod ? '' : ' Its owner or a moderator can switch them on.'}
                </p>
            </div>,
            mod && Settings(community, info),
        ]
    }
    const feed = st.feed.get(id) ?? []
    const where: Where = {id, mod, signedIn: true}
    return [
        <div class="post-bar">
            <span class="post-public" title="Public: anybody can read these, here and on other servers">
                {Icon('public')} Public · {info.followers} follower{info.followers === 1 ? '' : 's'} on the fediverse
                {info.address && <span class="post-dim"> · @{info.address}</span>}
            </span>
            <span class="post-bar-tools">
                <button aria-label="Read again" class="post-icon" onclick={() => void load(id)} title="Read again" type="button">{Icon('refresh')}</button>
                {mod && (
                    <button
                        aria-label="Settings"
                        class={st.settingsOpen ? 'post-icon is-on' : 'post-icon'}
                        onclick={() => (st.settingsOpen = !st.settingsOpen)}
                        title="Settings"
                        type="button"
                    >
                        {Icon('settings')}
                    </button>
                )}
            </span>
        </div>,
        error,
        info.address && info.federating && FollowFrom(info.address, true),
        mod && st.settingsOpen && Settings(community, info),
        mod && Reports(id),
        info.may_post && Composer(community, info, me, ctx),
        feed.length === 0 && (
            <div class="post-empty">
                <b>Nothing posted yet</b>
                <p>
                    {info.may_post
                        ? 'Write the first: it is pinned on the map where it is about.'
                        : 'Posts from members and from the fediverse appear here.'}
                </p>
            </div>
        ),
        feed.map((p) => Card(where, p, ctx, true)),
    ]
}

/** What the box asking for somebody's own server holds, per address. */
const fromServer = new Map<string, string>()

/**
 * "Follow from your own server": somebody on Mastodon, Lemmy or elsewhere
 * types their server, and is sent to the page there that follows this address
 * — Mastodon's follow screen, Lemmy's search, Misskey's — which the simulator
 * picks by asking their server what it runs (`/api/fediverse/follow-link`).
 */
export function FollowFrom(address: string, group: boolean) {
    const typed = fromServer.get(address) ?? ''
    const go = async () => {
        const server = typed.trim()
        if (!server) return
        try {
            const link = await signupApi.get<FollowLink>(
                `/api/fediverse/follow-link?server=${encodeURIComponent(server)}&address=${encodeURIComponent(address)}`,
            )
            const url = webLink(link.url)
            if (url) window.open(url, '_blank', 'noopener')
        } catch (e) {
            failed(e)
        }
    }
    return (
        <form
            class="post-follow-from"
            onsubmit={(e: Event) => {
                e.preventDefault()
                void go()
            }}
        >
            <span class="post-dim">
                Follow {group ? `@${address} (on Lemmy: !${address})` : `@${address}`} from your own server
            </span>
            <span class="post-follow-from-row">
                <input
                    aria-label="Your server"
                    oninput={(e: Event) => fromServer.set(address, (e.target as HTMLInputElement).value)}
                    placeholder="mastodon.social, lemmy.world, …"
                    value={typed}
                />
                <Button disabled={!typed.trim()} size="sm" type="submit">Follow</Button>
                <button
                    class="post-icon"
                    onclick={() => void navigator.clipboard?.writeText(`@${address}`)}
                    title="Copy the address"
                    type="button"
                >
                    {Icon('content_copy')}
                </button>
            </span>
        </form>
    )
}

/** Reports from other servers about posts here, for the moderators. */
function Reports(id: string) {
    const reports = st.reports.get(id) ?? []
    if (reports.length === 0) return null
    const resolve = async (report: string, action: 'hide' | 'dismiss') => {
        try {
            await signupApi.post(`/api/communities/${id}/reports/${report}`, {action})
            await load(id)
        } catch (e) {
            failed(e)
        }
    }
    return (
        <div class="post-reports">
            <h3 class="chat-pane-h">
                {Icon('flag')} {reports.length} report{reports.length === 1 ? '' : 's'} from other servers
            </h3>
            {reports.map((r) => (
                <div class="post-report" key={r.id}>
                    <p class="post-dim is-public">{r.reporter} · {ago(r.created_ms)}</p>
                    {r.reason && <p class="post-report-reason">{r.reason}</p>}
                    <blockquote class="post-report-post">
                        <b>{r.post.author.address}</b> {r.post.text}
                    </blockquote>
                    <span class="post-report-actions">
                        <Button onclick={() => void resolve(r.id, 'hide')} size="sm">Hide the post</Button>
                        <Button onclick={() => void resolve(r.id, 'dismiss')} size="sm" variant="ghost">Leave it</Button>
                    </span>
                </div>
            ))}
        </div>
    )
}

/** A post's place, as a chip that opens it in World. */
function PlaceChip(place: PostPlace | undefined, ctx: PostsCtx) {
    if (!place) return <span class="post-noplace">Not on the map</span>
    return (
        <button
            class="post-place-chip"
            onclick={() => ctx.onMap(placeParam(place))}
            title="Open in World, seen from where it was written"
            type="button"
        >
            {Icon('location_on')} {place.name ?? 'Show on the map'}
        </button>
    )
}

export function Card(where: Where, p: PostDto, ctx: PostsCtx, top: boolean) {
    const mod = where.mod
    const url = ctx.pictureUrl(p.picture)
    const snapshot = ctx.pictureUrl(p.snapshot)
    const open = top && st.threadOpen === p.id
    const act = async (run: () => Promise<unknown>) => {
        try {
            await run()
            if (st.threadOpen === p.id) st.threadOpen = null
            await load(where.id)
        } catch (e) {
            failed(e)
        }
    }
    const editing = st.editing?.id === p.id ? st.editing : null
    const saveEdit = async () => {
        const text = editing?.text.trim()
        if (!text || st.busy) return
        st.busy = true
        try {
            const changed = await signupApi.put<PostDto>(`/api/posts/${p.id}`, {text})
            Object.assign(p, changed)
            st.editing = null
            st.busy = false
        } catch (e) {
            failed(e)
        }
        m.redraw()
    }
    const menu = [
        p.mine && (
            <button onclick={() => (st.editing = {id: p.id, text: p.text})} type="button">
                Edit
                <small>Other servers get the change, as they do Mastodon's own edits</small>
            </button>
        ),
        p.mine && (
            <button onclick={() => void act(() => signupApi.delete(`/api/posts/${p.id}`))} type="button">
                Delete
                <small>Here and, as far as they listen, on other servers</small>
            </button>
        ),
        mod && !p.mine && (
            <button onclick={() => void act(() => signupApi.post(`/api/posts/${p.id}/hide`))} type="button">
                Hide from this community
            </button>
        ),
        mod && !p.author.local && (
            <button
                onclick={() =>
                    void act(() => signupApi.post(`/api/communities/${where.id}/blocks`, {address: p.author.address}))}
                type="button"
            >
                Block {p.author.address}
                <small>They can't post here again, and what they wrote is hidden</small>
            </button>
        ),
        !p.author.local && webLink(p.url) && (
            <a href={webLink(p.url) ?? undefined} rel="noopener" target="_blank">Open where it was posted</a>
        ),
        where.signedIn && !p.mine && (
            <button onclick={() => (st.reporting = {forward: false, id: p.id, reason: ''})} type="button">
                Report
                <small>To this community's moderators{p.author.local ? '' : ', and if you like to its own server'}</small>
            </button>
        ),
    ].filter(Boolean)
    // Behind a content warning, the words and pictures wait until opened; a
    // sensitive picture is blurred until clicked.
    const hidden = !!p.warning && !p.mine && !st.shown.has(p.id)
    const blurred = p.sensitive && !p.mine && !st.shown.has(p.id)
    const pictures = [
        url && {alt: p.picture_alt ?? '', src: url},
        ...(p.more_pictures ?? []).map((x) => {
            const src = ctx.pictureUrl(x.hash)
            return src && {alt: x.alt ?? '', src}
        }),
    ].filter(Boolean) as {alt: string; src: string}[]
    return (
        <article class={top ? 'post-card' : 'post-card is-reply'} key={p.id}>
            <div class="post-face">{ctx.avatar(p.author.address, top ? 36 : 28)}</div>
            <div class="post-main">
                <header class="post-head">
                    <span class="post-who">
                        <b>{p.author.name}</b>
                        <span class={p.author.local ? 'post-dim' : 'post-dim is-public'}>
                            {p.author.local ? 'member' : `@${p.author.address}`}
                        </span>
                    </span>
                    <span class="post-dim" title={new Date(p.created_ms).toLocaleString()}>
                        {ago(p.created_ms)}
                        {p.edited_ms && <span title={`Edited ${new Date(p.edited_ms).toLocaleString()}`}> · edited</span>}
                    </span>
                    {menu.length > 0 && (
                        <details class="post-menu">
                            <summary aria-label="More" title="More">{Icon('more_horiz')}</summary>
                            <div class="post-menu-list">{menu}</div>
                        </details>
                    )}
                </header>
                {p.via && <div class="post-via">via {p.via}</div>}
                {p.answers && <div class="post-via">{Icon('reply')} to {p.answers}</div>}
                {p.warning && (
                    <div class="post-warning">
                        {Icon('warning')} <span>{p.warning}</span>
                        {!p.mine && (
                            <Button
                                onclick={() => (hidden ? st.shown.add(p.id) : st.shown.delete(p.id))}
                                size="sm"
                                variant="ghost"
                            >
                                {hidden ? 'Show' : 'Hide'}
                            </Button>
                        )}
                    </div>
                )}
                {hidden ? null : editing ? (
                    <form
                        class="post-edit"
                        onsubmit={(e: Event) => {
                            e.preventDefault()
                            void saveEdit()
                        }}
                    >
                        <textarea
                            maxlength={500}
                            oninput={(e: Event) => (editing.text = (e.target as HTMLTextAreaElement).value)}
                            rows={3}
                            value={editing.text}
                        />
                        <span class="post-edit-actions">
                            <Button onclick={() => (st.editing = null)} size="sm" variant="ghost">Cancel</Button>
                            <Button disabled={!editing.text.trim() || st.busy} size="sm" type="submit">Save</Button>
                        </span>
                    </form>
                ) : <p class="post-text">{p.text}</p>}
                {!hidden && snapshot && (
                    <img alt="A snapshot of the map where this was written" class="post-polaroid" loading="lazy" src={snapshot} />
                )}
                {!hidden && pictures.map((pic) => (
                    <img
                        alt={pic.alt}
                        class={blurred ? 'post-picture is-blurred' : 'post-picture'}
                        loading="lazy"
                        onclick={() => blurred && st.shown.add(p.id)}
                        src={pic.src}
                        title={blurred ? 'Sensitive: click to show' : pic.alt || undefined}
                    />
                ))}
                {st.reporting?.id === p.id && ReportBox(where, p)}
                {top && (
                    <footer class="post-foot">
                        {PlaceChip(p.place, ctx)}
                        <button
                            class={open ? 'post-replies is-on' : 'post-replies'}
                            onclick={() => void toggleThread(p.id)}
                            type="button"
                        >
                            {Icon('comment')} {p.replies} {p.replies === 1 ? 'reply' : 'replies'}
                        </button>
                        {Actions(where, p, top)}
                    </footer>
                )}
                {!top && <footer class="post-foot">{Actions(where, p, top)}</footer>}
                {open && Replies(where, ctx)}
            </div>
        </article>
    )
}

/** Update a post wherever this page holds it: the feed, the open thread. */
function replaceEverywhere(changed: PostDto): void {
    for (const list of [st.feed.get(changed.community), st.thread?.replies, st.thread ? [st.thread.post] : []]) {
        const at = list?.find((x) => x.id === changed.id)
        if (at) Object.assign(at, changed)
    }
}

/**
 * Like, pass on, and answer: for somebody signed in. Counts include what
 * other servers sent (Mastodon's favourites and boosts, Lemmy's votes).
 */
function Actions(where: Where, p: PostDto, top: boolean) {
    const toggle = async (what: 'like' | 'boost', on: boolean) => {
        try {
            replaceEverywhere(await signupApi.post<PostDto>(`/api/posts/${p.id}/${what}`, {on}))
        } catch (e) {
            failed(e)
        }
        m.redraw()
    }
    const likes = (
        <>
            {Icon('favorite')} {p.likes}
            {p.dislikes > 0 && <> · {Icon('thumb_down')} {p.dislikes}</>}
        </>
    )
    if (!where.signedIn) {
        return (p.likes > 0 || p.dislikes > 0 || p.boosts > 0) && (
            <span class="post-votes">
                {likes}
                {p.boosts > 0 && <> · {Icon('repeat')} {p.boosts}</>}
            </span>
        )
    }
    return (
        <span class="post-actions">
            <button
                aria-pressed={p.liked}
                class={p.liked ? 'post-action is-on' : 'post-action'}
                onclick={() => void toggle('like', !p.liked)}
                title={p.liked ? 'Take your like back' : 'Like'}
                type="button"
            >
                {likes}
            </button>
            {top && !p.mine && (
                <button
                    aria-pressed={p.boosted}
                    class={p.boosted ? 'post-action is-on' : 'post-action'}
                    onclick={() => void toggle('boost', !p.boosted)}
                    title={p.boosted ? 'Stop passing it on' : 'Pass it on to the people who follow you'}
                    type="button"
                >
                    {Icon('repeat')} {p.boosts}
                </button>
            )}
            {top && p.mine && p.boosts > 0 && <span class="post-votes">{Icon('repeat')} {p.boosts}</span>}
            {!top && (
                <button
                    class="post-action"
                    onclick={() => {
                        st.replyTo = p
                        if (!st.reply.trim() && !p.author.local) st.reply = `@${p.author.address} `
                    }}
                    title={`Answer ${p.author.name}`}
                    type="button"
                >
                    {Icon('reply')} Reply
                </button>
            )}
        </span>
    )
}

/** What somebody reporting a post says, and whether it goes to its own server too. */
function ReportBox(where: Where, p: PostDto) {
    const r = st.reporting!
    const send = async () => {
        try {
            await signupApi.post(`/api/posts/${p.id}/report`, {forward: r.forward, reason: r.reason.trim()})
            st.reporting = null
            st.error = 'Reported. The moderators will look at it.'
        } catch (e) {
            failed(e)
        }
        m.redraw()
    }
    return (
        <form
            class="post-report-box"
            onsubmit={(e: Event) => {
                e.preventDefault()
                void send()
            }}
        >
            <textarea
                maxlength={1000}
                oninput={(e: Event) => (r.reason = (e.target as HTMLTextAreaElement).value)}
                placeholder="What is wrong with it? (optional)"
                rows={2}
                value={r.reason}
            />
            {!p.author.local && (
                <label class="post-check">
                    <input
                        checked={r.forward}
                        onchange={(e: Event) => (r.forward = (e.target as HTMLInputElement).checked)}
                        type="checkbox"
                    />
                    Also tell {p.author.address.split('@')[1] ?? 'its server'}, without your name
                </label>
            )}
            <span class="post-edit-actions">
                <Button onclick={() => (st.reporting = null)} size="sm" variant="ghost">Cancel</Button>
                <Button disabled={st.busy || !where.signedIn} size="sm" type="submit">Report</Button>
            </span>
        </form>
    )
}

/** A post's replies, under it, with a box to answer in for somebody signed in. */
function Replies(where: Where, ctx: PostsCtx) {
    const t = st.thread
    if (!t) return <p class="post-dim post-loading">Reading the replies…</p>
    return (
        <div class="post-thread">
            {t.replies.map((r) => Card(where, r, ctx, false))}
            {!where.signedIn && <p class="post-dim">Sign in, or reply from your own fediverse server.</p>}
            {where.signedIn && <form
                class="post-reply"
                onsubmit={async (e: Event) => {
                    e.preventDefault()
                    const text = st.reply.trim()
                    if (!text || st.busy) return
                    st.busy = true
                    try {
                        const to = st.replyTo?.id ?? t.post.id
                        const reply = await signupApi.post<PostDto>(`/api/posts/${to}/replies`, {text})
                        t.replies.push(reply)
                        t.post.replies += 1
                        const inFeed = st.feed.get(where.id)?.find((x) => x.id === t.post.id)
                        if (inFeed) inFeed.replies = t.post.replies
                        st.reply = ''
                        st.replyTo = null
                        st.busy = false
                    } catch (err) {
                        failed(err)
                    }
                    m.redraw()
                }}
            >
                {st.replyTo && (
                    <span class="post-replying">
                        {Icon('reply')} To {st.replyTo.author.name}
                        <button aria-label="Answer the post instead" onclick={() => (st.replyTo = null)} type="button">{Icon('close')}</button>
                    </span>
                )}
                <textarea
                    maxlength={500}
                    oninput={(e: Event) => (st.reply = (e.target as HTMLTextAreaElement).value)}
                    placeholder="Reply in public"
                    rows={1}
                    value={st.reply}
                />
                <Button disabled={!st.reply.trim() || st.busy} size="sm" type="submit">Reply</Button>
            </form>}
        </div>
    )
}

/** The box at the top of the feed: one line until it is used, then the whole post. */
function Composer(community: CommunityView, info: PublicInfo, me: string, ctx: PostsCtx) {
    const d = st.draft?.community === community.id ? st.draft : null
    const start = () => {
        const p = community.settings.place
        st.draft = {
            alt: '',
            community: community.id,
            picture: null,
            sensitive: false,
            warning: null,
            place: p ? {latitude: p.latitude, longitude: p.longitude, name: p.name} : null,
            placing: !p,
            text: '',
            where: '',
        }
    }
    if (!d) {
        return (
            <button class="post-compose is-folded" onclick={start} type="button">
                <span class="post-face">{ctx.avatar(me, 36)}</span>
                <span class="post-compose-hint">Write a public post…</span>
            </button>
        )
    }
    const url = ctx.pictureUrl(d.picture)
    const reach = info.federating
        ? `Public. On this page and the map, and sent to ${info.followers} follower${info.followers === 1 ? '' : 's'} elsewhere, under your own address.`
        : 'Public. On this page and the map, for anybody on this server.'
    const ready = !!d.place && !!d.text.trim() && !st.busy
    const send = async () => {
        if (!ready || !d.place) return
        st.busy = true
        try {
            const post = await signupApi.post<PostDto>(`/api/communities/${community.id}/posts`, {
                picture: d.picture ?? undefined,
                place: d.place,
                text: d.text.trim(),
                ...(d.warning?.trim() ? {warning: d.warning.trim()} : {}),
                ...(d.picture && d.alt.trim() ? {picture_alt: d.alt.trim()} : {}),
                sensitive: !!d.picture && d.sensitive,
            })
            st.feed.set(community.id, [post, ...(st.feed.get(community.id) ?? [])])
            st.draft = null
            st.busy = false
        } catch (e) {
            failed(e)
        }
        m.redraw()
    }
    return (
        <div class="post-compose">
            <span class="post-face">{ctx.avatar(me, 36)}</span>
            <div class="post-main">
                <textarea
                    autofocus
                    class="post-compose-text"
                    maxlength={500}
                    oninput={(e: Event) => (d.text = (e.target as HTMLTextAreaElement).value)}
                    placeholder="What's happening here?"
                    rows={3}
                    value={d.text}
                />
                {d.warning !== null && (
                    <input
                        aria-label="Content warning"
                        class="post-compose-warning"
                        maxlength={100}
                        oninput={(e: Event) => (d.warning = (e.target as HTMLInputElement).value)}
                        placeholder="Content warning: what readers see before they open it"
                        value={d.warning}
                    />
                )}
                {url && (
                    <div class="post-compose-picture">
                        <img alt={d.alt} class="post-picture" src={url} />
                        <button aria-label="Remove the picture" onclick={() => (d.picture = null)} type="button">{Icon('close')}</button>
                    </div>
                )}
                {url && (
                    <div class="post-compose-alt">
                        <input
                            aria-label="What the photo shows"
                            maxlength={1500}
                            oninput={(e: Event) => (d.alt = (e.target as HTMLInputElement).value)}
                            placeholder="What the photo shows, for people who can't see it"
                            value={d.alt}
                        />
                        <label class="post-check">
                            <input
                                checked={d.sensitive}
                                onchange={(e: Event) => (d.sensitive = (e.target as HTMLInputElement).checked)}
                                type="checkbox"
                            />
                            Sensitive: blur it until opened
                        </label>
                    </div>
                )}
                {d.placing && (
                    <div class="post-placing">
                        <FieldText
                            model={{
                                get value() {
                                    return d.where
                                },
                                set value(v: string) {
                                    d.where = v
                                    const p = parsePlace(v)
                                    if (p) {
                                        d.place = p
                                        d.placing = false
                                    }
                                },
                            }}
                            placeholder="Paste a place link from the app (Copy link), or lat, lng"
                        />
                        <div class="post-placing-choices">
                            {community.settings.place && (
                                <button
                                    onclick={() => {
                                        const p = community.settings.place!
                                        d.place = {latitude: p.latitude, longitude: p.longitude, name: p.name}
                                        d.placing = false
                                    }}
                                    type="button"
                                >
                                    The community's place
                                </button>
                            )}
                            <button
                                onclick={() =>
                                    navigator.geolocation?.getCurrentPosition(
                                        (pos) => {
                                            d.place = {latitude: pos.coords.latitude, longitude: pos.coords.longitude}
                                            d.placing = false
                                            m.redraw()
                                        },
                                        () => failed(new Error('this browser would not say where it is')),
                                    )}
                                type="button"
                            >
                                {Icon('my_location')} Where I am
                            </button>
                        </div>
                    </div>
                )}
                <div class="post-compose-tools">
                    <button
                        class={d.place ? 'post-place-chip' : 'post-place-chip is-missing'}
                        onclick={() => (d.placing = !d.placing)}
                        title="Where it is about"
                        type="button"
                    >
                        {Icon('location_on')} {d.place ? (d.place.name ?? `${d.place.latitude.toFixed(4)}, ${d.place.longitude.toFixed(4)}`) : 'Add a place'}
                    </button>
                    <label class="post-tool" title="Add a photo">
                        {Icon('image')} Photo
                        <input
                            accept="image/*"
                            hidden
                            onchange={(e: Event) => {
                                const file = (e.target as HTMLInputElement).files?.[0]
                                if (file) void uploadPicture(file, d)
                            }}
                            type="file"
                        />
                    </label>
                    <button
                        aria-pressed={d.warning !== null}
                        class={d.warning !== null ? 'post-tool is-on' : 'post-tool'}
                        onclick={() => (d.warning = d.warning === null ? '' : null)}
                        title="Add a content warning"
                        type="button"
                    >
                        {Icon('warning')} Warning
                    </button>
                    <span class="post-count">{500 - d.text.length}</span>
                    <Button onclick={() => (st.draft = null)} size="sm" variant="ghost">Cancel</Button>
                    <Button disabled={!ready} onclick={() => void send()} size="sm">Post</Button>
                </div>
                <p class="post-reach">{reach}</p>
            </div>
        </div>
    )
}

function Settings(community: CommunityView, info: PublicInfo) {
    const id = community.id
    const first = info.settings.handle == null
    if (first && !st.handle) st.handle = suggestHandle(community.settings.name)
    const save = async (enabled: boolean, who: WhoPosts) => {
        try {
            const next = await signupApi.post<PublicInfo>(`/api/communities/${id}/public`, {
                enabled,
                handle: first ? st.handle.trim() : undefined,
                who_posts: who,
            })
            st.info.set(id, next)
            await load(id)
        } catch (e) {
            failed(e)
        }
    }
    return (
        <div class="post-settings">
            {first ? (
                <FieldText
                    help={`People elsewhere find it as @${st.handle.trim()}@${community.home}. It can't be changed later: their servers know the community by it.`}
                    label="The community's fediverse address"
                    model={{
                        get value() {
                            return st.handle
                        },
                        set value(v: string) {
                            st.handle = v
                        },
                    }}
                />
            ) : (
                <p><b>@{info.address}</b></p>
            )}
            <FieldSegmented
                label="Who can post"
                model={{
                    get value() {
                        return info.settings.who_posts
                    },
                    set value(w: WhoPosts) {
                        void save(info.settings.enabled, w)
                    },
                }}
                options={[
                    {label: 'Every member', value: 'members' as WhoPosts},
                    {label: 'Moderators only', value: 'moderators' as WhoPosts},
                ]}
                size="sm"
            />
            <Button onclick={() => void save(!info.settings.enabled, info.settings.who_posts)} size="sm" variant={info.settings.enabled ? 'ghost' : 'primary'}>
                {info.settings.enabled ? 'Switch public posts off' : 'Switch public posts on'}
            </Button>
            {info.settings.enabled && info.federating && (
                <>
                    <h3 class="chat-pane-h">This community follows</h3>
                    {info.follows.length === 0 && (
                        <p class="chat-hint">Nothing yet. Their posts would appear here, marked with where they came from.</p>
                    )}
                    {info.follows.map((f) => (
                        <div class="post-follow" key={f.address}>
                            <span class="is-public">{f.address}</span>
                            <span class="chat-sub">{f.kind}{f.accepted ? '' : ' · waiting for their server'}</span>
                            <button
                                onclick={async () => {
                                    try {
                                        st.info.set(id, await signupApi.delete<PublicInfo>(`/api/communities/${id}/follows/${encodeURIComponent(f.address)}`))
                                    } catch (e) {
                                        failed(e)
                                    }
                                    m.redraw()
                                }}
                                title="Stop following"
                                type="button"
                            >
                                {Icon('close')}
                            </button>
                        </div>
                    ))}
                    <form
                        class="chat-inline"
                        onsubmit={async (e: Event) => {
                            e.preventDefault()
                            const address = st.follow.trim()
                            if (!address.includes('@') && !address.startsWith('https://')) return
                            try {
                                st.info.set(id, await signupApi.post<PublicInfo>(`/api/communities/${id}/follows`, {address}))
                                st.follow = ''
                            } catch (err) {
                                failed(err)
                            }
                            m.redraw()
                        }}
                    >
                        <FieldText
                            model={{
                                get value() {
                                    return st.follow
                                },
                                set value(v: string) {
                                    st.follow = v
                                },
                            }}
                            placeholder="groningen@feddit.nl, someone@mastodon.social, or a relay's https:// address"
                        />
                        <Button size="sm" type="submit">Follow</Button>
                    </form>
                </>
            )}
        </div>
    )
}

/** What happened to the reader in public, and when it was last read. */
const notices = {
    list: null as PostNotices | null,
    readAt: 0,
}

/** How many notices are unread, for a badge; read again at most once a minute. */
export function unreadNotices(): number {
    if (Date.now() - notices.readAt > 60_000) {
        notices.readAt = Date.now()
        void loadNotices()
    }
    return notices.list?.unread ?? 0
}

async function loadNotices(): Promise<void> {
    try {
        notices.list = await signupApi.get<PostNotices>('/api/fediverse/notices')
        notices.readAt = Date.now()
    } catch {
        // Signed out, or the server is older than notices: no badge.
    }
    m.redraw()
}

const NOTICE_WORDS: Record<NoticeKind, [string, string]> = {
    boost: ['repeat', 'passed on your post'],
    follow: ['person_add', 'follows you'],
    like: ['favorite', 'likes'],
    mention: ['alternate_email', 'mentioned you'],
    reply: ['reply', 'replied'],
}

/**
 * Replies, mentions, likes, boosts and new followers, here and from other
 * servers, newest first. Opening the page marks them read; a notice about a
 * post opens that post's community with its replies showing.
 */
export function Notices(ctx: PostsCtx, open: (community: string, post: string) => void) {
    const list = notices.list
    if (!list) {
        void loadNotices()
        return <p class="chat-hint">Reading what happened…</p>
    }
    if (list.unread > 0) {
        const up_to = Math.max(...list.items.map((n) => n.id))
        list.unread = 0
        list.items.forEach((n) => (n.read = true))
        void signupApi.post('/api/fediverse/notices/read', {up_to}).catch(failed)
    }
    return (
        <div class="post-notices">
            <h2 class="chat-pane-h">{Icon('notifications')} Activity</h2>
            <p class="chat-hint">Replies, mentions, likes and followers on your public posts, here and on other servers.</p>
            {list.items.length === 0 && <p class="post-empty">Nothing yet.</p>}
            {list.items.map((n) => {
                const [icon, words] = NOTICE_WORDS[n.kind]
                const about = n.kind === 'like' && n.excerpt ? 'your post' : ''
                return (
                    <button
                        class={n.read ? 'post-notice' : 'post-notice is-unread'}
                        disabled={!n.post || !n.community}
                        key={n.id}
                        onclick={() => n.post && n.community && open(n.community, n.post)}
                        type="button"
                    >
                        <span class="post-face">{ctx.avatar(n.who.address, 32)}</span>
                        <span class="post-notice-text">
                            <span>
                                {Icon(icon)} <b>{n.who.name}</b> {words} {about}
                                <span class="post-dim"> · {ago(n.created_ms)}</span>
                            </span>
                            {n.excerpt && <span class="post-dim post-notice-excerpt">{n.excerpt}</span>}
                            {!n.who.local && <span class="post-dim is-public">@{n.who.address}</span>}
                        </span>
                    </button>
                )
            })}
        </div>
    )
}

/** Show a community's posts with one post's replies open under it. */
export function openPost(community: string, post: string): void {
    st.loaded = community
    st.tab.set(community, 'posts')
    st.settingsOpen = false
    st.error = ''
    st.draft = null
    st.threadOpen = null
    void load(community)
    void toggleThread(post)
}