From 98bd82365c8ec159b3647bf87cf397b208318fd3 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Fri, 2 Oct 2015 09:39:23 +0200 Subject: [PATCH] [FIX] mail: fetch channel info on new message notification When receiving a new message, we add it in the client side cache of the corresponding channel. The problem was that when we previously unsubscribed to this channel, it was removed from the cache, producing a traceback when trying to add the new message. This rev. checks whether or not the channel is in the cache, and if not, performs an rpc to fetch its info before adding the message. --- addons/mail/static/src/js/chat_manager.js | 24 ++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index 9546a3b6111..280af961b5a 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -376,15 +376,17 @@ var chat_manager = { bus.restart_poll(); }); }, - join_channel: function (channel_id) { + join_channel: function (channel_id, options) { return ChannelModel .call('channel_join_and_get_info', [[channel_id]]) - .then(add_channel); + .then(function (result) { + add_channel(result, options); + }); }, unsubscribe: function (channel_id) { var def; - var channel = chat_manager.get_channel(channel_id); + var channel = chat_manager.get_channel(channel_id); if (channel.type === "dm") { def = ChannelModel.call('channel_pin', [channel.uuid, false]); } else { @@ -441,11 +443,19 @@ function init () { // new message in a channel var message = notification[1]; var channel_id = message.channel_ids[0]; - message = add_message(message, { channel_id: channel_id, show_notification: true} ); - if (_.contains(message.channel_ids, 'channel_inbox')) { - needaction_counter = needaction_counter + 1; - chat_manager.bus.trigger('update_needaction', needaction_counter); + // fetch the channel info if not done already + var channel = _.findWhere(channels, {id: channel_id}); + var channel_ready; + if (!channel) { + channel_ready = chat_manager.join_channel(channel_id, { autoswitch: false }); } + $.when(channel_ready).then(function () { + message = add_message(message, { channel_id: channel_id, show_notification: true }); + if (_.contains(message.channel_ids, 'channel_inbox')) { + needaction_counter = needaction_counter + 1; + chat_manager.bus.trigger('update_needaction', needaction_counter); + } + }); } if (model === 'res.partner') { var chat_session = notification[1];