155 lines
4.3 KiB
Python
155 lines
4.3 KiB
Python
from odoo import models
|
|
import logging
|
|
import re
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class DiscussChannelFirebaseNotify(models.Model):
|
|
_inherit = "discuss.channel"
|
|
|
|
def _notify_thread(self, message, msg_vals=False, **kwargs):
|
|
"""
|
|
Extend Odoo Discuss notifications with Firebase push notifications.
|
|
"""
|
|
res = super()._notify_thread(
|
|
message,
|
|
msg_vals=msg_vals,
|
|
**kwargs
|
|
)
|
|
|
|
try:
|
|
recipients = self._notify_get_recipients(
|
|
message,
|
|
msg_vals or {},
|
|
**kwargs
|
|
)
|
|
|
|
_logger.info(
|
|
"Firebase notification processing. Channel=%s Message=%s Recipients=%s",
|
|
self.display_name,
|
|
message.id,
|
|
len(recipients),
|
|
)
|
|
|
|
notifications = []
|
|
|
|
for recipient in recipients:
|
|
user = False
|
|
|
|
# Direct user recipient
|
|
if recipient.get("uid"):
|
|
user = (
|
|
self.env["res.users"]
|
|
.sudo()
|
|
.browse(recipient["uid"])
|
|
)
|
|
|
|
# Channel member recipient (uid=False)
|
|
elif recipient.get("id"):
|
|
partner = (
|
|
self.env["res.partner"]
|
|
.sudo()
|
|
.browse(recipient["id"])
|
|
)
|
|
|
|
user = partner.user_ids[:1]
|
|
|
|
if not user:
|
|
continue
|
|
|
|
token = user.token
|
|
|
|
if not token:
|
|
_logger.info(
|
|
"Skipping user %s: no token",
|
|
user.name,
|
|
)
|
|
continue
|
|
|
|
# Record name
|
|
record_name = (
|
|
msg_vals.get("record_name")
|
|
if msg_vals and isinstance(msg_vals, dict)
|
|
else message.record_name
|
|
)
|
|
|
|
# Author
|
|
author = message.author_id
|
|
|
|
if self.channel_type == "chat":
|
|
title = author.name or "New Message"
|
|
|
|
elif self.channel_type == "channel":
|
|
title = (
|
|
f"#{record_name} - {author.name}"
|
|
if record_name
|
|
else author.name
|
|
)
|
|
|
|
elif self.channel_type == "group":
|
|
title = (
|
|
f"{record_name} - {author.name}"
|
|
if record_name
|
|
else author.name
|
|
)
|
|
|
|
else:
|
|
title = record_name or author.name or "New Message"
|
|
|
|
# Remove html tags
|
|
body_html = (
|
|
msg_vals.get("body")
|
|
if msg_vals and msg_vals.get("body")
|
|
else message.body
|
|
)
|
|
|
|
body = re.sub(
|
|
r"<[^>]+>",
|
|
"",
|
|
body_html or "",
|
|
).strip()
|
|
|
|
payload = {
|
|
"action": "mail.action_discuss",
|
|
"channel_id": str(self.id),
|
|
"message_id": str(message.id),
|
|
}
|
|
|
|
if self.channel_type:
|
|
payload["channel_type"] = self.channel_type
|
|
|
|
notifications.append({
|
|
"token": token,
|
|
"title": title,
|
|
"body": body,
|
|
"data": payload,
|
|
})
|
|
|
|
_logger.info(
|
|
"Prepared Firebase notification for user=%s",
|
|
user.name,
|
|
)
|
|
|
|
if notifications:
|
|
_logger.info(
|
|
"Sending %s Firebase notifications",
|
|
len(notifications),
|
|
)
|
|
|
|
self.env["firebase.service"].send_notification(
|
|
notifications
|
|
)
|
|
|
|
else:
|
|
_logger.info(
|
|
"No Firebase notifications to send"
|
|
)
|
|
|
|
except Exception as exc:
|
|
_logger.exception(
|
|
"Firebase notification error: %s",
|
|
exc
|
|
)
|
|
|
|
return res |