|
| 1 | +import re |
| 2 | + |
| 3 | +from odoo import http |
| 4 | +from odoo.http import request |
| 5 | + |
| 6 | +# Valida que la URL sea http/https con host válido, sin path (solo origen) |
| 7 | +_VALID_ORIGIN_RE = re.compile(r"^https?://[a-zA-Z0-9]([a-zA-Z0-9\-\.]*[a-zA-Z0-9])?(:\d+)?$") |
| 8 | + |
| 9 | + |
| 10 | +class ExternalLivechatController(http.Controller): |
| 11 | + @http.route( |
| 12 | + "/website_livechat_external/config", |
| 13 | + type="json", |
| 14 | + auth="user", |
| 15 | + ) |
| 16 | + def livechat_config(self): |
| 17 | + """Devuelve la configuración del livechat externo para el backend.""" |
| 18 | + ICP = request.env["ir.config_parameter"].sudo() |
| 19 | + enabled = ICP.get_param("website_livechat_external.enabled", "False") |
| 20 | + return { |
| 21 | + "enabled": enabled in ("True", "1", "true"), |
| 22 | + } |
| 23 | + |
| 24 | + @http.route( |
| 25 | + "/website_livechat_external/frame", |
| 26 | + type="http", |
| 27 | + auth="public", |
| 28 | + website=True, |
| 29 | + sitemap=False, |
| 30 | + ) |
| 31 | + def livechat_frame(self): |
| 32 | + """ |
| 33 | + Devuelve una página HTML mínima y aislada que carga los scripts del |
| 34 | + livechat del proveedor externo (Odoo 19). Al servirse desde el mismo |
| 35 | + origen de Odoo 18, el iframe que la contiene puede manipular su DOM |
| 36 | + para habilitar pointer-events sólo sobre el widget del chat. |
| 37 | + """ |
| 38 | + ICP = request.env["ir.config_parameter"].sudo() |
| 39 | + |
| 40 | + enabled = ICP.get_param("website_livechat_external.enabled", "False") |
| 41 | + if enabled not in ("True", "1", "true"): |
| 42 | + return request.not_found() |
| 43 | + |
| 44 | + provider_url = ICP.get_param("website_livechat_external.provider_url", "").rstrip("/") |
| 45 | + channel_id_raw = ICP.get_param("website_livechat_external.channel_id", "0") |
| 46 | + |
| 47 | + # Validaciones de seguridad básicas |
| 48 | + if not provider_url or not _VALID_ORIGIN_RE.match(provider_url): |
| 49 | + return request.make_response("URL del proveedor inválida.", status=400) |
| 50 | + |
| 51 | + try: |
| 52 | + channel_id = int(channel_id_raw) |
| 53 | + if channel_id <= 0: |
| 54 | + raise ValueError |
| 55 | + except (ValueError, TypeError): |
| 56 | + return request.make_response("ID de canal inválido.", status=400) |
| 57 | + |
| 58 | + loader_src = f"{provider_url}/im_livechat/loader/{channel_id}" |
| 59 | + embed_src = f"{provider_url}/im_livechat/assets_embed.js" |
| 60 | + |
| 61 | + html = f"""<!DOCTYPE html> |
| 62 | +<html> |
| 63 | +<head> |
| 64 | + <meta charset="utf-8"/> |
| 65 | + <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| 66 | + <style> |
| 67 | + html, body {{ |
| 68 | + margin: 0; |
| 69 | + padding: 0; |
| 70 | + background: transparent; |
| 71 | + overflow: hidden; |
| 72 | + }} |
| 73 | + </style> |
| 74 | +</head> |
| 75 | +<body> |
| 76 | + <!-- |
| 77 | + Esta página corre en su propio contexto JS (sin los globals de Odoo 18), |
| 78 | + por lo que los scripts de Odoo 19 no colisionan con los de Odoo 18. |
| 79 | + --> |
| 80 | + <script defer="defer" type="text/javascript" src="{loader_src}"></script> |
| 81 | + <script defer="defer" type="text/javascript" src="{embed_src}"></script> |
| 82 | + <script type="text/javascript"> |
| 83 | + // Escucha mensajes del parent para abrir/cerrar el livechat. |
| 84 | + // El botón vive dentro de un shadow DOM (.o-livechat-root) y no es |
| 85 | + // accesible con querySelector desde el parent frame. |
| 86 | + window.addEventListener("message", function (ev) {{ |
| 87 | + if (!ev.data || ev.data.type !== "toggle_livechat") return; |
| 88 | + var root = document.querySelector(".o-livechat-root"); |
| 89 | + if (!root || !root.shadowRoot) return; |
| 90 | + var btn = root.shadowRoot.querySelector(".o-livechat-LivechatButton"); |
| 91 | + if (btn) btn.click(); |
| 92 | + }}); |
| 93 | + </script> |
| 94 | +</body> |
| 95 | +</html>""" |
| 96 | + |
| 97 | + return request.make_response( |
| 98 | + html, |
| 99 | + headers=[ |
| 100 | + ("Content-Type", "text/html; charset=utf-8"), |
| 101 | + # Solo permite embeber este frame desde el mismo origen |
| 102 | + ("X-Frame-Options", "SAMEORIGIN"), |
| 103 | + ], |
| 104 | + ) |
| 105 | + |
| 106 | + @http.route( |
| 107 | + "/website_livechat_external/backend_frame", |
| 108 | + type="http", |
| 109 | + auth="user", |
| 110 | + sitemap=False, |
| 111 | + ) |
| 112 | + def livechat_backend_frame(self): |
| 113 | + """ |
| 114 | + Igual que livechat_frame pero sin website=True, para uso en el |
| 115 | + backend web client. |
| 116 | + """ |
| 117 | + return self.livechat_frame() |
0 commit comments