Skip to content

Commit

Permalink
[ADD] automation_mail_gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Jun 19, 2024
1 parent cb80ede commit c90406f
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 0 deletions.
1 change: 1 addition & 0 deletions automation_mail_gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions automation_mail_gateway/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Automation Mail Gateway",
"summary": """
Integrate automation and mail gateway""",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "Dixmit,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/automation",
"depends": ["automation_oca", "mail_gateway"],
"data": [
"views/automation_configuration_activity.xml",
"views/automation_configuration.xml",
"views/automation_record.xml",
],
"demo": [],
}
4 changes: 4 additions & 0 deletions automation_mail_gateway/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import automation_configuration_activity
from . import automation_record_activity
from . import mail_notification
from . import mail_gateway_abstract
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AutomationConfigurationActivity(models.Model):

_inherit = "automation.configuration.activity"
activity_type = fields.Selection(
selection_add=[("gateway", "Gateway")], ondelete={"gateway": "cascade"}
)
gateway_id = fields.Many2one("mail.gateway")
gateway_author_id = fields.Many2one("res.users")
gateway_template_id = fields.Many2one(
"mail.template", domain="[('model_id', '=', model_id)]"
)
gateway_field_id = fields.Many2one("ir.model.fields")

def _get_mail_activities(self):
return super()._get_mail_activities() + ["gateway"]
63 changes: 63 additions & 0 deletions automation_mail_gateway/models/automation_record_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2024 Dixmit
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import threading

from odoo import _, models
from odoo.exceptions import UserError


class AutomationRecordActivity(models.Model):

_inherit = "automation.record.activity"

def _run_gateway(self):
record = self.env[self.record_id.model].browse(self.record_id.res_id)
partner = record[self.configuration_activity_id.gateway_field_id.name]
channel = partner.gateway_channel_ids.filtered(
lambda r: r.gateway_id == self.configuration_activity_id.gateway_id
)
if not channel:
raise UserError(
_("Partner has no channel to send with gateway %s")
% self.configuration_activity_id.gateway_id.name
)
channel = channel[0]
author_id = self.configuration_activity_id.gateway_author_id.id
composer_values = {
"author_id": author_id,
"record_name": False,
"model": self.record_id.model,
"composition_mode": "mass_post",
"template_id": self.configuration_activity_id.gateway_template_id.id,
"automation_record_activity_id": self.id,
}
res_ids = [self.record_id.res_id]
composer = (
self.env["mail.compose.message"]
.with_context(active_ids=res_ids)
.create(composer_values)
)
composer.write(
composer._onchange_template_id(
self.configuration_activity_id.gateway_template_id.id,
"mass_post",
self.record_id.model,
self.record_id.res_id,
)["value"]
)
# composer.body =
extra_context = self._run_mail_context()
composer = composer.with_context(active_ids=res_ids, **extra_context)
# auto-commit except in testing mode
auto_commit = not getattr(threading.current_thread(), "testing", False)
if not self.is_test:
# We just abort the sending, but we want to check how the generation works
_mails, messages = composer._action_send_mail(auto_commit=auto_commit)
messages.with_context(
automation_record_activity_id=self.id
)._send_to_gateway_thread(channel)
self.mail_status = "sent"
self.message_id = messages.message_id
self._fill_childs()
return
51 changes: 51 additions & 0 deletions automation_mail_gateway/models/mail_gateway_abstract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Dixmit
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import re

import markupsafe
import werkzeug.urls

from odoo import models, tools


class MailGatewayAbstract(models.AbstractModel):
_inherit = "mail.gateway.abstract"

def _post_process_reply(self, related_message):
msg_references = related_message.mapped("message_id") + related_message.mapped(
"gateway_message_id.message_id"
)
records = self.env["automation.record.activity"].search(
[("message_id", "in", msg_references)]
)
records._set_mail_reply()
return super()._post_process_reply(related_message)

def _get_message_body(self, record):
body = super()._get_message_body(record)
if self.env.context.get("automation_record_activity_id"):
body = self.env["mail.render.mixin"]._shorten_links(body, {}, blacklist=[])
record_activity = self.env["automation.record.activity"].browse(
self.env.context.get("automation_record_activity_id")
)
token = record_activity._get_mail_tracking_token()
for match in set(re.findall(tools.URL_REGEX, body)):
href = match[0]
url = match[1]

parsed = werkzeug.urls.url_parse(url, scheme="http")
if parsed.scheme.startswith("http") and parsed.path.startswith("/r/"):
new_href = href.replace(
url,
"%s/au/%s/%s"
% (
url,
str(self.env.context.get("automation_record_activity_id")),
token,
),
)
body = body.replace(
markupsafe.Markup(href), markupsafe.Markup(new_href)
)
return body
19 changes: 19 additions & 0 deletions automation_mail_gateway/models/mail_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 Dixmit
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class MailNotification(models.Model):
_inherit = "mail.notification"

def _set_read_gateway(self):
res = super()._set_read_gateway()
msg_references = self.mapped("mail_message_id.message_id") + self.mapped(
"mail_message_id.gateway_message_id.message_id"
)
records = self.env["automation.record.activity"].search(
[("message_id", "in", msg_references)]
)
records._set_mail_open()
return res
1 change: 1 addition & 0 deletions automation_mail_gateway/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Enric Tobella ([Dixmit](https://www.dixmit.com/))
1 change: 1 addition & 0 deletions automation_mail_gateway/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to send messages using gateways directly on Automation.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 170 additions & 0 deletions automation_mail_gateway/views/automation_configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="automation_configuration_form_view">
<field name="model">automation.configuration</field>
<field
name="inherit_id"
ref="automation_oca.automation_configuration_form_view"
/>
<field name="arch" type="xml">
<xpath
expr="//field[@name='automation_activity_ids']/kanban//button[hasclass('o_automation_kanban_header_icon')]"
position="inside"
>
<span
class="fa fa-paper-plane"
t-if="record.activity_type.raw_value == 'gateway'"
title="Gateway"
role="img"
aria-label="Gateway"
/>
</xpath>
<!--
<div
t-if="!read_only_mode"
class="o_automation_kanban_child_add text-center"
>
<div
class="o_automation_kanban_child_add_title card-footer"
>
<i
class="fa fa-plus-circle"
/> Add child activity
</div>
<div
class="o_automation_kanban_child_add_buttons"
>
<div
trigger-type="activity"
class="text-success o_automation_kanban_child_add_button"
>
<i
class="fa fa-code-fork fa-rotate-180 fa-flip-vertical"
title="Add Another Activity"
role="img"
aria-label="Add Another Activity"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'activity'"
trigger-type="activity_done"
class="text-success o_automation_kanban_child_add_button"
>
<i
class="fa fa-clock-o"
title="Activity Done"
role="img"
aria-label="Activity Done"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'activity'"
trigger-type="activity_not_done"
class="text-danger o_automation_kanban_child_add_button"
>
<i
class="fa fa-clock-o"
title="Activity Not Done"
role="img"
aria-label="Activity Not Done"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_open"
class="text-success o_automation_kanban_child_add_button"
>
<i
class="fa fa-envelope-open-o"
title="Opened"
role="img"
aria-label="Opened"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_not_open"
class="text-danger o_automation_kanban_child_add_button"
>
<i
class="fa fa-envelope-open-o "
title="Not Opened"
role="img"
aria-label="Not Opened"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_reply"
type="new_child"
class="text-success o_automation_kanban_child_add_button"
>
<i
class="fa fa-reply"
title="Replied"
role="img"
aria-label="Replied"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_not_reply"
class="text-danger o_automation_kanban_child_add_button"
>
<i
class="fa fa-reply"
title="Not Replied"
role="img"
aria-label="Not Replied"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_click"
class="text-success o_automation_kanban_child_add_button"
>
<i
class="fa fa-hand-pointer-o"
title="Clicked"
role="img"
aria-label="Clicked"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_not_click"
class="text-danger o_automation_kanban_child_add_button"
>
<i
class="fa fa-hand-pointer-o"
title="Not Clicked"
role="img"
aria-label="Not Clicked"
/>
</div>
<div
t-if="record.activity_type.raw_value == 'mail'"
trigger-type="mail_bounce"
class="text-danger o_automation_kanban_child_add_button"
>
<i
class="fa fa-exclamation-circle"
title="Bounced"
role="img"
aria-label="Bounced"
/>
</div>
</div>
</div>
</div>
</div>
</templates>
</kanban>-->
</field>
</record>

</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="automation_configuration_activity_form_view">
<field name="model">automation.configuration.activity</field>
<field
name="inherit_id"
ref="automation_oca.automation_configuration_activity_form_view"
/>
<field name="arch" type="xml">
<xpath expr="//field[@name='activity_type']/../.." position="inside">

<group attrs="{'invisible':[('activity_type', '!=', 'gateway')]}">
<field
name="gateway_id"
attrs="{'required': [('activity_type', '=', 'gateway')]}"
/>
<field
name="gateway_template_id"
attrs="{'required': [('activity_type', '=', 'gateway')]}"
/>
<field name="gateway_author_id" />
<field
name="gateway_field_id"
domain="[('model_id', '=', model_id), ('relation', '=', 'res.partner')]"
attrs="{'required': [('activity_type', '=', 'gateway')]}"
/>
</group>
</xpath>
</field>
</record>



</odoo>
Loading

0 comments on commit c90406f

Please sign in to comment.