From 6b55471fc316c808a3464b40e9d170c005774fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20D=2E=20D=C3=ADaz?= Date: Fri, 2 Oct 2020 20:23:03 +0200 Subject: [PATCH] [IMP] mail_tracking: Discuss failed messages: Button and domain --- mail_tracking/demo/demo.xml | 16 +++++++ mail_tracking/models/mail_message.py | 47 +++++++++++++------ .../static/src/js/failed_message/discuss.js | 35 +++++++------- .../static/src/xml/failed_message/discuss.xml | 2 +- mail_tracking/tests/test_mail_tracking.py | 4 +- 5 files changed, 70 insertions(+), 34 deletions(-) diff --git a/mail_tracking/demo/demo.xml b/mail_tracking/demo/demo.xml index e26ae1d..6c056d6 100644 --- a/mail_tracking/demo/demo.xml +++ b/mail_tracking/demo/demo.xml @@ -16,6 +16,10 @@ wood.corner26@example.com + Message with CC @@ -40,6 +44,10 @@ wood.corner26@example.com + Failed Message @@ -64,6 +72,10 @@ jackson.group82@example.com + Failed Message @@ -88,6 +100,10 @@ admin@example.com + Failed Message diff --git a/mail_tracking/models/mail_message.py b/mail_tracking/models/mail_message.py index bc093bb..ab711d5 100644 --- a/mail_tracking/models/mail_message.py +++ b/mail_tracking/models/mail_message.py @@ -5,6 +5,7 @@ from email.utils import getaddresses from odoo import _, api, fields, models +from odoo.osv import expression from odoo.tools import email_split @@ -25,7 +26,9 @@ class MailMessage(models.Model): help="The message tracking will be considered" " to filter tracking issues", default=False, ) - is_failed_message = fields.Boolean(compute="_compute_is_failed_message") + is_failed_message = fields.Boolean( + compute="_compute_is_failed_message", search="_search_is_failed_message", + ) @api.model def get_failed_states(self): @@ -35,7 +38,7 @@ class MailMessage(models.Model): @api.depends( "mail_tracking_needs_action", "author_id", - "partner_ids", + "notification_ids", "mail_tracking_ids.state", ) def _compute_is_failed_message(self): @@ -44,7 +47,7 @@ class MailMessage(models.Model): for message in self: needs_action = message.mail_tracking_needs_action involves_me = self.env.user.partner_id in ( - message.author_id | message.partner_ids + message.author_id | message.notification_ids.mapped("res_partner_id") ) has_failed_trackings = failed_states.intersection( message.mapped("mail_tracking_ids.state") @@ -53,6 +56,31 @@ class MailMessage(models.Model): needs_action and involves_me and has_failed_trackings ) + def _search_is_failed_message(self, operator, value): + """Search for messages considered failed for the active user. + Be notice that 'notificacion_ids' is a record that change if + the user mark the message as readed. + """ + # FIXME: Due to ORM issue with auto_join and 'OR' we construct the domain + # using an extra query to get valid results. + # For more information see: https://github.com/odoo/odoo/issues/25175 + notification_partner_ids = self.search( + [("notification_ids.res_partner_id", "=", self.env.user.partner_id.id)] + ) + return expression.normalize_domain( + [ + ( + "mail_tracking_ids.state", + "in" if value else "not in", + list(self.get_failed_states()), + ), + ("mail_tracking_needs_action", "=", True), + "|", + ("author_id", "=", self.env.user.partner_id.id), + ("id", "in", notification_partner_ids.ids), + ] + ) + def _tracking_status_map_get(self): """Map tracking states to be used in chatter""" return { @@ -252,19 +280,10 @@ class MailMessage(models.Model): (self._cr.dbname, "res.partner", self.env.user.partner_id.id), notification ) - def _get_failed_message_domain(self): - domain = self.env["mail.thread"]._get_failed_message_domain() - domain += [ - "|", - ("partner_ids", "in", [self.env.user.partner_id.id]), - ("author_id", "=", self.env.user.partner_id.id), - ] - return domain - @api.model def get_failed_count(self): """ Gets the number of failed messages used on discuss mailbox item""" - return self.search_count(self._get_failed_message_domain()) + return self.search_count([("is_failed_message", "=", True)]) @api.model def set_all_as_reviewed(self): @@ -272,7 +291,7 @@ class MailMessage(models.Model): Used by Discuss """ - unreviewed_messages = self.search(self._get_failed_message_domain()) + unreviewed_messages = self.search([("is_failed_message", "=", True)]) unreviewed_messages.write({"mail_tracking_needs_action": False}) ids = unreviewed_messages.ids diff --git a/mail_tracking/static/src/js/failed_message/discuss.js b/mail_tracking/static/src/js/failed_message/discuss.js index adf4214..2fb8edd 100644 --- a/mail_tracking/static/src/js/failed_message/discuss.js +++ b/mail_tracking/static/src/js/failed_message/discuss.js @@ -15,15 +15,12 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) { var Discuss = require("mail.Discuss"); var MailManager = require("mail.Manager"); var Mailbox = require("mail.model.Mailbox"); + var Dialog = require("web.Dialog"); var core = require("web.core"); - var session = require("web.session"); var QWeb = core.qweb; var _t = core._t; - /* The states to consider a message as failed message */ - var FAILED_STATES = ["error", "rejected", "spam", "bounced", "soft-bounced"]; - AbstractMessage.include({ /** * Abstract declaration to know if a message is included in the @@ -216,10 +213,9 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) { * @override */ _updateControlPanelButtons: function(thread) { - this.$btn_set_all_reviewed.toggleClass( - "d-none d-md-none", - thread.getID() !== "mailbox_failed" - ); + this.$btn_set_all_reviewed + .toggleClass("d-none", thread.getID() !== "mailbox_failed") + .toggleClass("d-md-inline-block", thread.getID() === "mailbox_failed"); return this._super.apply(this, arguments); }, @@ -343,7 +339,20 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) { * @private */ _onSetAllAsReviewedClicked: function() { - this._thread.setAllMessagesAsReviewed(); + var self = this; + var failed = this.call("mail_service", "getMailbox", "failed"); + var promptText = _.str.sprintf( + _t( + "Do you really want to mark as reviewed all the" + + " failed messages (%d)?" + ), + failed.getMailboxCounter() + ); + Dialog.confirm(this, promptText, { + confirm_callback: function() { + self._thread.setAllMessagesAsReviewed(); + }, + }); }, }); @@ -371,13 +380,7 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) { */ _getThreadDomain: function() { if (this._id === "mailbox_failed") { - return [ - ["mail_tracking_ids.state", "in", FAILED_STATES], - ["mail_tracking_needs_action", "=", true], - "|", - ["partner_ids", "in", [session.partner_id]], - ["author_id", "=", session.partner_id], - ]; + return [["is_failed_message", "=", true]]; } // Workaround to avoid throw 'Missing domain' exception. Call _super // without a valid (hard-coded) thread id causes that exeception. diff --git a/mail_tracking/static/src/xml/failed_message/discuss.xml b/mail_tracking/static/src/xml/failed_message/discuss.xml index 3248de4..9e426b2 100644 --- a/mail_tracking/static/src/xml/failed_message/discuss.xml +++ b/mail_tracking/static/src/xml/failed_message/discuss.xml @@ -35,7 +35,7 @@ diff --git a/mail_tracking/tests/test_mail_tracking.py b/mail_tracking/tests/test_mail_tracking.py index 968f06f..2411e8a 100644 --- a/mail_tracking/tests/test_mail_tracking.py +++ b/mail_tracking/tests/test_mail_tracking.py @@ -290,9 +290,7 @@ class TestMailTracking(TransactionCase): values = tracking.mail_message_id.get_failed_messages() self.assertEqual(values[0]["id"], tracking.mail_message_id.id) messages = MailMessageObj.search([]) - messages_failed = MailMessageObj.search( - MailMessageObj._get_failed_message_domain() - ) + messages_failed = MailMessageObj.search([["is_failed_message", "=", True]]) self.assertTrue(messages) self.assertTrue(messages_failed) self.assertTrue(len(messages) > len(messages_failed))