[FIX] mail: click on unstar all button should reset the starredcounter

Before this commit:
Clicking on 'unstar all' button removes all messages from starred channel
but it only decreases the counter as many as there are displayed messages
on screen.

For example, if there are 40 messages starred, only 30 messages
are on screen (default limit) but unstar all will decrease counter
only for those messages instead of 40.

After this commit:
clicking on 'unstar all' button will correctly take in account all
fetched messages and not only displayed messages.
So now, after clicking on 'unstar all' button will update the starred
counter to 0.

task-ID: 33958
This commit is contained in:
Dipalee Bhalodia
2018-03-30 16:42:59 +02:00
committed by Adrien Dieudonne
parent 548e0ba239
commit 1fa8f67926
4 changed files with 133 additions and 3 deletions
@@ -1582,16 +1582,21 @@ var ChatManager = AbstractService.extend({
message.is_starred = data.starred;
if (!message.is_starred) {
self._removeMessageFromChannel("channel_starred", message);
self.starredCounter--;
} else {
self._addToCache(message, []);
var channelStarred = self.getChannel('channel_starred');
channelStarred.cache = _.pick(channelStarred.cache, "[]");
self.starredCounter++;
}
self.chatBus.trigger('update_message', message);
}
});
if (data.starred) { // increase starred counter if message is marked as star
this.starredCounter += data.message_ids.length;
} else { // decrease starred counter if message is remove from star if unstar_all then it will set to 0.
this.starredCounter -= data.message_ids.length;
}
this.chatBus.trigger('update_starred', this.starredCounter);
},
/**
+104
View File
@@ -375,5 +375,109 @@ QUnit.test('older messages are loaded on scroll', function (assert) {
});
});
QUnit.test('"Unstar all" button should reset the starred counter', function (assert) {
assert.expect(2);
var done = assert.async();
var bus = new Bus();
var BusService = createBusService(bus);
var msgData = [];
_.each(_.range(1, 41), function (num) {
msgData.push({
id: num,
body: "<p>test" + num + "</p>",
author_id: ["1", "Me"],
channel_ids: [1],
starred: true,
starred_partner_ids: [1],
}
);
});
this.data = {
initMessaging: {
channel_slots: {
channel_channel: [{
id: 1,
channel_type: "channel",
name: "general",
}],
},
starred_counter: msgData.length,
},
'mail.message': {
fields: {
body: {
string: "Contents",
type: 'html',
},
author_id: {
string: "Author",
relation: 'res.partner',
},
channel_ids: {
string: "Channels",
type: 'many2many',
relation: 'mail.channel',
},
starred: {
string: "Starred",
type: 'boolean',
},
needaction: {
string: "Need Action",
type: 'boolean',
},
starred_partner_ids: {
string: "partner ids",
type: 'integer',
}
},
records: msgData,
},
};
createDiscuss({
id: 1,
context: {},
params: {},
data: this.data,
services: [ChatManager, BusService],
mockRPC: function (route, args) {
if (args.method === 'unstar_all') {
var data = {
message_ids: _.range(1, 41),
starred: false,
type: 'toggle_star',
};
var notification = [[false, 'res.partner'], data];
bus.trigger('notification', [notification]);
return $.when(42);
}
return this._super.apply(this, arguments);
},
session: {partner_id: 1},
})
.then(function (discuss) {
var $starred = discuss.$('.o_mail_chat_sidebar').find('.o_mail_chat_title_starred');
var $starredCounter = $('.o_mail_chat_title_starred > .o_mail_sidebar_needaction');
// Go to Starred channel
$starred.click();
// Test Initial Value
assert.strictEqual($starredCounter.text().trim(), "40", "40 messages should be starred");
// Unstar all and wait 'update_starred'
$('.o_control_panel .o_mail_chat_button_unstar_all').click();
$starredCounter = $('.o_mail_chat_title_starred > .o_mail_sidebar_needaction');
assert.strictEqual($starredCounter.text().trim(), "0",
"All messages should be unstarred");
discuss.destroy();
done();
});
});
});
});
@@ -39,6 +39,21 @@ MockServer.include({
'menu_id': false,
});
},
/**
* Simulate the 'message_fetch' Python method
*
* @return {Object}
*/
_mockMessageFetch: function (args) {
args.kwargs.offset = Math.max(this.data['mail.message'].records.length - args.kwargs.limit, 0);
return this._mockSearchRead('mail.message', [
args.args[0],
_.keys(this.data['mail.message'].fields),
], args.kwargs);
},
/**
* @override
*/
@@ -47,7 +62,7 @@ MockServer.include({
return $.when(this._mockInitMessaging(args));
}
if (args.method === 'message_fetch') {
return $.when([]);
return $.when(this._mockMessageFetch(args));
}
if (args.method === 'channel_fetch_listeners') {
return $.when([]);
@@ -5,6 +5,7 @@ var Discuss = require('mail.chat_discuss');
var AbstractService = require('web.AbstractService');
var Bus = require('web.Bus');
var ControlPanel = require('web.ControlPanel');
var testUtils = require('web.test_utils');
var Widget = require('web.Widget');
@@ -75,6 +76,8 @@ function createDiscuss(params) {
var discuss = new Discuss(parent, params);
discuss.set_cp_bus(new Widget());
var selector = params.debug ? 'body' : '#qunit-fixture';
var controlPanel = new ControlPanel(parent);
controlPanel.appendTo($(selector));
discuss.appendTo($(selector));
// override 'destroy' of discuss so that it calls 'destroy' on the parent
@@ -86,6 +89,9 @@ function createDiscuss(params) {
parent.destroy();
};
// link the view to the control panel
discuss.set_cp_bus(controlPanel.get_bus());
return discuss.call('chat_manager', 'isReady').then(function () {
return discuss;
});