Skip to content

Commit

Permalink
[MIG] mail_broker: Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Dec 27, 2022
1 parent 51b3e21 commit fc80217
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 542 deletions.
2 changes: 1 addition & 1 deletion mail_broker/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Mail Broker",
"summary": """
Set a broker""",
"version": "13.0.1.0.0",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "Creu Blanca,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social",
Expand Down
10 changes: 7 additions & 3 deletions mail_broker/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class NewMailChatController(MailChatController):
def _poll(self, dbname, channels, last, options):
if request.session.uid:
if request.env.user.has_group("mail_broker.broker_user"):
for bot in request.env["mail.broker"].search([]):
channels.append((request.db, "mail.broker", bot.id))
return super()._poll(dbname, channels, last, options)
channels = list(channels)
for channel in request.env["mail.channel"].search(
[("public", "=", "broker")]
):
channels.append((request.db, "mail.channel", channel.id))
result = super()._poll(dbname, channels, last, options)
return result
6 changes: 3 additions & 3 deletions mail_broker/models/mail_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _compute_webhook_url(self):

def _get_channel_id(self, chat_token):
return (
self.env["mail.broker.channel"]
self.env["mail.channel"]
.search(
[("token", "=", str(chat_token)), ("broker_id", "=", self.id)],
limit=1,
Expand Down Expand Up @@ -94,7 +94,7 @@ def broker_fetch_slot(self):
"channel_name": "broker_%s" % record.id,
"threads": [
thread._get_thread_data()
for thread in self.env["mail.broker.channel"].search(
for thread in self.env["mail.channel"].search(
[("show_on_app", "=", True), ("broker_id", "=", record.id)]
)
],
Expand All @@ -107,7 +107,7 @@ def channel_search(self, name):
domain = [("broker_id", "=", self.id)]
if name:
domain += [("name", "ilike", "%" + name + "%")]
return self.env["mail.broker.channel"].search(domain).read(["name"])
return self.env["mail.channel"].search(domain).read(["name"])

def write(self, vals):
res = super(MailBroker, self).write(vals)
Expand Down
96 changes: 62 additions & 34 deletions mail_broker/models/mail_broker_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,32 @@
from odoo import api, fields, models


class MailBrokerChannel(models.Model):
_name = "mail.broker.channel"
_description = "Mail Broker Channel"
class MailChannel(models.Model):
_inherit = "mail.channel"

name = fields.Char(required=True)
active = fields.Boolean(default=True)
token = fields.Char(required=True)
broker_id = fields.Many2one("mail.broker", required=True)
message_ids = fields.One2many(
token = fields.Char()
broker_id = fields.Many2one("mail.broker")
broker_message_ids = fields.One2many(
"mail.message.broker",
inverse_name="channel_id",
)
mail_message_ids = fields.One2many(
"mail.message",
inverse_name="broker_channel_id",
)
last_message_date = fields.Datetime(
compute="_compute_message_data",
store=True,
)
unread = fields.Integer(
public = fields.Selection(
selection_add=[("broker", "Broker")], ondelete={"broker": "set default"}
)
channel_type = fields.Selection(
selection_add=[("broker", "Broker")], ondelete={"broker": "set default"}
)
broker_unread = fields.Integer(
compute="_compute_message_data",
store=True,
)
broker_token = fields.Char(related="broker_id.token", store=True, required=False)
show_on_app = fields.Boolean()
partner_id = fields.Many2one("res.partner")
message_main_attachment_id = fields.Many2one(
string="Main Attachment",
comodel_name="ir.attachment",
index=True,
copy=False,
)
broker_partner_id = fields.Many2one("res.partner")

def message_fetch(self, domain=False, limit=30):
self.ensure_one()
Expand All @@ -52,9 +45,9 @@ def message_fetch(self, domain=False, limit=30):
)

@api.depends(
"mail_message_ids",
"mail_message_ids.date",
"mail_message_ids.broker_unread",
"message_ids",
"message_ids.date",
"message_ids.broker_unread",
)
def _compute_message_data(self):
for r in self:
Expand All @@ -67,7 +60,7 @@ def _compute_message_data(self):
)
.date
)
r.unread = self.env["mail.message"].search_count(
r.broker_unread = self.env["mail.message"].search_count(
[("broker_channel_id", "=", r.id), ("broker_unread", "=", True)]
)

Expand All @@ -78,34 +71,41 @@ def _get_thread_data(self):
"name": self.name,
"last_message_date": self.last_message_date,
"channel_type": "broker_thread",
"unread": self.unread,
"unread": self.broker_unread,
"broker_id": self.broker_id.id,
}

def _broker_message_post_vals(self, body, **kwargs):
subtype_id = kwargs.get("subtype_id", False)
def _broker_message_post_vals(
self,
body,
subtype_id=False,
author_id=False,
date=False,
message_id=False,
**kwargs
):
if not subtype_id:
subtype = kwargs.get("subtype") or "mt_note"
if "." not in subtype:
subtype = "mail.%s" % subtype
subtype_id = self.env["ir.model.data"].xmlid_to_res_id(subtype)
vals = {
"channel_id": self.id,
"channel_ids": [(4, self.id)],
"body": body,
"subtype_id": subtype_id,
"model": self._name,
"res_id": self.id,
"broker_type": self.broker_id.broker_type,
}
if kwargs.get("author_id", False):
vals["author_id"] = kwargs["author_id"]
if "date" in kwargs:
date = kwargs["date"]
if author_id:
vals["author_id"] = author_id
if date:
if isinstance(date, DateTime):
date = datetime.strptime(str(date), "%Y%m%dT%H:%M:%S")
vals["date"] = date
if "message_id" in kwargs:
vals["message_id"] = kwargs["message_id"]
if message_id:
vals["message_id"] = message_id
vals["broker_unread"] = kwargs.get("broker_unread", False)
vals["attachment_ids"] = []
for attachment_id in kwargs.get("attachment_ids", []):
Expand Down Expand Up @@ -136,12 +136,40 @@ def message_post_broker(self, body=False, broker_type=False, **kwargs):
):
return False
vals = self._broker_message_post_vals(
body, broker_unread=True, author_id=self.partner_id.id, **kwargs
body, broker_unread=True, author_id=self.broker_partner_id.id, **kwargs
)
vals["state"] = "received"
vals["broker_type"] = broker_type
return self.env["mail.message.broker"].create(vals)

@api.returns("mail.message", lambda value: value.id)
def message_post(self, *args, **kwargs):
message = super().message_post(*args, **kwargs)
if self.broker_id:
self.env["mail.message.broker"].create(
{
"mail_message_id": message.id,
"channel_id": self.id,
}
).send()
return message

@api.model
def channel_fetch_slot(self):
result = super().channel_fetch_slot()
broker_channels = self.env["mail.channel"].search([("public", "=", "broker")])
result["channel_channel"] += broker_channels.channel_info()
return result

def channel_info(self, *args, **kwargs):
result = super().channel_info(*args, **kwargs)
for channel, channel_info in zip(self, result):
channel_info["broker_id"] = (
channel.broker_id and channel.broker_id.id or False
)
channel_info["broker_unread_counter"] = channel.broker_unread
return result

@api.model_create_multi
def create(self, vals_list):
channels = super().create(vals_list)
Expand Down
2 changes: 1 addition & 1 deletion mail_broker/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MailMessage(models.Model):
_inherit = "mail.message"

broker_channel_id = fields.Many2one(
"mail.broker.channel",
"mail.channel",
readonly=True,
compute="_compute_broker_channel_id",
store=True,
Expand Down
17 changes: 4 additions & 13 deletions mail_broker/models/mail_message_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class MailMessageBroker(models.Model):
auto_join=True,
)
message_id = fields.Char(readonly=True)
channel_id = fields.Many2one(
"mail.broker.channel", required=True, ondelete="cascade"
)
channel_id = fields.Many2one("mail.channel", required=True, ondelete="cascade")
state = fields.Selection(
[
("outgoing", "Outgoing"),
Expand All @@ -54,17 +52,10 @@ def create(self, vals_list):
if self.env.context.get("notify_broker", False):
notifications = []
for message in messages:
notifications.append(
[
(
self._cr.dbname,
"mail.broker",
message.channel_id.broker_id.id,
),
{"message": message.mail_message_id.message_format()[0]},
]
notifications += message.channel_id._channel_message_notifications(
message.mail_message_id
)
self.env["bus.bus"].sendmany(notifications)
self.env["bus.bus"].sudo().sendmany(notifications)
return messages

def send(self, auto_commit=False, raise_exception=False, parse_mode="HTML"):
Expand Down
3 changes: 0 additions & 3 deletions mail_broker/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ access_mail_message_broker_all,mail.message.broker.all,model_mail_message_broker
access_mail_message_broker_portal,mail.message.broker.portal,model_mail_message_broker,base.group_portal,1,1,1,0
access_mail_message_broker_user,mail.message.broker.user,model_mail_message_broker,base.group_user,1,1,1,0
access_mail_message_broker_system,mail.message.broker.system,model_mail_message_broker,base.group_system,1,1,1,1
access_mail_broker_channel_all,mail.telegram.chat.all,model_mail_broker_channel,,1,0,0,0
access_mail_broker_channel_system,mail_broker_channel,model_mail_broker_channel,base.group_system,1,1,1,1
access_mail_broker_all,mail.telegram.bot.all,model_mail_broker,,1,0,0,0
access_mail_broker_channel_user,mail_broker_manager_bot,model_mail_broker_channel,mail_broker.broker_user,1,1,0,0
access_mail_broker_system,mail_broker,model_mail_broker,base.group_system,1,1,1,1
21 changes: 21 additions & 0 deletions mail_broker/security/security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,25 @@
eval="[(4, ref('base.user_root')), (4, ref('base.user_admin'))]"
/>
</record>
<record id="mail_channel_broker_rule" model="ir.rule">
<field name="name">Mail.channel: access broker</field>
<field name="model_id" ref="mail.model_mail_channel" />
<field name="groups" eval="[(4, ref('mail_broker.broker_user'))]" />
<field name="domain_force">[('public', '=', 'broker')]</field>
<field name="perm_create" eval="False" />
<field name="perm_unlink" eval="True" />
</record>
<record id="ir_rule_mail_channel_partner_group_user" model="ir.rule">
<field
name="name"
>mail.channel.partner: write its own entries on broker channels</field>
<field name="model_id" ref="mail.model_mail_channel_partner" />
<field name="groups" eval="[(4, ref('mail_broker.broker_user'))]" />
<field name="domain_force">[('channel_id.public', '=', 'broker')]</field>
<field name="perm_read" eval="False" />
<field name="perm_write" eval="True" />
<field name="perm_create" eval="False" />
<field name="perm_unlink" eval="True" />
</record>

</odoo>
14 changes: 11 additions & 3 deletions mail_broker/services/mail_broker_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 ACSONE SA/NV
# Copyright 2022 CreuBlanca
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo.addons.base_rest import restapi
from odoo.addons.component.core import AbstractComponent
Expand All @@ -17,6 +17,12 @@ def to_openapi_requestbody(self, service):
def to_openapi_responses(self, service):
return {"200": {"content": {}}}

def to_openapi_query_parameters(self, service, spec):
return []

def to_json_schema(self, service, spec, direction):
return {}


class MailBrokerService(AbstractComponent):
_inherit = "base.rest.service"
Expand Down Expand Up @@ -60,10 +66,10 @@ def _remove_webhook(self):
def _get_channel(self, broker, token, update, force_create=False):
chat_id = broker._get_channel_id(token)
if chat_id:
return self.env["mail.broker.channel"].browse(chat_id)
return broker.env["mail.channel"].browse(chat_id)
if not force_create and broker.has_new_channel_security:
return False
return self.env["mail.broker.channel"].create(
return broker.env["mail.channel"].create(
self._get_channel_vals(broker, token, update)
)

Expand All @@ -72,6 +78,8 @@ def _get_channel_vals(self, broker, token, update):
"token": token,
"broker_id": broker.id,
"show_on_app": broker.show_on_app,
"public": "broker",
"channel_type": "broker",
}

def _send(self, record, auto_commit=False, raise_exception=False, parse_mode=False):
Expand Down
Loading

0 comments on commit fc80217

Please sign in to comment.