From 3ea84067b9a46712b7d8aa7d3c2512e9f695954c Mon Sep 17 00:00:00 2001 From: Pierre-Gilles Leymarie Date: Mon, 26 Nov 2018 14:59:31 +0800 Subject: [PATCH] Fix #356: Notification/Messages are always sent through websocket by default. --- api/core/brain/brain.answer.js | 31 ++++++++++++-------- api/core/notification/notification.create.js | 18 ++++++++---- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/api/core/brain/brain.answer.js b/api/core/brain/brain.answer.js index 5a0306d662..9c4a226553 100644 --- a/api/core/brain/brain.answer.js +++ b/api/core/brain/brain.answer.js @@ -49,17 +49,24 @@ module.exports = function answer(result, user) { newMessage.senderName = user.assistantName; newMessage.needAnswer = result.response.needAnswer; - // test each notification system - return Promise.mapSeries(notificationTypes, function(notificationType) { - return trySendingMessage(newMessage, notificationType, user) - .catch((err) => { - - // if error is normal, propagate error to abort chain - if(err.message == 'ok') { - return Promise.reject(err); - } - }); - }) + // remove socket, socket is sent in any way + notificationTypes = notificationTypes.filter((type) => type.service !== 'socket'); + + // send notification in websocket in any cases + return trySendingMessage(newMessage, { service: 'socket' }, user) + .catch(() => true) + + // then, try all the other ways + .then(() => Promise.mapSeries(notificationTypes, function(notificationType) { + return trySendingMessage(newMessage, notificationType, user) + .catch((err) => { + + // if error is normal, propagate error to abort chain + if(err.message == 'ok') { + return Promise.reject(err); + } + }); + })) .catch(function(err) { if (err.message !== 'ok') { sails.log.warn(err); @@ -113,7 +120,7 @@ function trySendingMessage(newMessage, type, user) { return toCall(newMessage, user) .then(function(result) { - sails.log.info(`Message sent with success with ${type.service}. Aborting the chain.`); + sails.log.info(`Message sent with success with ${type.service}.`); // if module resolved, we stop the promise chain // it means one notification worked! diff --git a/api/core/notification/notification.create.js b/api/core/notification/notification.create.js index 2fc7ebec23..256bf8e795 100644 --- a/api/core/notification/notification.create.js +++ b/api/core/notification/notification.create.js @@ -54,10 +54,18 @@ function create(options) { .spread(function(notification, types, user) { sails.log.info(`Notification : create : Notification saved with success. Trying to send notification to user ID ${notification.user}`); - - return Promise.mapSeries(types, function(type) { - return startService(notification, type, user); - }); + + // remove socket, socket is sent in any way + types = types.filter((type) => type.service !== 'socket'); + + // send notification in websocket in any cases + return startService(notification, { service: 'socket' }, user) + .catch(() => true) + + // then, try other ways + .then(() => Promise.mapSeries(types, function(type) { + return startService(notification, type, user); + })); }) .catch(function(err) { if (err.message !== 'ok') { @@ -90,7 +98,7 @@ function startService(notification, type, user) { return notify(notification, user) .then(function(result) { - sails.log.info(`Notification threw ${type.service} sent with success. Aborting the chain.`); + sails.log.info(`Notification threw ${type.service} sent with success.`); // if module resolved, we stop the promise chain // it means one notification worked!