From 3350a89791be91a05a6fc373bcf4485a2e2baf3b Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Tue, 25 Apr 2017 15:27:12 -0600 Subject: [PATCH 1/6] Allow aborting notification push --- src/notifications.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/notifications.js b/src/notifications.js index 147ae7fea4..b7000dc124 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -191,6 +191,10 @@ var utils = require('./utils'); plugins.fireHook('filter:notification.push', { notification: notification, uids: uids }, next); }, function (data, next) { + if (!data || !data.notification) { + return callback(); + } + uids = data.uids; notification = data.notification; From 42e21d5aa985f50e7d2749f93fc8cd7577494ef3 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Wed, 26 Apr 2017 10:45:40 -0600 Subject: [PATCH 2/6] Add more messaging hooks --- src/messaging.js | 19 ++++++++++++++++--- src/messaging/data.js | 12 ++++++++++++ src/messaging/notifications.js | 10 ++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/messaging.js b/src/messaging.js index aa16cd0b36..c7e13e72bb 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -172,6 +172,14 @@ Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) { next(null, { rooms: results.roomData, nextStart: stop + 1 }); }, + function (ref, next) { + plugins.fireHook('filter:messaging.getRecentChats', { + rooms: ref.rooms, + nextStart: ref.nextStart, + uid: uid, + callerUid: callerUid, + }, next); + }, ], callback); }; @@ -252,11 +260,16 @@ Messaging.canMessageUser = function (uid, toUid, callback) { }, next); }, function (results, next) { - if (!results.settings.restrictChat || results.isAdmin || results.isFollowing) { - return next(); + if (results.settings.restrictChat && !results.isAdmin && !results.isFollowing) { + return next(new Error('[[error:chat-restricted]]')); } - next(new Error('[[error:chat-restricted]]')); + plugins.fireHook('filter:messaging.canMessageUser', { + uid: uid, + toUid: toUid, + }, function (err) { + next(err); + }); }, ], callback); }; diff --git a/src/messaging/data.js b/src/messaging/data.js index 3805b1c6f1..6a3a7b03a4 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -6,6 +6,7 @@ var S = require('string'); var db = require('../database'); var user = require('../user'); var utils = require('../utils'); +var plugins = require('../plugins'); module.exports = function (Messaging) { Messaging.getMessageField = function (mid, field, callback) { @@ -128,6 +129,17 @@ module.exports = function (Messaging) { next(null, []); } }, + function (messages, next) { + plugins.fireHook('filter:messaging.getMessagesData', { + messages: messages, + uid: uid, + roomId: roomId, + isNew: isNew, + mids: mids, + }, function (err, data) { + next(err, data && data.messages); + }); + }, ], callback); }; }; diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index eb7a1a1a74..564ea01fee 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -9,6 +9,7 @@ var emailer = require('../emailer'); var notifications = require('../notifications'); var meta = require('../meta'); var sockets = require('../socket.io'); +var plugins = require('../plugins'); module.exports = function (Messaging) { Messaging.notifyQueue = {}; // Only used to notify a user of a new chat message, see Messaging.notifyUser @@ -27,6 +28,15 @@ module.exports = function (Messaging) { message: messageObj, }; + plugins.fireHook('filter:messaging.notifyUsersInRoom', data, next); + }, + function (data, next) { + if (!data || !data.uids || !data.uids.length) { + return next(); + } + + var uids = data.uids; + uids.forEach(function (uid) { data.self = parseInt(uid, 10) === parseInt(fromUid, 10) ? 1 : 0; Messaging.pushUnreadCount(uid); From 19d63862379fdeface4db0ed6c9ec4edf6d6f31c Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Wed, 26 Apr 2017 10:48:08 -0600 Subject: [PATCH 3/6] Also abort if no uids to push to --- src/notifications.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/notifications.js b/src/notifications.js index b7000dc124..057a86e674 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -191,7 +191,7 @@ var utils = require('./utils'); plugins.fireHook('filter:notification.push', { notification: notification, uids: uids }, next); }, function (data, next) { - if (!data || !data.notification) { + if (!data || !data.notification || !data.uids || !data.uids.length) { return callback(); } From 033ce02e7bada2158b004f3e2620d3ec957f3e00 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Wed, 26 Apr 2017 15:22:17 -0600 Subject: [PATCH 4/6] Rename hooks Add `static:messaging.canMessageRoom` as well --- src/messaging.js | 9 +++++++-- src/messaging/data.js | 2 +- src/messaging/notifications.js | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/messaging.js b/src/messaging.js index c7e13e72bb..8864368c11 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -264,7 +264,7 @@ Messaging.canMessageUser = function (uid, toUid, callback) { return next(new Error('[[error:chat-restricted]]')); } - plugins.fireHook('filter:messaging.canMessageUser', { + plugins.fireHook('static:messaging.canMessageUser', { uid: uid, toUid: toUid, }, function (err) { @@ -306,7 +306,12 @@ Messaging.canMessageRoom = function (uid, roomId, callback) { return next(new Error('[[error:email-not-confirmed-chat]]')); } - next(); + plugins.fireHook('static:messaging.canMessageRoom', { + uid: uid, + roomId: roomId, + }, function (err) { + next(err); + }); }, ], callback); }; diff --git a/src/messaging/data.js b/src/messaging/data.js index 6a3a7b03a4..b565294902 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -130,7 +130,7 @@ module.exports = function (Messaging) { } }, function (messages, next) { - plugins.fireHook('filter:messaging.getMessagesData', { + plugins.fireHook('filter:messaging.getMessages', { messages: messages, uid: uid, roomId: roomId, diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index 564ea01fee..9e223a8158 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -28,7 +28,7 @@ module.exports = function (Messaging) { message: messageObj, }; - plugins.fireHook('filter:messaging.notifyUsersInRoom', data, next); + plugins.fireHook('filter:messaging.notify', data, next); }, function (data, next) { if (!data || !data.uids || !data.uids.length) { From 4b873242348ce980ca645693ec4669140639c689 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Wed, 26 Apr 2017 16:02:15 -0600 Subject: [PATCH 5/6] Quick fix --- src/messaging/notifications.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index 9e223a8158..37318121c3 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -26,6 +26,7 @@ module.exports = function (Messaging) { roomId: roomId, fromUid: fromUid, message: messageObj, + uids: uids, }; plugins.fireHook('filter:messaging.notify', data, next); From 5ccb488b023cd1d0e93f1333f97c4f10d442dc9d Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Tue, 9 May 2017 11:45:09 -0600 Subject: [PATCH 6/6] Fix indents --- src/notifications.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index a158a036fb..fef4a4a4d2 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -194,10 +194,10 @@ function pushToUids(uids, notification, callback) { plugins.fireHook('filter:notification.push', { notification: notification, uids: uids }, next); }, function (data, next) { - if (!data || !data.notification || !data.uids || !data.uids.length) { - return callback(); - } - + if (!data || !data.notification || !data.uids || !data.uids.length) { + return callback(); + } + uids = data.uids; notification = data.notification;