💾 Archived View for iich.space › src › app › admin › posts.ts captured on 2022-01-08 at 14:18:00.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

🚧 View Differences

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

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

import {
  createBan,
  getAdminPosts,
  removeBan,
  setPostDeleted,
  setPostLocked,
  setPostMuted,
  setPostThreadId,
} from '~/db/admin/queries';
import { hasBan } from '~/db/queries';
import withQuery from '~/middleware/withQuery';
import AdminPostsPage from '~/views/admin/AdminPostsPage';

const router = new Router<Handler>();

router.use('/', (req, res, { tb }) => {
  const page = req.query ? parseInt(req.query, 10) : 0;
  const posts = getAdminPosts(page);

  res.send(tb.include(AdminPostsPage, { page, posts }));
  res.end();
});

router.use('/ban/:origin', (req, res, { params }) => {
  const origin = decodeURIComponent(params.origin!);
  const page = req.query ? parseInt(req.query, 10) : 0;

  if (hasBan(origin)) {
    removeBan(origin);
  } else {
    createBan(origin);
  }

  res.redirect(`/admin/posts?${page}`);
});

router.use('/lock/:id/:toggle', (req, res, { params }) => {
  const id = parseInt(params.id, 10);
  const page = req.query ? parseInt(req.query, 10) : 0;

  setPostLocked(id, params.toggle === 'lock');

  res.redirect(`/admin/posts?${page}`);
});

router.use('/delete/:id/:toggle', (req, res, { params }) => {
  const id = parseInt(params.id, 10);
  const page = req.query ? parseInt(req.query, 10) : 0;

  setPostDeleted(id, params.toggle === 'remove');

  res.redirect(`/admin/posts?${page}`);
});

router.use('/mute/:id/:toggle', (req, res, { params }) => {
  const id = parseInt(params.id, 10);
  const page = req.query ? parseInt(req.query, 10) : 0;

  setPostMuted(id, params.toggle === 'mute');

  res.redirect(`/admin/posts?${page}`);
});

router.use('/move/:id', withQuery('thread id'), (req, res, { params }) => {
  const id = parseInt(params.id, 10);
  const threadId = parseInt(req.query!, 10);

  setPostThreadId(id, threadId);

  res.redirect(`/admin/posts`);
});

export default router;