From 96c37c25b05e485f3852a67a329b57234dc0313c Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 8 Jan 2015 17:23:40 -0500 Subject: [PATCH] join, leave, accept, reject: handlers + UI, #2588 --- public/src/client/groups/details.js | 24 +++++++++++++++++++++--- src/groups.js | 7 ++++++- src/socket.io/groups.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/public/src/client/groups/details.js b/public/src/client/groups/details.js index af3bf54afa..15291cb9d8 100644 --- a/public/src/client/groups/details.js +++ b/public/src/client/groups/details.js @@ -1,15 +1,15 @@ "use strict"; -/* globals socket, ajaxify */ +/* globals define, socket, ajaxify, app */ define('forum/groups/details', function() { var Details = {}; Details.init = function() { - var memberList = $('.groups .members'); + var detailsPage = $('.groups'); $('.latest-posts .content img').addClass('img-responsive'); - memberList.on('click', '[data-action]', function() { + detailsPage.on('click', '[data-action]', function() { var btnEl = $(this), userRow = btnEl.parents('tr'), ownerFlagEl = userRow.find('.member-name i'), @@ -25,6 +25,24 @@ define('forum/groups/details', function() { }, function(err) { if (!err) { ownerFlagEl.toggleClass('invisible'); + } else { + app.alertError(err); + } + }); + break; + + case 'join': // intentional fall-throughs! + case 'leave': + case 'accept': + case 'reject': + socket.emit('groups.' + action, { + toUid: uid, + groupName: ajaxify.variables.get('group_name') + }, function(err) { + if (!err) { + ajaxify.refresh(); + } else { + app.alertError(err); } }); break; diff --git a/src/groups.js b/src/groups.js index f6f619ff87..6286fe7a9d 100644 --- a/src/groups.js +++ b/src/groups.js @@ -558,12 +558,17 @@ var async = require('async'), }); }; - Groups.approveMembership = function(groupName, uid, callback) { + Groups.acceptMembership = function(groupName, uid, callback) { // Note: For simplicity, this method intentially doesn't check the caller uid for ownership! db.setRemove('group:' + groupName + ':pending', uid, callback); Groups.join.apply(Groups, arguments); }; + Groups.rejectMembership = function(groupName, uid, callback) { + // Note: For simplicity, this method intentially doesn't check the caller uid for ownership! + db.setRemove('group:' + groupName + ':pending', uid, callback); + }; + Groups.leave = function(groupName, uid, callback) { callback = callback || function() {}; diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js index 366a12348f..039ad42b89 100644 --- a/src/socket.io/groups.js +++ b/src/socket.io/groups.js @@ -59,4 +59,32 @@ SocketGroups.rescind = function(socket, data, callback) { }); }; +SocketGroups.accept = function(socket, data, callback) { + if (!data) { + return callback(new Error('[[error:invalid-data]]')); + } + + groups.ownership.isOwner(socket.uid, data.groupName, function(err, isOwner) { + if (!isOwner) { + return callback(new Error('[[error:no-privileges]]')); + } + + groups.acceptMembership(data.groupName, data.toUid, callback); + }); +}; + +SocketGroups.reject = function(socket, data, callback) { + if (!data) { + return callback(new Error('[[error:invalid-data]]')); + } + + groups.ownership.isOwner(socket.uid, data.groupName, function(err, isOwner) { + if (!isOwner) { + return callback(new Error('[[error:no-privileges]]')); + } + + groups.rejectMembership(data.groupName, data.toUid, callback); + }); +}; + module.exports = SocketGroups;