import debug from 'debug';
import page from 'page';
import dom from 'component-dom';
import t from 't-component';
import config from '../config/config';
import { findForum } from '../forum-middlewares/forum-middlewares';
import forumRouter from '../forum-router/forum-router';
import title from '../title/title';
import user from '../user/user';
import ForumForm from '../forum-form/forum-form';
import { verifyForumAccess } from './not-allowed/not-allowed';

const log = debug('democracyos:forum');

page('/forums/new', onlyAllowOnFirstSetup, user.required, () => {
  log('render /forums/new');

  title(t('forum.new.title'));
  document.body.classList.add('forum-new');

  let section = dom('section#content.site-content').empty();
  let view = new ForumForm();

  view.appendTo(section[0]);
});

page.exit('/forums/new', (ctx, next) => {
  document.body.classList.remove('forum-new');
  next();
});

page(forumRouter(), setDefaultForum, findForum, verifyForumAccess);
page(forumRouter('*'), setDefaultForum, findForum, verifyForumAccess);

function onlyAllowOnFirstSetup (ctx, next) {
  if (config.multiForum) return next();
  if (!config.defaultForum) return next();
  page.redirect('/');
}

function setDefaultForum (ctx, next) {
  if (config.multiForum) return next();

  if (config.defaultForum) {
    ctx.params.forum = config.defaultForum;
    return next();
  }

  page.redirect('/forums/new');
}

@@ -513,16 +513,31 @@
 ms/new',
+ user.required,
  onlyAll
@@ -551,31 +551,16 @@
 stSetup,
- user.required,
  () =%3E %7B
@@ -913,16 +913,19 @@
 mRouter(
+'/'
 ), setDe
@@ -1124,16 +1124,89 @@
 next();%0A
+  if (!config.defaultForum && !user.staff) return page.redirect('/404');%0A
   if (!c

update first single forum pemission