💾 Archived View for iich.space › src › app › board › thread.ts captured on 2021-12-03 at 14:04:38.

View Raw

More Information

-=-=-=-=-=-=-

import { Status } from '@/gemini';
import { Handler, Router } from '@/mission-control';

import { TRUNCATED_REPLY_COUNT } from '~/constants';
import { getBoardByName, getThreadById } from '~/db/queries';
import { getDisplayIdentityFromRequest } from '~/util/identity';
import { createToken } from '~/util/tokens';
import ThreadPage from '~/views/ThreadPage';

const router = new Router<Handler>();

router.use('/post', (req, res, { params }) => {
  const types = (req.query || '').split(',');
  const host = types.includes('image') ? `titan://${req.url.host}` : '';
  const name = params.name;
  const id = params.id;
  const signed = types.includes('signed') ? 'signed' : 'anon';
  const token = createToken();
  const image = types.includes('image') ? 'image' : 'text';
  const url = [host, 'post', name, 'threads', id, signed, token, image].join(
    '/',
  );

  res.redirect(url);
});

router.use('/:full?', (req, res, { params, tb }) => {
  const full = params.full === 'full';
  const replyCount = full ? undefined : TRUNCATED_REPLY_COUNT;
  const board = getBoardByName(params.name);
  const thread = getThreadById(parseInt(params.id, 10), replyCount);
  const identity = getDisplayIdentityFromRequest(req);

  if (board === undefined || thread === undefined || thread.id === null) {
    res.sendStatus(Status.NOT_FOUND);
  } else {
    res.send(tb.include(ThreadPage, { board, identity, thread }));
  }

  res.end();
});

export default router;