~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
main
47 lines · 1.9 KB Raw History
import type * as getViewer from '#lib/lexicons/org/atgrp/internal/getViewer.ts';
import type * as describeServer from '#lib/lexicons/com/atproto/server/describeServer.ts';
import type { Actions, PageServerLoad } from './$types';
import { fail, redirect } from '@sveltejs/kit';
import { xrpc } from '#lib/data.ts';
import { finished } from '#lib/handles.ts';
import { refused } from '#lib/errors.ts';

type Event = Parameters<PageServerLoad>[0];

/** The domain this host names its groups under. */
async function domainOf(event: Pick<Event, 'fetch'>) {
  const described = await xrpc<typeof describeServer.main>(
    event,
    'com.atproto.server.describeServer',
    {}
  );
  return described.availableUserDomains[0];
}

/** Who is starting a group, whether this host lets them, and the domain its address is under. */
export const load: PageServerLoad = async (event) => {
  const { actor } = await xrpc<typeof getViewer.main>(event, 'org.atgrp.internal.getViewer', {});
  return { actor: actor ?? null, domain: await domainOf(event) };
};

/** Starts the group, its founder the person signed in, and goes to its page. */
export const actions: Actions = {
  default: async (event) => {
    const form = await event.request.formData();
    const text = (k: string) => String(form.get(k) ?? '').trim();
    const input = {
      // As the form made it, and again for a page drawn without script.
      handle: `${finished(text('label'))}${await domainOf(event)}`,
      displayName: text('displayName'),
      description: text('description') || undefined,
      joinPolicy: text('joinPolicy') || undefined
    };
    const res = await event.fetch('/xrpc/org.atgrp.internal.createGroup', {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify(input)
    });
    if (!res.ok) return fail(res.status, { failed: await refused(res) });
    redirect(303, `/g/${input.handle}`);
  }
};