💾 Archived View for iich.space › src › app › board › index.ts captured on 2022-03-01 at 16:03:09.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

🚧 View Differences

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

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

import { REPLIES_PER_THREAD, THREADS_PER_PAGE } from '~/constants';
import {
  getBoardByName,
  getBoardStatsById,
  getThreadsByBoardId,
} from '~/db/queries';
import { getDisplayIdentityFromRequest } from '~/util/identity';
import { createToken } from '~/util/tokens';
import BoardPage from '~/views/BoardPage';

import thread from './thread';

const router = new Router<Handler>();

router.use('/threads/:id', thread);

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 signed = types.includes('signed') ? 'signed' : 'anon';
  const token = createToken();
  const image = types.includes('image') ? 'image' : 'text';
  const url = [host, 'post', name, signed, token, image].join('/');

  res.redirect(url);
});

router.use('/:page?', (req, res, { params, tb }) => {
  const board = getBoardByName(params.name);

  if (board === undefined) {
    res.sendStatus(Status.NOT_FOUND);
    res.end();
    return;
  }

  const boardStats = getBoardStatsById(board.id);
  const page = parseInt(params.page || '1', 10);
  const threads = getThreadsByBoardId(
    board.id,
    REPLIES_PER_THREAD,
    page - 1,
    THREADS_PER_PAGE,
  );
  const identity = getDisplayIdentityFromRequest(req);

  res.send(
    tb.include(BoardPage, {
      board,
      boardStats,
      identity,
      page,
      repliesPerThread: REPLIES_PER_THREAD,
      threads,
      threadsPerPage: THREADS_PER_PAGE,
    }),
  );
  res.end();
});

export default router;