From 12d5667830feeda005218c14ba2caa321f1cffa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 10 Feb 2016 15:07:11 +0100 Subject: [PATCH] [FIX] mail: use the domain to determine message to mark as read The button 'mark all as read' used to mark all messages as read, which is not usually what the user wants when there is an active filter in the search view. This commit make sure that the domain is send to the server and used correctly. --- addons/mail/models/mail_message.py | 49 ++++++++++++---------- addons/mail/static/src/js/chat_manager.js | 6 +-- addons/mail/static/src/js/client_action.js | 2 +- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 306137fa723..fe1fc7a1141 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -7,6 +7,7 @@ import logging from openerp import _, api, fields, models, SUPERUSER_ID from openerp import tools from openerp.exceptions import UserError, AccessError +from openerp.osv import expression _logger = logging.getLogger(__name__) @@ -147,34 +148,36 @@ class Message(models.Model): #------------------------------------------------------ @api.model - def mark_all_as_read(self, channel_ids=None): + def mark_all_as_read(self, channel_ids=None, domain=None): """ Remove all needactions of the current partner. If channel_ids is given, restrict to messages written in one of those channels. """ partner_id = self.env.user.partner_id.id - # possibly horribly inefficient method: - # it does one db request for the search, and one for each message in - # the result set to remove the current user from the relation. - # domain = [('needaction_partner_ids', 'in', partner_id)] - # if channel_ids: - # domain += [('channel_ids', 'in', channel_ids)] - # unread_messages = self.search(domain) - # unread_messages.write({'needaction_partner_ids': [(3, partner_id)]}) + if domain is None: + query = "DELETE FROM mail_message_res_partner_needaction_rel WHERE res_partner_id IN %s" + args = [(partner_id,)] + if channel_ids: + query += """ + AND mail_message_id in + (SELECT mail_message_id + FROM mail_message_mail_channel_rel + WHERE mail_channel_id in %s)""" + args += [tuple(channel_ids)] + query += " RETURNING mail_message_id as id" + self._cr.execute(query, args) + self.invalidate_cache() - # a much faster way to do this is in pure sql: - query = "DELETE FROM mail_message_res_partner_needaction_rel WHERE res_partner_id IN %s" - args = [(partner_id,)] - if channel_ids: - query += """ - AND mail_message_id in - (SELECT mail_message_id - FROM mail_message_mail_channel_rel - WHERE mail_channel_id in %s)""" - args += [tuple(channel_ids)] - query += " RETURNING mail_message_id as id" - self._cr.execute(query, args) - self.invalidate_cache() + ids = [m['id'] for m in self._cr.dictfetchall()] + else: + # not really efficient method: it does one db request for the + # search, and one for each message in the result set to remove the + # current user from the relation. + msg_domain = [('needaction_partner_ids', 'in', partner_id)] + if channel_ids: + msg_domain += [('channel_ids', 'in', channel_ids)] + unread_messages = self.search(expression.AND([msg_domain, domain])) + unread_messages.write({'needaction_partner_ids': [(3, partner_id)]}) + ids = unread_messages.mapped('id') - ids = [m['id'] for m in self._cr.dictfetchall()] notification = {'type': 'mark_as_read', 'message_ids': ids, 'channel_ids': channel_ids} self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', self.env.user.partner_id.id), notification) diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js index eb0e282e45f..d1c17e1b965 100644 --- a/addons/mail/static/src/js/chat_manager.js +++ b/addons/mail/static/src/js/chat_manager.js @@ -777,9 +777,9 @@ var chat_manager = { return $.when(); } }, - mark_all_as_read: function (channel) { - if ((!channel && needaction_counter) || (channel && channel.needaction_counter)) { - return MessageModel.call('mark_all_as_read', channel ? [[channel.id]] : []); + mark_all_as_read: function (channel, domain) { + if ((channel.id === "channel_inbox" && needaction_counter) || (channel && channel.needaction_counter)) { + return MessageModel.call('mark_all_as_read', [], {channel_ids: channel.id !== "channel_inbox" ? [channel.id] : [], domain: domain}); } return $.when(); }, diff --git a/addons/mail/static/src/js/client_action.js b/addons/mail/static/src/js/client_action.js index 442e00e6b94..090b2025239 100644 --- a/addons/mail/static/src/js/client_action.js +++ b/addons/mail/static/src/js/client_action.js @@ -200,7 +200,7 @@ var ChatAction = Widget.extend(ControlPanelMixin, { self.$('.o_mail_chat_sidebar').slideToggle(200); }); this.$buttons.on('click', '.o_mail_chat_button_mark_read', function () { - chat_manager.mark_all_as_read(); + chat_manager.mark_all_as_read(self.channel, self.domain); }); this.$buttons.on('click', '.o_mail_chat_button_unstar_all', chat_manager.unstar_all); this.$buttons.on('click', '.o_mail_chat_button_new_message', this.on_click_new_message);