Skip to content

Commit

Permalink
Fix #356: Notification/Messages are always sent through websocket by …
Browse files Browse the repository at this point in the history
…default.
  • Loading branch information
Pierre-Gilles committed Nov 26, 2018
1 parent 1a81fce commit 3ea8406
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
31 changes: 19 additions & 12 deletions api/core/brain/brain.answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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!
Expand Down
18 changes: 13 additions & 5 deletions api/core/notification/notification.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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!
Expand Down

0 comments on commit 3ea8406

Please sign in to comment.