Skip to content

Commit

Permalink
Add mail_message_purge
Browse files Browse the repository at this point in the history
  • Loading branch information
TDu committed Nov 18, 2024
1 parent 5101340 commit 44a400e
Show file tree
Hide file tree
Showing 15 changed files with 809 additions and 0 deletions.
94 changes: 94 additions & 0 deletions mail_message_purge/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
==================
Mail Message Purge
==================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:38d57fe022ecc270b0aba5103c3450da0e87c68cc969792d05339f782ec79a4b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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/14.0/mail_message_purge
:alt: OCA/social
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/social-14-0/social-14-0-mail_message_purge
: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=14.0
:alt: Try me on Runboat

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

Over the years of using an Odoo instance, its database can grow to a very large size.
Message from the chatter can quickly add up to this load and some of them have no added
value after their related records has been settled.

This module allows to configure mail message retention policies on a per model basis.
Go to Settings > Technical > Discuss > Message Purge to add some configuration.

A cron will run daily to purge old messages following the configurations. A maximum of
1000 messages by model will be delete by day to avoid blocking normal Odoo execution.

**Table of contents**

.. contents::
:local:

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_message_purge%0Aversion:%2014.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
~~~~~~~

* Camptocamp

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

* `Camptocamp <https://www.camptocamp.com>`_

* Thierry Ducrest <thierry.ducrest@camptocamp.com>

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-TDu| image:: https://github.com/TDu.png?size=40px
:target: https://github.com/TDu
:alt: TDu

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

|maintainer-TDu|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions mail_message_purge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions mail_message_purge/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

{
"name": "Mail Message Purge",
"summary": "Delete old mail messages based on configuration.",
"version": "14.0.1.0.0",
"category": "Mail",
"author": "Camptocamp,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social",
"maintainers": ["TDu"],
"license": "AGPL-3",
"installable": True,
"depends": ["mail"],
"data": [
"data/ir_cron.xml",
"security/ir.model.access.csv",
"views/mail_message_purge_views.xml",
],
}
22 changes: 22 additions & 0 deletions mail_message_purge/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Camptocamp SA
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo noupdate="1">

<record model="ir.cron" id="cron_purge_mail_message">
<field name='name'>Purge Mail Messages</field>
<field name='interval_number'>1</field>
<field name='interval_type'>days</field>
<field name="numbercall">-1</field>
<field name="active" eval="True" />
<field name="doall" eval="False" />
<field
name="nextcall"
eval="(datetime.now() + timedelta(days=1)).strftime('%Y-%m-%d 00:00:00')"
/>
<field name="model_id" ref="mail_message_purge.model_mail_message_purge" />
<field name="state">code</field>
<field name="code">model._cron_purge_mail_message()</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions mail_message_purge/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import mail_message_purge
85 changes: 85 additions & 0 deletions mail_message_purge/models/mail_message_purge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

import logging

from dateutil.relativedelta import relativedelta

from odoo import api, fields, models
from odoo.osv.expression import AND
from odoo.tools import safe_eval

_logger = logging.getLogger(__name__)


class MailMessagePurge(models.Model):
_name = "mail.message.purge"
_description = "Mail Message Purge"

res_model = fields.Many2one(
comodel_name="ir.model",
domain=[("transient", "=", False), ("is_mail_thread", "=", True)],
string="Target model",
ondelete="cascade",
required=True,
)
model_name = fields.Char(related="res_model.model", readonly=True)
retention_period = fields.Integer(
default=5, required=True, help="Retention period in years"
)
domain = fields.Char(string="Filtering domain")
mail_message_subtype_ids = fields.Many2many(
comodel_name="mail.message.subtype",
domain="['|', ('res_model', '=', model_name), ('res_model', '=', False)]",
string="Subtypes",
)
include_user_notification = fields.Boolean()
active = fields.Boolean(default=True)

def _domain_mail_message_purge(self):
"""Generate a domain to search for mail message to purge."""
self.ensure_one()
domain = [
("model", "=", self.res_model.model),
(
"create_date",
"<",
fields.Date.today() - relativedelta(years=self.retention_period),
),
]
if self.include_user_notification:
domain = AND(
[
domain,
[("message_type", "in", ("notification", "user_notification"))],
]
)
else:
domain = AND([domain, [("message_type", "=", "notification")]])
if self.mail_message_subtype_ids:
domain = AND(
[domain, [("subtype_id", "in", self.mail_message_subtype_ids.ids)]]
)
if self.domain:
record_ids = (
self.env[self.res_model.model]
.search(safe_eval.safe_eval(self.domain))
.ids
)
if record_ids:
domain = AND([domain, [("res_id", "in", record_ids)]])
return domain

def _purge(self):
domain = self._domain_mail_message_purge()
messages = self.env["mail.message"].search(domain, limit=1000)
_logger.info(
f"Purging {len(messages)} messages for {self.res_model.model}"
)
messages.unlink()

@api.model
def _cron_purge_mail_message(self):
records = self.env["mail.message.purge"].search([])
for record in records:
record._purge()
3 changes: 3 additions & 0 deletions mail_message_purge/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Camptocamp <https://www.camptocamp.com>`_

* Thierry Ducrest <thierry.ducrest@camptocamp.com>
9 changes: 9 additions & 0 deletions mail_message_purge/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Over the years of using an Odoo instance, its database can grow to a very large size.
Message from the chatter can quickly add up to this load and some of them have no added
value after their related records has been settled.

This module allows to configure mail message retention policies on a per model basis.
Go to Settings > Technical > Discuss > Message Purge to add some configuration.

A cron will run daily to purge old messages following the configurations. A maximum of
1000 messages by model will be delete by day to avoid blocking normal Odoo execution.
3 changes: 3 additions & 0 deletions mail_message_purge/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_mail_message_purge_all,mail.message.purge.all,model_mail_message_purge,,1,0,0,0
access_mail_message_purge_admin,mail.message.purge.admin,model_mail_message_purge,base.group_system,1,1,1,1
Loading

0 comments on commit 44a400e

Please sign in to comment.