From 917f76feab7a3e74192fc87f8e0e5b9e333ff147 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 7 Jan 2015 14:58:31 -0500 Subject: [PATCH] group join/leave controls in groups list page (+ helper!) --- public/src/helpers.js | 14 ++++++++++++++ src/controllers/groups.js | 3 ++- src/groups.js | 20 +++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/public/src/helpers.js b/public/src/helpers.js index a89aeae0e2..38875ea8b8 100644 --- a/public/src/helpers.js +++ b/public/src/helpers.js @@ -17,6 +17,19 @@ return ''; }; + // Groups helpers + helpers.membershipBtn = function(groupObj) { + if (groupObj.isMember) { + return ''; + } else { + if (groupObj.pending) { + return ''; + } else { + return ''; + } + } + }; + if ('undefined' !== typeof window) { $(document).ready(module.exports); } @@ -26,6 +39,7 @@ templates.registerHelper('displayUsersLink', helpers.displayUsersLink); templates.registerHelper('buildMetaTag', helpers.buildMetaTag); + templates.registerHelper('membershipBtn', helpers.membershipBtn); }; })('undefined' === typeof module ? { diff --git a/src/controllers/groups.js b/src/controllers/groups.js index 3843672a09..72d83d4fc2 100644 --- a/src/controllers/groups.js +++ b/src/controllers/groups.js @@ -9,7 +9,8 @@ var groups = require('../groups'), groupsController.list = function(req, res, next) { groups.list({ truncateUserList: true, - expand: true + expand: true, + uid: req.user ? req.user.uid : 0 }, function(err, groups) { if (err) { return next(err); diff --git a/src/groups.js b/src/groups.js index 47e7002db9..4f59d39820 100644 --- a/src/groups.js +++ b/src/groups.js @@ -71,7 +71,25 @@ var async = require('async'), groupNames = groupNames.concat(ephemeralGroups); async.map(groupNames, function (groupName, next) { - Groups.get(groupName, options, next); + async.waterfall([ + async.apply(Groups.get, groupName, options), + function(groupObj, next) { + // Retrieve group membership state, if uid is passed in + if (!options.uid) { + return next(null, groupObj); + } + + Groups.isMember(options.uid, groupName, function(err, isMember) { + if (err) { + winston.warn('[groups.list] Could not determine membership in group `' + groupName + '` for uid `' + options.uid + '`: ' + err.message); + return next(null, groupObj); + } + + groupObj.isMember = isMember; + next(null, groupObj); + }); + } + ], next); }, function (err, groups) { callback(err, internals.filterGroups(groups, options)); });