Files
NodeBB/src/controllers/groups.js

108 lines
2.4 KiB
JavaScript
Raw Normal View History

2014-05-22 15:23:19 -04:00
"use strict";
var async = require('async'),
2014-07-31 23:16:12 -04:00
nconf = require('nconf'),
meta = require('../meta'),
groups = require('../groups'),
user = require('../user'),
2014-12-01 20:28:36 -05:00
helpers = require('./helpers'),
2014-05-22 15:23:19 -04:00
groupsController = {};
2014-10-27 01:49:57 -04:00
groupsController.list = function(req, res, next) {
2015-05-26 15:37:33 -04:00
groups.list(req.uid, 0, -1, function(err, groups) {
2014-10-27 01:49:57 -04:00
if (err) {
return next(err);
}
groups = groups.filter(function(group) {
return group && !group.hidden && !group.system;
});
2014-05-22 15:23:19 -04:00
res.render('groups/list', {
groups: groups,
allowGroupCreation: parseInt(meta.config.allowGroupCreation, 10) === 1
2014-05-22 15:23:19 -04:00
});
});
};
2014-12-01 20:28:36 -05:00
groupsController.details = function(req, res, next) {
async.waterfall([
async.apply(groups.exists, res.locals.groupName),
function(exists, next) {
if (!exists) {
return next(undefined, null);
}
// Ensure the group isn't hidden either
groups.isHidden(res.locals.groupName, next);
},
function(hidden, next) {
if (hidden === null) { return next(undefined, false); } // Group didn't exist, not ok
if (!hidden) {
next(null, true);
} else {
// If not, only members are granted access
2015-02-23 16:31:18 -05:00
async.parallel([
async.apply(groups.isMember, req.uid, res.locals.groupName),
async.apply(groups.isInvited, req.uid, res.locals.groupName)
2015-02-23 16:31:18 -05:00
], function(err, checks) {
next(err, checks[0] || checks[1]);
});
}
}
], function(err, ok) {
2015-03-09 18:22:44 -04:00
if (err) {
return next(err);
}
2014-12-01 20:28:36 -05:00
2015-03-09 18:22:44 -04:00
if (!ok) {
return helpers.redirect(res, '/groups');
2015-02-06 20:42:20 -05:00
}
2015-03-09 18:22:44 -04:00
async.parallel({
group: function(next) {
groups.get(res.locals.groupName, {
uid: req.uid
2015-03-09 18:22:44 -04:00
}, next);
},
posts: function(next) {
groups.getLatestMemberPosts(res.locals.groupName, 10, req.uid, next);
2015-03-09 18:22:44 -04:00
}
}, function(err, results) {
if (err) {
return next(err);
}
if (!results.group) {
return helpers.notFound(req, res);
}
res.render('groups/details', results);
});
2014-05-22 21:17:48 -04:00
});
};
groupsController.members = function(req, res, next) {
async.waterfall([
function(next) {
groups.getGroupNameByGroupSlug(req.params.slug, next);
},
function(groupName, next) {
user.getUsersFromSet('group:' + groupName + ':members', req.uid, 0, 49, next);
},
], function(err, users) {
if (err) {
return next(err);
}
res.render('groups/members', {
users: users,
nextStart: 50,
loadmore_display: users.length > 50 ? 'block' : 'hide',
});
});
};
2014-05-22 15:23:19 -04:00
module.exports = groupsController;