From ed1223645e8187dd7c38b85657aeedbc1d046d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 28 Sep 2019 22:44:13 -0400 Subject: [PATCH] feat: send notification to group owners when someone leaves --- public/language/en-GB/groups.json | 1 + src/groups/invite.js | 4 +--- src/socket.io/groups.js | 12 ++++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/public/language/en-GB/groups.json b/public/language/en-GB/groups.json index abb34947cc..d165c4a71b 100644 --- a/public/language/en-GB/groups.json +++ b/public/language/en-GB/groups.json @@ -60,6 +60,7 @@ "membership.invitation-pending": "Invitation Pending", "membership.join-group": "Join Group", "membership.leave-group": "Leave Group", + "membership.leave.notification_title": "%1 has left group %2", "membership.reject": "Reject", "new-group.group_name": "Group Name:", diff --git a/src/groups/invite.js b/src/groups/invite.js index 93b69c20e5..df1263441d 100644 --- a/src/groups/invite.js +++ b/src/groups/invite.js @@ -45,9 +45,7 @@ module.exports = function (Groups) { groupNames = [groupNames]; } const sets = []; - groupNames.forEach(function (groupName) { - sets.push('group:' + groupName + ':pending', 'group:' + groupName + ':invited'); - }); + groupNames.forEach(groupName => sets.push('group:' + groupName + ':pending', 'group:' + groupName + ':invited')); await db.setsRemove(sets, uid); }; diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js index b533f8ecba..fa88cd9e24 100644 --- a/src/socket.io/groups.js +++ b/src/socket.io/groups.js @@ -1,11 +1,13 @@ 'use strict'; +const validator = require('validator'); const groups = require('../groups'); const meta = require('../meta'); const user = require('../user'); const utils = require('../utils'); const events = require('../events'); const privileges = require('../privileges'); +const notifications = require('../notifications'); const SocketGroups = module.exports; @@ -74,6 +76,16 @@ SocketGroups.leave = async (socket, data) => { } await groups.leave(data.groupName, socket.uid); + const username = await user.getUserField(socket.uid, 'username'); + const notification = await notifications.create({ + type: 'group-leave', + bodyShort: '[[groups:membership.leave.notification_title, ' + username + ', ' + data.groupName + ']]', + nid: 'group:' + validator.escape(data.groupName) + ':uid:' + socket.uid + ':group-leave', + path: '/groups/' + utils.slugify(data.groupName), + }); + const uids = await groups.getOwners(data.groupName); + await notifications.push(notification, uids); + logGroupEvent(socket, 'group-leave', { groupName: data.groupName, });