Skip to content

Commit

Permalink
[ADD] mail_post_defer
Browse files Browse the repository at this point in the history
  • Loading branch information
nobuQuartile committed Dec 20, 2024
1 parent 8bce51e commit 3f9bc86
Show file tree
Hide file tree
Showing 22 changed files with 1,051 additions and 0 deletions.
142 changes: 142 additions & 0 deletions mail_post_defer/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
========================
Deferred Message Posting
========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:7574728536782d4db17b3f989c453d122d354668e822d5036e8247223c5e5c14
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github
:target: https://github.com/OCA/social/tree/15.0/mail_post_defer
:alt: OCA/social
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/social-15-0/social-15-0-mail_post_defer
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module enhances mail threads by using the mail queue by default.

Without this module, Odoo attempts to notify recipients of your message immediately.
If your mail server is slow or you have many followers, this can mean a lot of time.
Install this module and make Odoo more snappy!

All emails will be kept in the outgoing queue by at least 30 seconds,
giving you some time to re-think what you wrote. During that time,
you can still delete the message and start again.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Configuration
=============

You need to do nothing. The module is configured appropriately out of the box.

The mail queue processing is made by a cron job. This is normal Odoo behavior,
not specific to this module. However, since you will start using that queue for
every message posted by any user in any thread, this module configures that job
to execute every minute by default.

You can still change that cadence after installing the module (although it is
not recommended). To do so:

#. Log in with an administrator user.
#. Activate developer mode.
#. Go to *Settings > Technical > Automation > Scheduled Actions*.
#. Edit the action named "Mail: Email Queue Manager".
#. Lower down the frequency in the field *Execute Every*. Recommended: 1 minute.

Usage
=====

To use this module, you need to:

#. Go to the form view of any record that has a mail thread. It can be a partner, for example.
#. Post a message.

The mail is now in the outgoing mail queue. It will be there for at least 30
seconds. It will be really sent the next time the "Mail: Email Queue Manager"
cron job is executed.

While the message has not been yet sent:

#. Hover over the little envelope. You will see a paper airplane icon,
indicating it is still outgoing.
#. Hover over the message and click on the little trash icon to delete it.
Mails will not be sent.

Known issues / Roadmap
======================

* Add minimal deferring time configuration if it ever becomes necessary. See
https://github.com/OCA/social/pull/1001#issuecomment-1461581573 for the
rationale behind current hardcoded value of 30 seconds.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/social/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/social/issues/new?body=module:%20mail_post_defer%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Moduon

Contributors
~~~~~~~~~~~~

* Jairo Llopis (https://www.moduon.team/)

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.

.. |maintainer-Yajo| image:: https://github.com/Yajo.png?size=40px
:target: https://github.com/Yajo
:alt: Yajo

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-Yajo|

This module is part of the `OCA/social <https://github.com/OCA/social/tree/15.0/mail_post_defer>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions mail_post_defer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from .hooks import post_init_hook
25 changes: 25 additions & 0 deletions mail_post_defer/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022-2023 Moduon Team S.L. <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
{
"name": "Deferred Message Posting",
"summary": "Faster and cancellable outgoing messages",
"version": "15.0.1.0.0",
"development_status": "Alpha",
"category": "Productivity/Discuss",
"website": "https://github.com/OCA/social",
"author": "Moduon, Odoo Community Association (OCA)",
"maintainers": ["Yajo"],
"license": "LGPL-3",
"depends": [
"mail",
],
"post_init_hook": "post_init_hook",
"assets": {
"web.assets_backend": [
"mail_post_defer/static/src/**/*.js",
],
"web.assets_qweb": [
"mail_post_defer/static/src/**/*.xml",
],
},
}
23 changes: 23 additions & 0 deletions mail_post_defer/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2022-2023 Moduon Team S.L. <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def post_init_hook(cr, registry):
"""Increase cadence of mail queue cron."""
env = api.Environment(cr, SUPERUSER_ID, {})
try:
cron = env.ref("mail.ir_cron_mail_scheduler_action")
except ValueError:
_logger.warning(
"Couldn't find the standard mail scheduler cron. "
"Maybe no mails will be ever sent!"
)
else:
_logger.info("Setting mail queue cron cadence to 1 minute")
cron.interval_number = 1
cron.interval_type = "minutes"
31 changes: 31 additions & 0 deletions mail_post_defer/i18n/mail_post_defer.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * mail_post_defer
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: mail_post_defer
#: model:ir.model,name:mail_post_defer.model_mail_thread
msgid "Email Thread"
msgstr ""

#. module: mail_post_defer
#: model:ir.model,name:mail_post_defer.model_mail_message
msgid "Message"
msgstr ""

#. module: mail_post_defer
#. openerp-web
#: code:addons/mail_post_defer/static/src/xml/message.xml:0
#, python-format
msgid "messageActionList.message.canBeEdited"
msgstr ""
2 changes: 2 additions & 0 deletions mail_post_defer/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import mail_message
from . import mail_thread
18 changes: 18 additions & 0 deletions mail_post_defer/models/mail_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022-2023 Moduon Team S.L. <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

from odoo import models


class MailMessage(models.Model):
_inherit = "mail.message"

def _cleanup_side_records(self):
"""Delete pending outgoing mails."""
self.mail_ids.filtered(lambda mail: mail.state == "outgoing").unlink()
return super()._cleanup_side_records()

def _update_content(self, body, attachment_ids):
"""Let checker know about empty body."""
_self = self.with_context(deleting=body == "")
return super(MailMessage, _self)._update_content(body, attachment_ids)
43 changes: 43 additions & 0 deletions mail_post_defer/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2022-2023 Moduon Team S.L. <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

from datetime import timedelta

from odoo import fields, models


class MailThread(models.AbstractModel):
_inherit = "mail.thread"

def message_post(self, **kwargs):
"""Post messages using queue by default."""
_self = self
force_send = self.env.context.get("mail_notify_force_send") or kwargs.get(
"force_send", False
)
kwargs.setdefault("force_send", force_send)
if not force_send:
# If deferring message, give the user some minimal time to revert it
_self = self.with_context(mail_defer_seconds=30)
return super(MailThread, _self).message_post(**kwargs)

def _notify_by_email_add_values(self, base_mail_values):
"""Defer emails by default."""
result = super()._notify_by_email_add_values(base_mail_values)
defer_seconds = self.env.context.get("mail_defer_seconds")
if defer_seconds:
result.setdefault(
"scheduled_date",
fields.Datetime.now() + timedelta(seconds=defer_seconds),
)
return result

def _check_can_update_message_content(self, message):
"""Allow deleting unsent mails."""
if (
self.env.context.get("deleting")
and set(message.notification_ids.mapped("notification_status")) == {"ready"}
and set(message.mail_ids.mapped("state")) == {"outgoing"}
):
return
return super()._check_can_update_message_content(message)
15 changes: 15 additions & 0 deletions mail_post_defer/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
You need to do nothing. The module is configured appropriately out of the box.

The mail queue processing is made by a cron job. This is normal Odoo behavior,
not specific to this module. However, since you will start using that queue for
every message posted by any user in any thread, this module configures that job
to execute every minute by default.

You can still change that cadence after installing the module (although it is
not recommended). To do so:

#. Log in with an administrator user.
#. Activate developer mode.
#. Go to *Settings > Technical > Automation > Scheduled Actions*.
#. Edit the action named "Mail: Email Queue Manager".
#. Lower down the frequency in the field *Execute Every*. Recommended: 1 minute.
1 change: 1 addition & 0 deletions mail_post_defer/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Jairo Llopis (https://www.moduon.team/)
9 changes: 9 additions & 0 deletions mail_post_defer/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This module enhances mail threads by using the mail queue by default.

Without this module, Odoo attempts to notify recipients of your message immediately.
If your mail server is slow or you have many followers, this can mean a lot of time.
Install this module and make Odoo more snappy!

All emails will be kept in the outgoing queue by at least 30 seconds,
giving you some time to re-think what you wrote. During that time,
you can still delete the message and start again.
3 changes: 3 additions & 0 deletions mail_post_defer/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Add minimal deferring time configuration if it ever becomes necessary. See
https://github.com/OCA/social/pull/1001#issuecomment-1461581573 for the
rationale behind current hardcoded value of 30 seconds.
15 changes: 15 additions & 0 deletions mail_post_defer/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
To use this module, you need to:

#. Go to the form view of any record that has a mail thread. It can be a partner, for example.
#. Post a message.

The mail is now in the outgoing mail queue. It will be there for at least 30
seconds. It will be really sent the next time the "Mail: Email Queue Manager"
cron job is executed.

While the message has not been yet sent:

#. Hover over the little envelope. You will see a paper airplane icon,
indicating it is still outgoing.
#. Hover over the message and click on the little trash icon to delete it.
Mails will not be sent.
Binary file added mail_post_defer/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3f9bc86

Please sign in to comment.