import m, {MithrilComponent} from '@garage44/mithril' import {$s, signupApi} from '@/app' import {Card, FollowFrom, load, readOf, type PostsCtx} from '@/components/pages/chat/posts' import {ApiError} from '@/lib/api' import type {PersonPosts, PublicInfo} from '@/types' /** * The public pages: a community's posts (`/c/{handle}`) and a person's * (`/@{name}`), for anybody, signed in or not (`docs/notes/fediverse.md`). * * They are where other servers send people. A community's actor and a * person's name `url` these pages, so "Open original" on Mastodon and the * profile link on Lemmy land on what was written here, with a box to follow it * from the reader's own server, rather than on the site's front door. The * cards are the community page's own (`chat/posts.tsx`), drawn with what a * reader with no session can see: pictures from `/ap/media/`, the public copy * every public picture has, and faces as initials. */ const ctx: PostsCtx = { avatar: (address: string, size: number) => ( {address.slice(0, 1).toUpperCase()} ), // A post's place opens the viewer there, as a place link does. onMap: (place: string) => location.assign(`/go?place=${place}`), pictureUrl: (hash) => (hash ? `/ap/media/${hash}` : null), } function said(e: unknown): string { if (e instanceof ApiError && e.status === 404) return 'There is nothing public here.' return e instanceof Error ? e.message : String(e) } /** A community's public half, by its handle. */ export class PublicCommunity extends MithrilComponent<{handle: string}> { id: string | null = null error = '' asked = '' async read(handle: string) { this.asked = handle this.id = null this.error = '' try { const info = await signupApi.get(`/api/communities/${encodeURIComponent(handle)}/public`) if (!info.community) throw new ApiError(404, 'not public') this.id = info.community.id await load(this.id) } catch (e) { this.error = said(e) } m.redraw() } view(vnode: m.Vnode<{handle: string}>) { const handle = vnode.attrs.handle if (this.asked !== handle) void this.read(handle) if (this.error) return

{this.error}

const {info, feed} = this.id ? readOf(this.id) : {feed: [], info: undefined} const c = info?.community if (!info || !c || !this.id) return

Reading…

const where = {id: this.id, mod: info.may_moderate, signedIn: !!$s.signup.token} return (
{c.picture ? : {c.name.slice(0, 1)}}

{c.name}

@{info.address} · {info.followers} follower{info.followers === 1 ? '' : 's'} on the fediverse
{c.about &&

{c.about}

} {info.address && info.federating && FollowFrom(info.address, true)} {feed.length === 0 &&

Nothing posted yet.

} {feed.map((p) => Card(where, p, ctx, true))}
) } } /** A person's public posts, by their name. */ export class PublicPerson extends MithrilComponent<{name: string}> { page: PersonPosts | null = null error = '' asked = '' async read(name: string) { this.asked = name this.page = null this.error = '' try { this.page = await signupApi.get(`/api/posts?author=${encodeURIComponent(name)}`) } catch (e) { this.error = said(e) } m.redraw() } view(vnode: m.Vnode<{name: string}>) { const name = decodeURIComponent(vnode.attrs.name) if (this.asked !== name) void this.read(name) if (this.error) return

{this.error}

const page = this.page if (!page) return

Reading…

const signedIn = !!$s.signup.token return (
{page.picture ? : {name.slice(0, 1).toUpperCase()}}

{page.address.split('@')[0]}

@{page.address}
{FollowFrom(page.address, false)} {page.posts.length === 0 &&

No public posts yet.

} {page.posts.map((p) => Card({id: p.community, mod: false, signedIn}, p, ctx, true))}
) } }