Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD]mute_notifications_user_autosubscribe: Do not send notification …
Browse files Browse the repository at this point in the history
…to users autosubscribed trhough user_id field
manuelregidor committed Oct 9, 2024
1 parent ed82ad5 commit 6a9a3d4
Showing 18 changed files with 931 additions and 0 deletions.
118 changes: 118 additions & 0 deletions mute_notifications_user_autosubscribe/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
=====================================
Mute Notifications User Autosubscribe
=====================================

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

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

This module allows to select users and/or groups whose members do not
have to be sent notifications through the chatter. The models in which
these rules have to be applied can also be selected.

**Table of contents**

.. contents::
:local:

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

To configure this module you need to:

- Go to Settings > Technical > Auto Subscribe Mute
- Create a new instance or edit an exiting one. The following fields
are available:

- **Name**. The name of the rule.
- **Model**. Model in which the rule is applied. Only models with a
user_id field can be selected.
- **Users**. List of users which the rule will be applied to. The
users in this field will be autosubscribed to the document when
they are set in the user_id field but only with the "Muted"
subtype, which notifies nothing. Note that this rule will not be
applied when the user in the list set itself in the user_id field,
as the subscription in this case depends on the creation of the
document, not the autosubscription.
- **Groups**. List of groups which the rule will be applied to. The
users that belong to the groups in this field will be
autosubscribed to the document when they are set in the user_id
field but only with the "Muted" subtype, which notifies nothing.
Note that this rule will not be applied when a user in any of the
groups in the list set itself in the user_id field, as the
subscription in this case depends on the creation of the document,
not the autosubscription.
- **Notes**. Text field.

Usage
=====

In case a user that has been automatically applied the "Mute" message
subtype in a subscription through one of the rules provided in this
module needs to be sent notifications in a specific document, just edit
the subscription and select the subtypes for which the user has to be
sent notifications.

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:%20mute_notifications_user_autosubscribe%0Aversion:%2017.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
-------

* Sygel

Contributors
------------

- Manuel Regidor <manuel.regidor@sygel.es>
- Valentín Vinagre <valentin.vinagre@sygel.es>
- Harald Panten <harald.panten@sygel.es>

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 <https://github.com/OCA/social/tree/17.0/mute_notifications_user_autosubscribe>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions mute_notifications_user_autosubscribe/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
21 changes: 21 additions & 0 deletions mute_notifications_user_autosubscribe/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Mute Notifications User Autosubscribe",
"summary": "Do not send notifications to users autosubcribed through user_id field",
"version": "17.0.1.0.0",
"category": "Social",
"website": "https://github.com/OCA/social",
"author": "Sygel,Odoo Community Association (OCA)",
"license": "AGPL-3",
"installable": True,
"depends": [
"mail",
],
"data": [
"data/mail_message_subtype_data.xml",
"security/ir.model.access.csv",
"views/user_autosubscribe_mute_views.xml",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="muted" model="mail.message.subtype">
<field name="name">Mute</field>
<field name="default" eval="False" />
<field name="sequence">999</field>
</record>
</odoo>
5 changes: 5 additions & 0 deletions mute_notifications_user_autosubscribe/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import user_autosubscribe_mute
from . import mail_thread
41 changes: 41 additions & 0 deletions mute_notifications_user_autosubscribe/models/mail_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


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

def _mute_user_autosubscribe_vals(self, vals, user):
final_vals = vals
field = self._fields.get("user_id")
if field and user:
model = self.env["ir.model"]._get(self._name)
if self.env["user.autosubscribe.mute"].is_mute_user(model, user):
subtype = self.env.ref("mute_notifications_user_autosubscribe.muted")
partner_id = user.partner_id.id
final_vals = [p for p in vals if p[0] != partner_id]
final_vals += [
(p[0], [subtype.id], p[2]) for p in vals if p[0] == partner_id
]
return final_vals

def _message_auto_subscribe_followers(self, updated_values, default_subtype_ids):
vals = super()._message_auto_subscribe_followers(
updated_values, default_subtype_ids
)
if vals:
user_id = updated_values.get("user_id")
if user_id:
user = self.env["res.users"].browse(user_id)
partner_id = user.partner_id.id
# Do nothing in case the user is already subscribe.
# It happes, for example, when it is the user_id who creates
# the document
if (
partner_id not in self.message_partner_ids.ids
and updated_values.get("user_id")
):
vals = self._mute_user_autosubscribe_vals(vals, user)
return vals
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models


class UserAutosubscribeMute(models.Model):
_name = "user.autosubscribe.mute"
_description = "User Autosubscribe Mute"

def _get_user_models_domain(self):
models = (
self.env["ir.model.fields"]
.search([("name", "=", "user_id"), ("relation", "=", "res.users")])
.mapped("model_id")
)
return f"[('id', 'in', {models.ids})]"

name = fields.Char(required=True)
active = fields.Boolean(default=True)
model_id = fields.Many2one(
comodel_name="ir.model",
domain=_get_user_models_domain,
ondelete="cascade",
required=True,
)
user_ids = fields.Many2many(comodel_name="res.users", string="Users")
group_ids = fields.Many2many(comodel_name="res.groups", string="Groups")
notes = fields.Text()

_sql_constraints = [
(
"unique_model_id",
"UNIQUE(model_id)",
_("Model must be unique in User Autosubscribe Mute instances."),
)
]

@api.model
def is_mute_user(self, model, user):
mute = False
user_autosubscribe_mute = self.search([("model_id", "=", model.id)], limit=1)
if user_autosubscribe_mute:
groups = (
",".join(user_autosubscribe_mute.group_ids.get_external_id().values())
or ""
)
if user.id in user_autosubscribe_mute.user_ids.ids or (
groups and user.user_has_groups(groups)
):
mute = True
return mute
3 changes: 3 additions & 0 deletions mute_notifications_user_autosubscribe/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
9 changes: 9 additions & 0 deletions mute_notifications_user_autosubscribe/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
To configure this module you need to:

- Go to Settings > Technical > Auto Subscribe Mute
- Create a new instance or edit an exiting one. The following fields are available:
- **Name**. The name of the rule.
- **Model**. Model in which the rule is applied. Only models with a user_id field can be selected.
- **Users**. List of users which the rule will be applied to. The users in this field will be autosubscribed to the document when they are set in the user_id field but only with the "Muted" subtype, which notifies nothing. Note that this rule will not be applied when the user in the list set itself in the user_id field, as the subscription in this case depends on the creation of the document, not the autosubscription.
- **Groups**. List of groups which the rule will be applied to. The users that belong to the groups in this field will be autosubscribed to the document when they are set in the user_id field but only with the "Muted" subtype, which notifies nothing. Note that this rule will not be applied when a user in any of the groups in the list set itself in the user_id field, as the subscription in this case depends on the creation of the document, not the autosubscription.
- **Notes**. Text field.
3 changes: 3 additions & 0 deletions mute_notifications_user_autosubscribe/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Manuel Regidor \<<manuel.regidor@sygel.es>\>
- Valentín Vinagre \<<valentin.vinagre@sygel.es>\>
- Harald Panten \<<harald.panten@sygel.es>\>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to select users and/or groups whose members do not have to be sent notifications through the chatter. The models in which these rules have to be applied can also be selected.
1 change: 1 addition & 0 deletions mute_notifications_user_autosubscribe/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In case a user that has been automatically applied the "Mute" message subtype in a subscription through one of the rules provided in this module needs to be sent notifications in a specific document, just edit the subscription and select the subtypes for which the user has to be sent notifications.
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"
"user_autosubscribe_mute_access","user.autosubscribe.mute.access","model_user_autosubscribe_mute",base.group_user,1,0,0,0
"user_autosubscribe_mute_manage","user.autosubscribe.mute.manage","model_user_autosubscribe_mute",base.group_system,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
464 changes: 464 additions & 0 deletions mute_notifications_user_autosubscribe/static/description/index.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions mute_notifications_user_autosubscribe/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import test_mute_user_sutosubscribe
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo.tests import new_test_user

from odoo.addons.mail.tests.common import MailCommon


class TestMuteUserAutosubscribe(MailCommon):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.user_1 = new_test_user(
cls.env, login="user_1", groups="base.group_partner_manager,base.group_user"
)
cls.user_2 = new_test_user(
cls.env, login="user_2", groups="base.group_partner_manager,base.group_user"
)
cls.user_autosubscribe_mute = cls.env["user.autosubscribe.mute"].create(
{
"name": "Mute Contact Notification",
"model_id": cls.env["ir.model"]._get("res.partner").id,
}
)

def send_email_mute_autosubscribe(cls, doc, user_id, body):
with cls.mock_mail_gateway():
return doc.with_user(user_id.id).message_post(
body=body, subtype_xmlid="mail.mt_comment"
)

def test_do_not_mute_user_field(self):
user_2_partner = self.user_2.partner_id
# Create new contact with user_1.
contact = (
self.env["res.partner"]
.with_user(self.user_1.id)
.create({"name": "Contact"})
)
# Check user_2 is not subscribed
self.assertFalse(user_2_partner.id in contact.message_partner_ids.ids)
# Set user_2 as the Salesperson
# Check user_2 is now subscribed
contact.write({"user_id": self.user_2.id})
self.assertTrue(user_2_partner.id in contact.message_partner_ids.ids)
# Post message with user_1
message = self.send_email_mute_autosubscribe(contact, self.user_1, "Test-1")
# user_2 has been sent a notification
receivers = message.notification_ids.mapped("res_partner_id")
self.assertTrue(user_2_partner.id in receivers.ids)

def test_mute_users_field(self):
user_2_partner = self.user_2.partner_id
# Mute user_2 from salesperson autosubscription in res.partner
self.user_autosubscribe_mute.write({"user_ids": [self.user_2.id]})
# Create new contact with user_1 and set user_2 and set user_2 as the
# salesperson
contact = (
self.env["res.partner"]
.with_user(self.user_1.id)
.create({"name": "Contact", "user_id": self.user_2.id})
)
# Check user_2 is subscribed
self.assertTrue(user_2_partner.id in contact.message_partner_ids.ids)
# Post message with user_1
message = self.send_email_mute_autosubscribe(contact, self.user_1, "Test-2")
# user_2 has NOT been sent a notification
receivers = message.notification_ids.mapped("res_partner_id")
self.assertFalse(user_2_partner.id in receivers.ids)
# user_2 subscription only contains "Mute" subtype
follower = self.env["mail.followers"].search(
[
("res_model", "=", "res.partner"),
("res_id", "=", contact.id),
("partner_id", "=", user_2_partner.id),
],
limit=1,
)
self.assertEqual(len(follower.subtype_ids), 1)
self.assertEqual(
follower.subtype_ids[0],
self.env.ref("mute_notifications_user_autosubscribe.muted"),
)

def test_mute_groups_field(self):
user_2_partner = self.user_2.partner_id
# Mute group base.group_partner_manager from salesperson
# autosubscription in res.partner
self.user_autosubscribe_mute.write(
{"group_ids": [self.env.ref("base.group_partner_manager").id]}
)
# Create new contact with user_1 and set user_2 and set user_2 as the
# salesperson
contact = (
self.env["res.partner"]
.with_user(self.user_1.id)
.create({"name": "Contact", "user_id": self.user_2.id})
)
# Check user_2 is subscribed
self.assertTrue(user_2_partner.id in contact.message_partner_ids.ids)
# Post message with user_1
message = self.send_email_mute_autosubscribe(contact, self.user_1, "Test-2")
# user_2 has NOT been sent a notification
receivers = message.notification_ids.mapped("res_partner_id")
self.assertFalse(user_2_partner.id in receivers.ids)
# user_2 subscription only contains "Mute" subtype
follower = self.env["mail.followers"].search(
[
("res_model", "=", "res.partner"),
("res_id", "=", contact.id),
("partner_id", "=", user_2_partner.id),
],
limit=1,
)
self.assertEqual(len(follower.subtype_ids), 1)
self.assertEqual(
follower.subtype_ids[0],
self.env.ref("mute_notifications_user_autosubscribe.muted"),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Manuel Regidor <manuel.regidor@sygel.es>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<record id="user_autosubscribe_mute_form_view" model="ir.ui.view">
<field name="name">user.autosubscribe.mute.form.view</field>
<field name="model">user.autosubscribe.mute</field>
<field name="arch" type="xml">
<form string="User Autosubscribe Mute">
<sheet>
<widget
name="web_ribbon"
title="Archived"
bg_color="text-bg-danger"
invisible="active"
/>
<div class="oe_title">
<label for="name" class="oe_edit_only" />
<h1>
<field name="name" />
</h1>
</div>
<group>
<group>
<field name="active" invisible="1" />
<field
name="model_id"
options="{'no_create_edit': True, 'no_create': True}"
/>
</group>
<group>
<field
name="user_ids"
widget="many2many_tags"
options="{'no_create_edit': True, 'no_create': True}"
/>
<field
name="group_ids"
widget="many2many_tags"
options="{'no_create_edit': True, 'no_create': True}"
/>
</group>
</group>
<group>
<field name="notes" />
</group>
</sheet>
</form>
</field>
</record>
<record id="user_autosubscribe_mute_tree_view" model="ir.ui.view">
<field name="name">user.autosubscribe.mute.tree.view</field>
<field name="model">user.autosubscribe.mute</field>
<field name="arch" type="xml">
<tree>
<field name="name" />
<field name="model_id" />
<field name="user_ids" />
<field name="group_ids" />
</tree>
</field>
</record>
<record id="user_autosubscribe_mute_action" model="ir.actions.act_window">
<field name="name">User Autosubscribe Mute</field>
<field name="res_model">user.autosubscribe.mute</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem
parent="mail.mail_menu_technical"
id="menu_user_autosubscribe_mute"
name="User Autosubscribe Mute"
action="user_autosubscribe_mute_action"
groups="base.group_system"
/>
</odoo>

0 comments on commit 6a9a3d4

Please sign in to comment.