From 3baad542237d2561112cf570b40cdc0c4e0b63e8 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 9 Sep 2015 19:57:12 -0400 Subject: [PATCH] Close #3578 This commit introduces notification creation if a user requests membership to a group (in which case notifications are sent to all group owners), and user invites to a group (in which case a notification is sent to the invitee). --- public/language/en_GB/groups.json | 4 ++++ src/groups.js | 4 ++++ src/groups/membership.js | 40 +++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/public/language/en_GB/groups.json b/public/language/en_GB/groups.json index 958efd13e0..d800d74b1c 100644 --- a/public/language/en_GB/groups.json +++ b/public/language/en_GB/groups.json @@ -14,6 +14,10 @@ "invited.none": "There are no invited members at this time", "invited.uninvite": "Rescind Invitation", "invited.search": "Search for a user to invite to this group", + "invited.notification_title": "You have been invited to join %1", + + "request.notification_title": "Group Membership Request from %1", + "request.notification_text": "%1 has requested to become a member of %2", "cover-instructions": "Drag and Drop a photo, drag to position, and hit Save", "cover-change": "Change", diff --git a/src/groups.js b/src/groups.js index 13bae53e5f..24ee5a9fd8 100644 --- a/src/groups.js +++ b/src/groups.js @@ -197,6 +197,10 @@ var async = require('async'), }); }; + Groups.getOwners = function(groupName, callback) { + db.getSetMembers('group:' + groupName + ':owners', callback); + }; + Groups.getOwnersAndMembers = function(groupName, uid, start, stop, callback) { async.parallel({ owners: function (next) { diff --git a/src/groups/membership.js b/src/groups/membership.js index 51ee633787..c8cf9a9871 100644 --- a/src/groups/membership.js +++ b/src/groups/membership.js @@ -5,7 +5,9 @@ var async = require('async'), _ = require('underscore'), user = require('../user'), + utils = require('../../public/src/utils'), plugins = require('../plugins'), + notifications = require('../notifications'), db = require('./../database'); module.exports = function(Groups) { @@ -66,7 +68,29 @@ module.exports = function(Groups) { }; Groups.requestMembership = function(groupName, uid, callback) { - inviteOrRequestMembership(groupName, uid, 'request', callback); + async.waterfall([ + async.apply(inviteOrRequestMembership, groupName, uid, 'request'), + function(next) { + user.getUserField(uid, 'username', function(err, username) { + if (err) { + return next(err); + } + next(null, { + bodyShort: '[[groups:request.notification_title, ' + username + ']]', + bodyLong: '[[groups:request.notification_text, ' + username + ', ' + groupName + ']]', + nid: 'group:' + groupName + ':uid:' + uid + ':request', + path: '/groups/' + utils.slugify(groupName) + }); + }); + }, + async.apply(notifications.create), + function(notification, next) { + Groups.getOwners(groupName, function(err, ownerUids) { + next(null, notification, ownerUids); + }); + }, + async.apply(notifications.push) + ], callback); }; Groups.acceptMembership = function(groupName, uid, callback) { @@ -87,7 +111,19 @@ module.exports = function(Groups) { }; Groups.invite = function(groupName, uid, callback) { - inviteOrRequestMembership(groupName, uid, 'invite', callback); + async.waterfall([ + async.apply(inviteOrRequestMembership, groupName, uid, 'invite'), + async.apply(notifications.create, { + bodyShort: '[[groups:invited.notification_title, ' + groupName + ']]', + bodyLong: '', + nid: 'group:' + groupName + ':uid:' + uid + ':invite', + path: '/groups/' + utils.slugify(groupName) + }), + function(notification, next) { + next(null, notification, [uid]); + }, + async.apply(notifications.push) + ], callback); }; function inviteOrRequestMembership(groupName, uid, type, callback) {