From d3ceaa3adc82d016ee06d47d97ff61ad745e4072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Wed, 10 Oct 2018 16:23:29 +0000 Subject: [PATCH] [FIX] mail: improved message_fetch for moderated channels There was two issues with the previous code: 1. It was using a weak heuristic to determine whether the current thread is a channel (ID is a number) 2. For any channel, it was always passing `moderated_channel_ids` with some values to the server, even though the channel is not moderated at all. This commit solves both issues by passing `moderated_channel_ids` only for moderated channel. Also, this logic has been moved from SearchableThread to Channel, since moderation makes only sense for channels and channels are aware of whether they are moderated or not. --- .../static/src/js/models/threads/channel.js | 17 ++++++++++++++ .../js/models/threads/searchable_thread.js | 22 ++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/addons/mail/static/src/js/models/threads/channel.js b/addons/mail/static/src/js/models/threads/channel.js index 2e186bd11e5..3a257eb2272 100644 --- a/addons/mail/static/src/js/models/threads/channel.js +++ b/addons/mail/static/src/js/models/threads/channel.js @@ -357,6 +357,23 @@ var Channel = SearchableThread.extend(ThreadTypingMixin, { return lastSeenMessageID; }); }, + /** + * Override so that it tells whether the channel is moderated or not. This + * is useful in order to display pending moderation messages when the + * current user is either moderator of the channel or has posted some + * messages that are pending moderation. + * + * @override + * @private + * @returns {Object} + */ + _getFetchMessagesKwargs: function () { + var kwargs = this._super.apply(this, arguments); + if (this.isModerated()) { + kwargs.moderated_channel_ids = [this.getID()]; + } + return kwargs; + }, /** * Get the domain to fetch all the messages in the current channel * diff --git a/addons/mail/static/src/js/models/threads/searchable_thread.js b/addons/mail/static/src/js/models/threads/searchable_thread.js index 17fa119890a..8ddff5457af 100644 --- a/addons/mail/static/src/js/models/threads/searchable_thread.js +++ b/addons/mail/static/src/js/models/threads/searchable_thread.js @@ -163,10 +163,7 @@ var SearchableThread = Thread.extend({ */ _fetchMessages: function (pDomain, loadMore) { var self = this; - var domain = this._getThreadDomain(); - var moderatedChannelIds = typeof(this._id) === 'number' ? [this._id] : false; - var cache = this._getCache(pDomain); if (pDomain) { domain = domain.concat(pDomain || []); @@ -179,11 +176,7 @@ var SearchableThread = Thread.extend({ model: 'mail.message', method: 'message_fetch', args: [domain], - kwargs: { - moderated_channel_ids: moderatedChannelIds, - limit: this._FETCH_LIMIT, - context: session.user_context - }, + kwargs: this._getFetchMessagesKwargs(), }).then(function (messages) { if (!cache.allHistoryLoaded) { cache.allHistoryLoaded = messages.length < self._FETCH_LIMIT; @@ -218,6 +211,19 @@ var SearchableThread = Thread.extend({ } return this._cache[stringifiedDomain]; }, + /** + * Get the kwargs that are passed to the server with the 'message_fetch' + * RPC. + * + * @private + * @returns {Object} + */ + _getFetchMessagesKwargs: function () { + return { + limit: this._FETCH_LIMIT, + context: session.user_context + }; + }, /** * Get the domain to fetch all the messages in the current thread *