[IMP] mail_tracking: Discuss failed messages: Button and domain

This commit is contained in:
Alexandre D. Díaz 2020-10-02 20:23:03 +02:00 committed by Jasmin Solanki
parent 2724284aaa
commit 6b55471fc3
5 changed files with 70 additions and 34 deletions

View File

@ -16,6 +16,10 @@
<field name="email_from">wood.corner26@example.com</field> <field name="email_from">wood.corner26@example.com</field>
<field name="author_id" ref="base.res_partner_1" /> <field name="author_id" ref="base.res_partner_1" />
<field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" /> <field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" />
<field
name="notification_ids"
eval="[(0, 0, {'res_partner_id': ref('base.partner_demo')})]"
/>
<field name="subject">Message with CC</field> <field name="subject">Message with CC</field>
</record> </record>
@ -40,6 +44,10 @@
<field name="email_from">wood.corner26@example.com</field> <field name="email_from">wood.corner26@example.com</field>
<field name="author_id" ref="base.res_partner_1" /> <field name="author_id" ref="base.res_partner_1" />
<field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" /> <field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" />
<field
name="notification_ids"
eval="[(0, 0, {'res_partner_id': ref('base.partner_demo')})]"
/>
<field name="subject">Failed Message</field> <field name="subject">Failed Message</field>
</record> </record>
@ -64,6 +72,10 @@
<field name="email_from">jackson.group82@example.com</field> <field name="email_from">jackson.group82@example.com</field>
<field name="author_id" ref="base.res_partner_10" /> <field name="author_id" ref="base.res_partner_10" />
<field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" /> <field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" />
<field
name="notification_ids"
eval="[(0, 0, {'res_partner_id': ref('base.partner_demo')})]"
/>
<field name="subject">Failed Message</field> <field name="subject">Failed Message</field>
</record> </record>
@ -88,6 +100,10 @@
<field name="email_from">admin@example.com</field> <field name="email_from">admin@example.com</field>
<field name="author_id" ref="base.partner_admin" /> <field name="author_id" ref="base.partner_admin" />
<field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" /> <field name="partner_ids" eval="[(6, 0, [ref('base.partner_demo')])]" />
<field
name="notification_ids"
eval="[(0, 0, {'res_partner_id': ref('base.partner_demo')})]"
/>
<field name="subject">Failed Message</field> <field name="subject">Failed Message</field>
</record> </record>

View File

@ -5,6 +5,7 @@
from email.utils import getaddresses from email.utils import getaddresses
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.osv import expression
from odoo.tools import email_split 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", help="The message tracking will be considered" " to filter tracking issues",
default=False, 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 @api.model
def get_failed_states(self): def get_failed_states(self):
@ -35,7 +38,7 @@ class MailMessage(models.Model):
@api.depends( @api.depends(
"mail_tracking_needs_action", "mail_tracking_needs_action",
"author_id", "author_id",
"partner_ids", "notification_ids",
"mail_tracking_ids.state", "mail_tracking_ids.state",
) )
def _compute_is_failed_message(self): def _compute_is_failed_message(self):
@ -44,7 +47,7 @@ class MailMessage(models.Model):
for message in self: for message in self:
needs_action = message.mail_tracking_needs_action needs_action = message.mail_tracking_needs_action
involves_me = self.env.user.partner_id in ( 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( has_failed_trackings = failed_states.intersection(
message.mapped("mail_tracking_ids.state") message.mapped("mail_tracking_ids.state")
@ -53,6 +56,31 @@ class MailMessage(models.Model):
needs_action and involves_me and has_failed_trackings 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): def _tracking_status_map_get(self):
"""Map tracking states to be used in chatter""" """Map tracking states to be used in chatter"""
return { return {
@ -252,19 +280,10 @@ class MailMessage(models.Model):
(self._cr.dbname, "res.partner", self.env.user.partner_id.id), notification (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 @api.model
def get_failed_count(self): def get_failed_count(self):
""" Gets the number of failed messages used on discuss mailbox item""" """ 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 @api.model
def set_all_as_reviewed(self): def set_all_as_reviewed(self):
@ -272,7 +291,7 @@ class MailMessage(models.Model):
Used by Discuss """ 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}) unreviewed_messages.write({"mail_tracking_needs_action": False})
ids = unreviewed_messages.ids ids = unreviewed_messages.ids

View File

@ -15,15 +15,12 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) {
var Discuss = require("mail.Discuss"); var Discuss = require("mail.Discuss");
var MailManager = require("mail.Manager"); var MailManager = require("mail.Manager");
var Mailbox = require("mail.model.Mailbox"); var Mailbox = require("mail.model.Mailbox");
var Dialog = require("web.Dialog");
var core = require("web.core"); var core = require("web.core");
var session = require("web.session");
var QWeb = core.qweb; var QWeb = core.qweb;
var _t = core._t; var _t = core._t;
/* The states to consider a message as failed message */
var FAILED_STATES = ["error", "rejected", "spam", "bounced", "soft-bounced"];
AbstractMessage.include({ AbstractMessage.include({
/** /**
* Abstract declaration to know if a message is included in the * Abstract declaration to know if a message is included in the
@ -216,10 +213,9 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) {
* @override * @override
*/ */
_updateControlPanelButtons: function(thread) { _updateControlPanelButtons: function(thread) {
this.$btn_set_all_reviewed.toggleClass( this.$btn_set_all_reviewed
"d-none d-md-none", .toggleClass("d-none", thread.getID() !== "mailbox_failed")
thread.getID() !== "mailbox_failed" .toggleClass("d-md-inline-block", thread.getID() === "mailbox_failed");
);
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
}, },
@ -343,7 +339,20 @@ odoo.define("mail_tracking.FailedMessageDiscuss", function(require) {
* @private * @private
*/ */
_onSetAllAsReviewedClicked: function() { _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() { _getThreadDomain: function() {
if (this._id === "mailbox_failed") { if (this._id === "mailbox_failed") {
return [ return [["is_failed_message", "=", true]];
["mail_tracking_ids.state", "in", FAILED_STATES],
["mail_tracking_needs_action", "=", true],
"|",
["partner_ids", "in", [session.partner_id]],
["author_id", "=", session.partner_id],
];
} }
// Workaround to avoid throw 'Missing domain' exception. Call _super // Workaround to avoid throw 'Missing domain' exception. Call _super
// without a valid (hard-coded) thread id causes that exeception. // without a valid (hard-coded) thread id causes that exeception.

View File

@ -35,7 +35,7 @@
<t t-jquery="div" t-operation="append"> <t t-jquery="div" t-operation="append">
<button <button
type="button" type="button"
class="btn btn-secondary o_mail_discuss_button_set_all_reviewed d-none d-md-none d-md-inline-block" class="btn btn-secondary o_mail_discuss_button_set_all_reviewed"
title="Mark all as reviewed" title="Mark all as reviewed"
>Set all as reviewed</button> >Set all as reviewed</button>
</t> </t>

View File

@ -290,9 +290,7 @@ class TestMailTracking(TransactionCase):
values = tracking.mail_message_id.get_failed_messages() values = tracking.mail_message_id.get_failed_messages()
self.assertEqual(values[0]["id"], tracking.mail_message_id.id) self.assertEqual(values[0]["id"], tracking.mail_message_id.id)
messages = MailMessageObj.search([]) messages = MailMessageObj.search([])
messages_failed = MailMessageObj.search( messages_failed = MailMessageObj.search([["is_failed_message", "=", True]])
MailMessageObj._get_failed_message_domain()
)
self.assertTrue(messages) self.assertTrue(messages)
self.assertTrue(messages_failed) self.assertTrue(messages_failed)
self.assertTrue(len(messages) > len(messages_failed)) self.assertTrue(len(messages) > len(messages_failed))