[REF] mail: clean some JS code

1. Removed `getLocalMessages` method in mailbox, and replaced
   with an override on `getMessages`.

   (This method was used before the distinction was made
   between `getMessages` and `fetchMessages`)

2. Removed unused method `_warnUpdatedUnreadCounter` in
   abstract thread window.

   (Leftover from an early POC)

3. Set the default value `''` for the status of a thread.

   (The method `getStatus` should always return a string)

4. Enforce default value `false` for detached status of channel.

   (The method `isDetached` should always return a boolean)

5. Improve readability of some tests.

   (removed useless variables and/or redundant code)
This commit is contained in:
Alexandre Kühn
2018-08-30 13:26:07 +02:00
parent bd43df539f
commit be44efe852
7 changed files with 26 additions and 48 deletions
@@ -16,7 +16,7 @@ var AbstractThread = Class.extend(Mixins.EventDispatcherMixin, {
* @param {Object} params.data
* @param {integer|string} params.data.id the ID of this thread
* @param {string} params.data.name the name of this thread
* @param {string} params.data.status the status of this thread
* @param {string} [params.data.status=''] the status of this thread
* @param {Object} params.parent Object with the event-dispatcher mixin
* (@see {web.mixins.EventDispatcherMixin})
*/
@@ -27,7 +27,7 @@ var AbstractThread = Class.extend(Mixins.EventDispatcherMixin, {
this._folded = false; // threads are unfolded by default
this._id = params.data.id;
this._name = params.data.name;
this._status = params.data.status;
this._status = params.data.status || '';
this._unreadCounter = 0; // amount of messages not yet been read
},
@@ -51,7 +51,7 @@ var Channel = SearchableThread.extend(ThreadTypingMixin, {
this._autoswitch = 'autoswitch' in options ? options.autoswitch : true;
this._commands = undefined;
this._creatorUID = data.create_uid;
this._detached = data.is_minimized;
this._detached = data.is_minimized || false;
this._directPartnerID = undefined;
this._folded = data.state === 'folded';
// if set: hide 'Leave channel' button
@@ -66,7 +66,6 @@ var Channel = SearchableThread.extend(ThreadTypingMixin, {
// number of messages in this channel that are in inbox.
this._needactionCounter = data.message_needaction_counter || 0;
this._serverType = data.channel_type;
this._status = undefined;
this._throttleFetchSeen = _.throttle(this._fetchSeen.bind(this), 3000);
// unique identifier for this channel, which is required for some rpc
this._uuid = data.uuid;
@@ -46,11 +46,8 @@ var Mailbox = SearchableThread.extend({
this._mailboxCounter = Math.max(this._mailboxCounter - num, 0);
},
/**
* Get the local messages of the mailbox (by local messages, we mean
* messages that have already been fetched from the server).
*
* It is possible to filter on local messages that are specific to a
* document with the `options` parameter.
* Override so that there are options to filter messages based on document
* model and ID.
*
* @param {Object} [options]
* @param {string} [options.documentModel] model of the document that the
@@ -58,15 +55,15 @@ var Mailbox = SearchableThread.extend({
* @param {integer} [options.documentID] ID of the document that the local
* messages of inbox must be linked to.
*/
getLocalMessages: function (options) {
var localMessages = this._cache['[]'].messages;
getMessages: function (options) {
var messages = this._super.apply(this, arguments);
if (!options) {
return localMessages;
return messages;
}
if (options.documentModel && options.documentID) {
return _.filter(localMessages, function (localMessage) {
return localMessage.getDocumentModel() === options.documentModel &&
localMessage.getDocumentID() === options.documentID;
return _.filter(messages, function (message) {
return message.getDocumentModel() === options.documentModel &&
message.getDocumentID() === options.documentID;
});
}
},
@@ -312,7 +312,7 @@ var MessagingMenu = Widget.extend({
if (previewID === 'mailbox_inbox') {
var documentID = $preview.data('document-id');
var inbox = this.call('mail_service', 'getMailbox', 'inbox');
var messages = inbox.getLocalMessages({
var messages = inbox.getMessages({
documentModel: documentModel,
documentID: documentID,
});
@@ -384,13 +384,6 @@ var AbstractThreadWindow = Widget.extend({
this.updateVisualFoldState();
}
},
/**
* Warn other mail components that the unread counter has been updated
*
* @abstract
* @private
*/
_warnUpdatedUnreadCounter: function () {},
//--------------------------------------------------------------------------
// Handlers
@@ -156,14 +156,6 @@ var ThreadWindow = AbstractThreadWindow.extend({
removePassive: function () {
this._passive = false;
},
/**
* Set the thread window in passive mode, so that new received message will
* keep the thread window as unread until there is focus on the thread
* window.
*/
setPassive: function () {
this._passive = true;
},
/**
* Update this thread window
*
@@ -177,7 +169,7 @@ var ThreadWindow = AbstractThreadWindow.extend({
update: function (options) {
var self = this;
if (options.passively) {
this.setPassive();
this._setPassive();
}
var bottomVisible = !this.isFolded() &&
!this.isHidden() &&
@@ -231,6 +223,16 @@ var ThreadWindow = AbstractThreadWindow.extend({
_open: function () {
this.call('mail_service', 'openThreadWindow', this.getID());
},
/**
* Set the thread window in passive mode, so that new received message will
* keep the thread window as unread until there is focus on the thread
* window.
*
* @private
*/
_setPassive: function () {
this._passive = true;
},
/**
* @private
*/
@@ -196,23 +196,10 @@ QUnit.test('emoji popover should open correctly in thread windows', function (as
QUnit.test('do not increase unread counter when receiving message with myself as author', function (assert) {
assert.expect(4);
var generalChannelID = 1;
var myselfPartnerID = 44;
this.data.initMessaging = {
channel_slots: {
channel_channel: [{
id: generalChannelID,
channel_type: 'channel',
name: "general",
}],
},
};
var parent = this.createParent({
data: this.data,
services: this.services,
session: { partner_id: myselfPartnerID }
session: { partner_id: 3 }
});
// get channel instance to link to thread window
@@ -228,14 +215,14 @@ QUnit.test('do not increase unread counter when receiving message with myself as
// simulate receiving message from myself
var messageData = {
author_id: [myselfPartnerID, "Myself"],
author_id: [3, "Myself"],
body: "<p>Test message</p>",
id: 2,
model: 'mail.channel',
res_id: 1,
channel_ids: [1],
};
var notification = [[false, 'mail.channel', generalChannelID], messageData];
var notification = [[false, 'mail.channel', 1], messageData];
parent.call('bus_service', 'trigger', 'notification', [notification]);
threadWindowHeaderText = $('.o_thread_window_header').text().replace(/\s/g, "");