From 90c0a4af0bfc0dd572fa0bb3e7cab5119d918120 Mon Sep 17 00:00:00 2001 From: Carlos Roca Date: Thu, 20 Aug 2020 13:48:01 +0200 Subject: [PATCH] [ADD] mail_history: Backport from version 13.0 of the history of messages --- mail_history/README.rst | 99 +++++ mail_history/__init__.py | 2 + mail_history/__manifest__.py | 25 ++ mail_history/hooks.py | 18 + mail_history/models/__init__.py | 2 + mail_history/models/mail_message.py | 59 +++ mail_history/models/mail_notification.py | 14 + mail_history/readme/CONFIGURE.rst | 2 + mail_history/readme/CONTRIBUTORS.rst | 3 + mail_history/readme/DESCRIPTION.rst | 1 + mail_history/readme/INSTALL.rst | 2 + mail_history/readme/ROADMAP.rst | 1 + mail_history/readme/USAGE.rst | 3 + mail_history/static/description/index.html | 446 +++++++++++++++++++++ mail_history/static/src/js/discuss.js | 31 ++ mail_history/static/src/js/mail_manager.js | 49 +++ mail_history/static/src/js/mailbox.js | 24 ++ mail_history/static/src/js/message.js | 50 +++ mail_history/static/src/xml/discuss.xml | 9 + mail_history/static/src/xml/thread.xml | 11 + mail_history/views/assets.xml | 19 + 21 files changed, 870 insertions(+) create mode 100644 mail_history/README.rst create mode 100644 mail_history/__init__.py create mode 100644 mail_history/__manifest__.py create mode 100644 mail_history/hooks.py create mode 100644 mail_history/models/__init__.py create mode 100644 mail_history/models/mail_message.py create mode 100644 mail_history/models/mail_notification.py create mode 100644 mail_history/readme/CONFIGURE.rst create mode 100644 mail_history/readme/CONTRIBUTORS.rst create mode 100644 mail_history/readme/DESCRIPTION.rst create mode 100644 mail_history/readme/INSTALL.rst create mode 100644 mail_history/readme/ROADMAP.rst create mode 100644 mail_history/readme/USAGE.rst create mode 100644 mail_history/static/description/index.html create mode 100644 mail_history/static/src/js/discuss.js create mode 100644 mail_history/static/src/js/mail_manager.js create mode 100644 mail_history/static/src/js/mailbox.js create mode 100644 mail_history/static/src/js/message.js create mode 100644 mail_history/static/src/xml/discuss.xml create mode 100644 mail_history/static/src/xml/thread.xml create mode 100644 mail_history/views/assets.xml diff --git a/mail_history/README.rst b/mail_history/README.rst new file mode 100644 index 000000000..6948d76e6 --- /dev/null +++ b/mail_history/README.rst @@ -0,0 +1,99 @@ +============= +Email History +============= + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/12.0/mail_history + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-12-0/social-12-0-mail_history + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/205/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module add the functionality to preserve the mails marked as read + +**Table of contents** + +.. contents:: + :local: + +Installation +============ + +When the module is installed a process is executed for recover the messages that was +mark as read yet. + +Configuration +============= + +To use this module you have to manage the messages with Odoo. To do this you have to go +*Profile > Preferences*, select *Handle in Odoo* and Save the changes. + +Usage +===== + +To see this module working, send some messages to the user that manage the emails with +Odoo. After that if you mark as read the messages they will dissapear from the inbox +and appear on the history section. + +Known issues / Roadmap +====================== + +This module will dissapear for v13.0 because it is implemented on native Odoo yet. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * Carlos Roca + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_history/__init__.py b/mail_history/__init__.py new file mode 100644 index 000000000..cc6b6354a --- /dev/null +++ b/mail_history/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import post_init_hook diff --git a/mail_history/__manifest__.py b/mail_history/__manifest__.py new file mode 100644 index 000000000..dd31c4884 --- /dev/null +++ b/mail_history/__manifest__.py @@ -0,0 +1,25 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Email History", + "summary": "Module to see old messages", + "version": "12.0.1.0.0", + "category": "Social Network", + "website": "http://github.com/OCA/social", + "author": "Tecnativa, " + "Odoo Community Association (OCA)", + "license": "AGPL-3", + 'installable': True, + "depends": [ + "mail", + ], + "qweb": [ + "static/src/xml/thread.xml", + "static/src/xml/discuss.xml", + ], + "data": [ + "views/assets.xml", + ], + "post_init_hook": "post_init_hook", +} diff --git a/mail_history/hooks.py b/mail_history/hooks.py new file mode 100644 index 000000000..6ef35c935 --- /dev/null +++ b/mail_history/hooks.py @@ -0,0 +1,18 @@ +def post_init_hook(cr, registry): + cr.execute( + """WITH sub as ( + SELECT mail_message_id, res_partner_id + FROM mail_message_res_partner_needaction_rel + ) + INSERT INTO mail_message_res_partner_needaction_rel + (mail_message_id, res_partner_id, is_read) + SELECT mm.id, mmrpr.res_partner_id, True + FROM mail_message mm + JOIN mail_message_res_partner_rel mmrpr ON mmrpr.mail_message_id = mm.id + WHERE mmrpr.res_partner_id NOT IN ( + SELECT res_partner_id FROM sub + ) AND mm.id NOT IN ( + SELECT mail_message_id FROM sub + ) + """ + ) diff --git a/mail_history/models/__init__.py b/mail_history/models/__init__.py new file mode 100644 index 000000000..5695e7f19 --- /dev/null +++ b/mail_history/models/__init__.py @@ -0,0 +1,2 @@ +from . import mail_message +from . import mail_notification diff --git a/mail_history/models/mail_message.py b/mail_history/models/mail_message.py new file mode 100644 index 000000000..a582be575 --- /dev/null +++ b/mail_history/models/mail_message.py @@ -0,0 +1,59 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models + + +class Message(models.Model): + _inherit = 'mail.message' + + @api.multi + def message_format(self): + """Override this method to add the notification fetching for the read messages. + """ + vals = super().message_format() + read_notifs = self.env['mail.notification'].sudo().search( + [ + ('mail_message_id', 'in', self.ids), + ('res_partner_id', '!=', False), + ('is_read', '=', True), + ] + ) + # Obtain partners per message + notif_dict = {} + for notif in read_notifs: + mid = notif.mail_message_id.id + notif_dict.setdefault(mid, []) + notif_dict[mid].append( + notif.res_partner_id.id + ) + # Fill history partner ids field + for message in vals: + if message['id'] in notif_dict: + message.update({ + 'history_partner_ids': notif_dict[ + message['id'] + ], + }) + return vals + + @api.model + def mark_all_as_read(self, channel_ids=None, domain=None): + """Override this method for injecting a context key for avoiding the removal of + mail.notification. We need to inject as well a true leaf in the domain for + avoiding a part of code in super that removes the records by sql. + """ + if not domain: + domain = [['id', '!=', False]] + return super( + Message, self.with_context(unlink_is_read=True) + ).mark_all_as_read(channel_ids=channel_ids, domain=domain) + + @api.multi + def set_message_done(self): + """Override this method for injecting a context key for avoiding the removal of + mail.notification. + """ + return super( + Message, self.with_context(unlink_is_read=True) + ).set_message_done() diff --git a/mail_history/models/mail_notification.py b/mail_history/models/mail_notification.py new file mode 100644 index 000000000..3655a5d15 --- /dev/null +++ b/mail_history/models/mail_notification.py @@ -0,0 +1,14 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class Notification(models.Model): + _inherit = 'mail.notification' + + def unlink(self): + if self.env.context.get('unlink_is_read'): + self.write({"is_read": True}) + else: + return super().unlink() diff --git a/mail_history/readme/CONFIGURE.rst b/mail_history/readme/CONFIGURE.rst new file mode 100644 index 000000000..1dbbf40ee --- /dev/null +++ b/mail_history/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +To use this module you have to manage the messages with Odoo. To do this you have to go +*Profile > Preferences*, select *Handle in Odoo* and Save the changes. diff --git a/mail_history/readme/CONTRIBUTORS.rst b/mail_history/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..3b98bebb6 --- /dev/null +++ b/mail_history/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Tecnativa `_: + + * Carlos Roca diff --git a/mail_history/readme/DESCRIPTION.rst b/mail_history/readme/DESCRIPTION.rst new file mode 100644 index 000000000..834b23467 --- /dev/null +++ b/mail_history/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module add the functionality to preserve the mails marked as read diff --git a/mail_history/readme/INSTALL.rst b/mail_history/readme/INSTALL.rst new file mode 100644 index 000000000..47dde68cc --- /dev/null +++ b/mail_history/readme/INSTALL.rst @@ -0,0 +1,2 @@ +When the module is installed a process is executed for recover the messages that was +mark as read yet. diff --git a/mail_history/readme/ROADMAP.rst b/mail_history/readme/ROADMAP.rst new file mode 100644 index 000000000..3ff3d556d --- /dev/null +++ b/mail_history/readme/ROADMAP.rst @@ -0,0 +1 @@ +This module will dissapear for v13.0 because it is implemented on native Odoo yet. diff --git a/mail_history/readme/USAGE.rst b/mail_history/readme/USAGE.rst new file mode 100644 index 000000000..958a3defc --- /dev/null +++ b/mail_history/readme/USAGE.rst @@ -0,0 +1,3 @@ +To see this module working, send some messages to the user that manage the emails with +Odoo. After that if you mark as read the messages they will dissapear from the inbox +and appear on the history section. diff --git a/mail_history/static/description/index.html b/mail_history/static/description/index.html new file mode 100644 index 000000000..25d60d061 --- /dev/null +++ b/mail_history/static/description/index.html @@ -0,0 +1,446 @@ + + + + + + +Email History + + + +
+

Email History

+ + +

Beta License: AGPL-3 OCA/social Translate me on Weblate Try me on Runbot

+

This module add the functionality to preserve the mails marked as read

+

Table of contents

+ +
+

Installation

+

When the module is installed a process is executed for recover the messages that was +mark as read yet.

+
+
+

Configuration

+

To use this module you have to manage the messages with Odoo. To do this you have to go +Profile > Preferences, select Handle in Odoo and Save the changes.

+
+
+

Usage

+

To see this module working, send some messages to the user that manage the emails with +Odoo. After that if you mark as read the messages they will dissapear from the inbox +and appear on the history section.

+
+
+

Known issues / Roadmap

+

This module will dissapear for v13.0 because it is implemented on native Odoo yet.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/social project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mail_history/static/src/js/discuss.js b/mail_history/static/src/js/discuss.js new file mode 100644 index 000000000..001c5da1f --- /dev/null +++ b/mail_history/static/src/js/discuss.js @@ -0,0 +1,31 @@ +/* Copyright 2020 Tecnativa - Carlos Roca + License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). */ +odoo.define('mail_history.Discuss', function (require) { + "use strict"; + + var Discuss = require('mail.Discuss'); + var core = require('web.core'); + + var QWeb = core.qweb; + + Discuss.include({ + + /** + * Render 'history' mailbox menu entry in Discuss + * + * @private + * @returns {jQueryElementt} + */ + _renderSidebar: function () { + var $sidebar = this._super.apply(this, arguments); + // Because Odoo implementation isn't designed to be inherited + // properly, we inject 'history' button using jQuery. + var $history_item = $(QWeb.render('mail_history.SidebarHistory', + {activeThreadID: + this._thread ? this._thread.getID() : undefined})); + $history_item.insertAfter( + $sidebar.find(".o_mail_discuss_title_main").filter(":last")); + return $sidebar; + }, + }); +}); diff --git a/mail_history/static/src/js/mail_manager.js b/mail_history/static/src/js/mail_manager.js new file mode 100644 index 000000000..f37e81944 --- /dev/null +++ b/mail_history/static/src/js/mail_manager.js @@ -0,0 +1,49 @@ +/* Copyright 2020 Tecnativa - Carlos Roca + License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). */ +odoo.define('mail_history.Manager', function (require) { + "use strict"; + + var Manager = require('mail.Manager'); + var MailManagerNotif = require('mail.Manager.Notification'); + var core = require('web.core'); + + var _t = core._t; + + Manager.include({ + + /** + * Adds the Mailbox history registry + * + * @override + */ + _updateMailboxesFromServer: function () { + this._super.apply(this, arguments); + this._addMailbox({ + id: 'history', + name: _t("History"), + }); + }, + }); + + MailManagerNotif.include({ + + /** + * Adds the read messages to the history mailbox + * + * @override + */ + _handlePartnerMarkAsReadNotification: function (data) { + var self = this; + var history = this.getMailbox('history'); + _.each(data.message_ids, function (messageID) { + var message = _.find(self._messages, function (msg) { + return msg.getID() === messageID; + }); + if (message) { + history.addMessage(message, []); + } + }); + return this._super.apply(this, arguments); + }, + }); +}); diff --git a/mail_history/static/src/js/mailbox.js b/mail_history/static/src/js/mailbox.js new file mode 100644 index 000000000..d75dfc377 --- /dev/null +++ b/mail_history/static/src/js/mailbox.js @@ -0,0 +1,24 @@ +/* Copyright 2020 Tecnativa - Carlos Roca + License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). */ +odoo.define('mail_history.Mailbox', function (require) { + "use strict"; + + var Mailbox = require('mail.model.Mailbox'); + + Mailbox.include({ + + /** + * @override + * @private + * @returns {Array} + * @throws on missing domain for the provided mailbox (mailboxes should + * always have a domain on messages to fetch) + */ + _getThreadDomain: function () { + if (this._id === 'mailbox_history') { + return [['needaction', '=', false]]; + } + return this._super.apply(this, arguments); + }, + }); +}); diff --git a/mail_history/static/src/js/message.js b/mail_history/static/src/js/message.js new file mode 100644 index 000000000..1b83fae58 --- /dev/null +++ b/mail_history/static/src/js/message.js @@ -0,0 +1,50 @@ +/* Copyright 2020 Tecnativa - Carlos Roca + License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). */ +odoo.define('mail_history.Message', function (require) { + "use strict"; + + var Message = require('mail.model.Message'); + var session = require('web.session'); + + Message.include({ + + /** + * Take the historyPartnerIDs that have to be added to History Mailbox + * + * @override + */ + init: function (parent, data) { + this._historyPartnerIDs = data.history_partner_ids || []; + return this._super.apply(this, arguments); + }, + + /** + * Process the mailbox History with the historyPartnerIDs + * + * @override + */ + _processMailboxes: function () { + if (_.contains(this._historyPartnerIDs, session.partner_id)) { + this._setHistory(true); + } + return this._super.apply(this, arguments); + }, + + /* + * Set whether the message is history or not. + * If it is history, the message is moved to the "History" mailbox. + * Note that this function only applies it locally, the server + * is not aware + * + * @private + * @param {boolean} history if set, the message is history + */ + _setHistory: function (history) { + if (history) { + this._addThread('mailbox_history'); + } else { + this.removeThread('mailbox_history'); + } + }, + }); +}); diff --git a/mail_history/static/src/xml/discuss.xml b/mail_history/static/src/xml/discuss.xml new file mode 100644 index 000000000..58bc2b6fe --- /dev/null +++ b/mail_history/static/src/xml/discuss.xml @@ -0,0 +1,9 @@ + + + +
+ History +
+
+
diff --git a/mail_history/static/src/xml/thread.xml b/mail_history/static/src/xml/thread.xml new file mode 100644 index 000000000..5e63e39cc --- /dev/null +++ b/mail_history/static/src/xml/thread.xml @@ -0,0 +1,11 @@ + + + + + +
No history messages
+
Messages marked as read will appear in the history.
+
+
+
+
diff --git a/mail_history/views/assets.xml b/mail_history/views/assets.xml new file mode 100644 index 000000000..04f8622e4 --- /dev/null +++ b/mail_history/views/assets.xml @@ -0,0 +1,19 @@ + + + +