Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add forum homepage #952

Merged
merged 17 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/boot/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import '../logout/logout.js';
import '../auth-facebook/auth-facebook';
import '../admin/admin.js';
import '../settings/settings.js';
import '../forum/forum.js';
import '../topic/topic.js';
import '../homepage/homepage.js';
import '../newsfeed/newsfeed.js';
import '../forum/forum.js';
import '../topic/topic.js';
// require('proposal');
// require('404');

Expand Down
7 changes: 1 addition & 6 deletions lib/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ app.use('/settings', require('lib/settings-api'));

app.use('/forgot', require('lib/forgot-api'));

/*
* Forums routes
*/

app.get('/forums/new', require('lib/forum'));

/*
* Stats routes
*/
Expand Down Expand Up @@ -201,4 +195,5 @@ app.use(require('lib/forgot'));
app.use(require('lib/help'));
app.use(require('lib/homepage'));
app.use(require('lib/topic'));
app.use(require('lib/forum'));
app.use(require('lib/404'));
2 changes: 1 addition & 1 deletion lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var defaultsPath = resolve(configPath, 'defaults.json');
var envPath = resolve(configPath, environment + '.json');

var defaultConfig = require(defaultsPath);
var localConfig = fs.existsSync(envPath) && require(envPath) || {};
var localConfig = fs.existsSync(envPath) && require(envPath) || {};
var config = {};

forEach(defaultConfig, parse);
Expand Down
67 changes: 14 additions & 53 deletions lib/db-api/forum.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@ var Log = require('debug');
var log = new Log('democracyos:db-api:forum');
var Forum = models.Forum;

exports.all = function all(fn) {
log('Looking for all forums');

Forum
.find({ deletedAt: null })
.populate('owner')
.select('id title summary url owner imageUrl')
.exec(function(err, forums) {
if (err) {
log('Found error: %s', err);
return fn(err);
}

log('Delivering forums %j', forums);

Forum.findOne({ deletedAt: null }).exec(function(_err) {
if (_err) {
log('Found error: %s', _err);
return fn(_err);
}

fn(null, { forums: forums });
});
});

return this;
};

exports.create = function create(data, fn) {
log('Creating new forum');

Expand All @@ -49,28 +21,16 @@ exports.create = function create(data, fn) {
}
};

exports.findByOwner = function findByOwner(owner, fn) {
log('Searching for forums whose owner is %s', owner);

Forum.find({ owner: owner })
.exec(function(err, forums) {
if (err) {
log('Found error: %j', err);
return fn(err);
}

log('Found %d forums', forums.length);
fn(null, forums);
});

return this;
exports.del = function del(forum, cb) {
log('Deleting forum');
forum.softdelete(cb);
};

exports.findOneByOwner = function findOneByOwner(owner, fn) {
log('Searching forum of owner %s', owner);

Forum
.where({ owner: owner })
.where({ owner: owner, deletedAt: null })
.findOne(function(err, forum) {
if (err) {
log('Found error: %j', err);
Expand All @@ -89,7 +49,9 @@ exports.findOneByOwner = function findOneByOwner(owner, fn) {
exports.findById = function findById(id, fn) {
log('Searching for forum with id %s', id);

Forum.findById(id, function(err, forum) {
Forum
.where({ deletedAt: null, id: id })
.findOne(id, function(err, forum) {
if (err) {
log('Found error: %j', err);
return fn(err);
Expand All @@ -109,8 +71,8 @@ exports.findOneByName = function findOneByName(name, fn) {
log('Searching for forum with name %s', name);

Forum
.where({ name: name })
.findOne(function(err, forum) {
.where({ deletedAt: null, name: name })
.findOne(function(err, forum) {
if (err) {
log('Found error: %j', err);
return fn(err);
Expand All @@ -126,14 +88,13 @@ exports.findOneByName = function findOneByName(name, fn) {
return this;
};

exports.count = function find(query, fn) {
Forum.count(query).exec(fn);
};

exports.exists = function exists(name, fn) {
name = normalize(name);
Forum.findOne({ name: name }, function(err, forum) {
return err ? fn(err) : fn(null, !!forum);
Forum
.find({ deletedAt: null, name: name })
.limit(1)
.exec(function(err, forum) {
return fn(err, !!(forum && forum.length));
});
};

Expand Down
16 changes: 11 additions & 5 deletions lib/forum-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var log = new Log('democracyos:forum');
var app = module.exports = express();

app.get('/forum/mine', restrict, function(req, res) {
if (!req.isAuthenticated()) return res.status(404).send();

api.forum.findOneByOwner(req.user.id, function(err, forum) {
if (err) return _handleError(err, req, res);

Expand Down Expand Up @@ -54,14 +56,18 @@ app.post('/forum', restrict, maintenance, function(req, res) {
});
});

app.delete('/forum/:id', restrict, maintenance, function(req, res) {
api.forum.findById(req.params.id, function(err, forum) {
app.del('/forum/:name', restrict, maintenance, function(req, res) {
api.forum.findOneByName(req.params.name, function(err, forum) {
if (err) return _handleError(err, req, res);
if (!forum) return _handleError('The user doesnt have any forum.', req, res);
if (!forum) return _handleError('Forum not found.', req, res);

if (forum.owner.toString() !== req.user._id.toString()) {
return res.status(401).send();
}

log('Trying to delete forum: %o', forum);
log('Trying to delete forum: %s', forum.id);

forum.remove(function(_err) {
api.forum.del(forum, function(_err) {
if (_err) return res.status(500).json(_err);
res.status(200).send();
});
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions lib/forum/styles.styl → lib/forum-form/styles.styl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
line-height 1.5
margin 15px 0


body.forum-new
background #ccc

.forum-new-form
fieldset
margin-top 1px
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions lib/forum-middlewares/forum-middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import debug from 'debug';
import page from 'page';
import forumStore from '../forum-store/forum-store';

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

/**
* Load from ':forum' param, and set ctx.forum.
*/

export function getForum (ctx, next) {
if (!ctx.params.forum) return next();

forumStore.get(ctx.params.forum)
.then(forum => {
ctx.forum = forum;
log(`setted ctx.forum with '${forum.name}'.`);
next();
})
.catch(err => {
if (404 === err.status) {
log(`forum not found '${ctx.params.forum}'.`);
return next();
}
log('Found error %s', err);
});
}

/**
* Load of logged in user, and set ctx.userForum.
*/

export function getUserForum (ctx, next) {
forumStore.getUserForum()
.then(userForum => {
ctx.userForum = userForum;
log(`setted ctx.userForum with '${userForum.name}'.`);
next();
})
.catch(err => {
if (404 === err.status) return next();
log('Found error %s', err);
});
}

/**
* Dont let in users that already have a forum.
*/

export function restrictUserWithForum (ctx, next) {
forumStore.getUserForum()
.then(() => page('/'))
.catch(err => {
if (404 === err.status) return next();
log('Found error %s', err);
});
}
11 changes: 11 additions & 0 deletions lib/forum-router/create-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var prefix = '/:forum';

function createRouter(config) {
var singleForum = config.singleForum;
return function forumRouter(route) {
if (singleForum) return route;
return prefix + ('/' === route ? '' : route);
};
}

module.exports = createRouter;
4 changes: 4 additions & 0 deletions lib/forum-router/forum-router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import config from '../config/config.js';
import createRouter from './create-router';

export default createRouter(config);
4 changes: 4 additions & 0 deletions lib/forum-router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var config = require('lib/config');
var createRouter = require('./create-router');

module.exports = createRouter(config);
49 changes: 20 additions & 29 deletions lib/forum-store/forum-store.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import bus from 'bus';
import Store from '../store/store';
import Log from 'debug';

const log = new Log('democracyos:forum-store');

class ForumStore extends Store {

constructor () {
super();
this.getFromParamsMiddleware = this.getFromParamsMiddleware.bind(this);
this._userForumName = null;

bus.on('logout', this.unloadUserForum.bind(this));
}
Expand All @@ -26,40 +23,34 @@ class ForumStore extends Store {
return forum;
}

/**
* Get the Forum of the current user
*
* @return {Promise} fetch
* @api public
*/
getUserForum () {
return this.get('mine');
let name = this._userForumName;
if (name && this.items.get(name)) return Promise.resolve(this.items.get(name));

if (this._fetches.get('mine')) return this._fetches.get('mine');

let fetch = this._fetch('mine')
.then(item => {
this._userForumName = item.name;
this.items.set(item.name, item);
bus.emit(`${this.name()}:update:${item.name}`, item);
})
.catch(err => {
this.log('Found error', err);
});

return fetch;
}

unloadUserForum () {
return this.unload('mine');
return this.unload(this._userForumName);
}

destroyUserForum () {
return this.destroy('mine');
return this.destroy(this._userForumName);
}

/**
* Middleware to load forum from current page url, gets it from '/:forum'.
*
* @return {Middleware}
* @api public
*/
getFromParamsMiddleware (ctx, next) {
const name = ctx.params.forum;

this.get(name)
.then(forum => {
ctx.forum = forum;
next();
})
.catch(err => log('Found error %s', err));
}

}

const forumStore = new ForumStore();
Expand Down
Loading