discuss_channel.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from odoo import models, api, fields
  2. from odoo.addons.mail.tools.discuss import Store
  3. from odoo.exceptions import ValidationError
  4. class DiscussChannel(models.Model):
  5. _inherit = "discuss.channel"
  6. is_whatsapp_web = fields.Boolean(compute="_compute_is_whatsapp_web")
  7. @api.depends("channel_type", "wa_account_id.whatsapp_web_url")
  8. def _compute_is_whatsapp_web(self):
  9. for record in self:
  10. record.is_whatsapp_web = record.channel_type == "whatsapp" and bool(
  11. record.wa_account_id.whatsapp_web_url
  12. )
  13. def _to_store(self, store: Store):
  14. """
  15. Send is_whatsapp_web to the frontend via Store.
  16. """
  17. super()._to_store(store)
  18. for channel in self:
  19. if channel.is_whatsapp_web:
  20. store.add(channel, {"is_whatsapp_web": True})
  21. def message_post(self, **kwargs):
  22. """
  23. Override message_post to allow sending free text messages in WhatsApp Web channels.
  24. Standard Odoo WhatsApp module might block or restrict messages without templates.
  25. """
  26. # Check if it's a WhatsApp channel with WhatsApp Web configured
  27. if self.channel_type == "whatsapp" and self.wa_account_id.whatsapp_web_url:
  28. # We want to use our custom logic for these channels
  29. # Extract basic message data
  30. body = kwargs.get("body", "")
  31. attachment_ids = kwargs.get("attachment_ids", [])
  32. # If it's a simple text message or has attachments, we handle it.
  33. # Note: We need to ensure we don't break other message_post usages (like system notifications)
  34. # System notifications usually have subtype_xmlid='mail.mt_note' or similar, strict check might be needed.
  35. # Let's check if we should intervene.
  36. # If the user is trying to send a message (comment)
  37. if kwargs.get("message_type") == "comment" or not kwargs.get(
  38. "message_type"
  39. ):
  40. # Check for attachments in kwargs (can be list of IDs or list of tuples)
  41. # We mainly care about passing them to the mail.message
  42. # 1. Create the mail.message manually to bypass potential blocks in super().message_post()
  43. # We need to replicate some logic from mail.thread.message_post
  44. # However, completely skipping super() is risky for notifications/followers.
  45. # Let's try a hybrid approach:
  46. # Create the message using mail.message.create() directly, then run necessary side effects?
  47. # Or invoke mail.thread's message_post directly if possible?
  48. # We can't easily invoke 'grandparent' methods in Odoo new API unless we are careful.
  49. # Simplified approach: mimic whatsapp_composer logic
  50. email_from = kwargs.get("email_from")
  51. if not email_from:
  52. email_from = self.env.user.email_formatted
  53. # Create mail.message
  54. msg_values = {
  55. "body": body,
  56. "model": self._name,
  57. "res_id": self.id,
  58. "message_type": "whatsapp_message", # Use whatsapp_message type so our other logic picks it up? Or 'comment'?
  59. # Standard WA uses 'whatsapp_message'
  60. "email_from": email_from,
  61. "partner_ids": [
  62. (4, p.id) for p in self.channel_partner_ids
  63. ], # Add channel partners?
  64. # 'subtype_id': ...
  65. "attachment_ids": attachment_ids,
  66. }
  67. # Handle author
  68. author_id = kwargs.get("author_id")
  69. if author_id:
  70. msg_values["author_id"] = author_id
  71. else:
  72. msg_values["author_id"] = self.env.user.partner_id.id
  73. # Create the message
  74. message = self.env["mail.message"].create(msg_values)
  75. # Now create the whatsapp.message to trigger sending (via our overridden _send_message or similar)
  76. # Note: whatsapp_message.create() triggers _send_message() if state is outgoing?
  77. # In our whatsapp_composer, we called _send_message() explicitly.
  78. # Determine recipient (Phone or Group)
  79. mobile_number = self.whatsapp_number
  80. recipient_type = "phone"
  81. if mobile_number and mobile_number.endswith("@g.us"):
  82. recipient_type = "group"
  83. wa_msg_values = {
  84. "mail_message_id": message.id,
  85. "wa_account_id": self.wa_account_id.id,
  86. "mobile_number": mobile_number,
  87. "recipient_type": recipient_type,
  88. "wa_template_id": False,
  89. "body": body,
  90. "state": "outgoing",
  91. }
  92. wa_msg = self.env["whatsapp.message"].create(wa_msg_values)
  93. # Send it
  94. wa_msg._send_message()
  95. # Ensure the message is linked to the channel (standard mail.message behavior should handle res_id/model)
  96. # But Discuss expects the message to be in the channel.
  97. return message
  98. # Default behavior for other channels or if conditions not met
  99. return super(DiscussChannel, self).message_post(**kwargs)