From 6ccb35576cc6811eb51d42b9f1a72dc2f8e68000 Mon Sep 17 00:00:00 2001 From: TheBronx Date: Sun, 18 Jan 2015 13:38:42 +0100 Subject: [PATCH 1/3] new filter hooks on favourite actions #2620 before a favourite action is made, fire a filter hook so plugins can modify or cancel that action before it takes place. --- src/socket.io/posts.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 97b8e7ea74..7d4ae8ea37 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -168,22 +168,40 @@ function favouriteCommand(socket, command, eventName, notification, data, callba return callback(new Error('[[error:post-deleted]]')); } - favourites[command](data.pid, socket.uid, function(err, result) { + /* + hooks: + filter.post.upvote + filter.post.downvote + filter.post.unvote + filter.post.favourite + filter.post.unfavourite + */ + plugins.fireHook('filter:post.' + command, data, function(err, filteredData) { if (err) { return callback(err); } - socket.emit('posts.' + command, result); + executeFavouriteCommand(socket, command, eventName, notification, filteredData, callback); + }); + }); +} - if (result && eventName) { - websockets.in(data.room_id).emit('event:' + eventName, result); - } +function executeFavouriteCommand(socket, command, eventName, notification, data, callback) { + favourites[command](data.pid, socket.uid, function(err, result) { + if (err) { + return callback(err); + } - if (notification) { - SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification); - } - callback(); - }); + socket.emit('posts.' + command, result); + + if (result && eventName) { + websockets.in(data.room_id).emit('event:' + eventName, result); + } + + if (notification) { + SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification); + } + callback(); }); } From e3cf528b57a260745b1796cbf69d7532642e54e9 Mon Sep 17 00:00:00 2001 From: TheBronx Date: Sun, 18 Jan 2015 19:09:26 +0100 Subject: [PATCH 2/3] pass also user id on fireHook pass also user id on fireHook as suggested by @barisusakli --- src/socket.io/posts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 7d4ae8ea37..a040493942 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -176,7 +176,7 @@ function favouriteCommand(socket, command, eventName, notification, data, callba filter.post.favourite filter.post.unfavourite */ - plugins.fireHook('filter:post.' + command, data, function(err, filteredData) { + plugins.fireHook('filter:post.' + command, {data: data, uid: socket.uid}, function(err, filteredData) { if (err) { return callback(err); } From 0bd48ef023b1f06517fbafdf1bf0fe1f229911ed Mon Sep 17 00:00:00 2001 From: TheBronx Date: Sun, 18 Jan 2015 19:15:59 +0100 Subject: [PATCH 3/3] fix error in previous commit fix error in previous commit --- src/socket.io/posts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index a040493942..fb67639c94 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -181,7 +181,7 @@ function favouriteCommand(socket, command, eventName, notification, data, callba return callback(err); } - executeFavouriteCommand(socket, command, eventName, notification, filteredData, callback); + executeFavouriteCommand(socket, command, eventName, notification, filteredData.data, callback); }); }); }