# HG changeset patch
# User szeka_wong <szeka.wong@xcg-consulting.fr>
# Date 1734086863 -3600
#      Fri Dec 13 11:47:43 2024 +0100
# Branch 18.0
# Node ID 435ed0f781ca9ea96dac034f379381cf212f3e5f
# Parent  fe884c99d3d575af1942a7910336d6462f1727f0
Provides a way for other modules to define a message type.
Fix message body encoding and display.

diff --git a/NEWS.rst b/NEWS.rst
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -1,6 +1,12 @@
 Changelog
 =========
 
+18.0.1.1.0
+----------
+
+* Provides a way for other modules to define a message type.
+* Fix message body encoding and display.
+
 18.0.1.0.0
 ----------
 
diff --git a/__manifest__.py b/__manifest__.py
--- a/__manifest__.py
+++ b/__manifest__.py
@@ -22,7 +22,7 @@
     "license": "AGPL-3",
     "summary": "Xbus common elements",
     "icon": "/xbus_common/static/description/icon.svg",
-    "version": "18.0.1.0.0",
+    "version": "18.0.1.1.0",
     "category": "Technical",
     "author": "XCG Consulting",
     "website": "https://orbeet.io/",
diff --git a/models/xbus_message.py b/models/xbus_message.py
--- a/models/xbus_message.py
+++ b/models/xbus_message.py
@@ -17,6 +17,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
+import base64
 import json
 import pprint
 
@@ -45,7 +46,7 @@
     # is only used to display information
     uid = fields.Char(string="UID", readonly=True)
 
-    type = fields.Selection([], required=True)
+    type = fields.Selection(selection="_get_type_selection", required=True)
 
     header = fields.Binary(attachment=False)
 
@@ -58,19 +59,23 @@
     @api.depends("header")
     def _compute_header_json(self):
         for message in self:
-            message.header_json = message._convert_binary_to_json(message.header)
+            message.header_json = message._convert_binary_to_json(
+                message.with_context(bin_size=False).header
+            )
 
     @api.depends("body")
     def _compute_body_json(self):
         for message in self:
-            message.body_json = message._convert_binary_to_json(message.body)
+            message.body_json = message._convert_binary_to_json(
+                message.with_context(bin_size=False).body
+            )
 
     def _convert_binary_to_json(self, binary_value):
         if binary_value:
             if isinstance(
                 binary_value, bytes
             ):  # Check if it is of type bytes before decoding
-                string_value = binary_value.decode()  # utf-8 by default
+                string_value = base64.b64decode(binary_value).decode()  # utf-8 by default
             else:
                 string_value = binary_value
             if string_value.startswith("{") or string_value.startswith("["):
@@ -81,6 +86,12 @@
                     return False
         return False
 
+    def _get_type_selection(self):
+        """ Provides a list of message type selection values.
+        Should return a list of value-label tuples.
+        """
+        raise NotImplementedError
+
     link_ids = fields.One2many(
         comodel_name="xbus.message.link",
         string="Linked Records",
diff --git a/tests/models.py b/tests/models.py
--- a/tests/models.py
+++ b/tests/models.py
@@ -26,10 +26,8 @@
 
     _inherit = "xbus.message"
 
-    type = fields.Selection(
-        selection_add=[
+    def _get_type_selection(self):
+        return [
             ("test_1", "Test Message Type 1"),
             ("test_2", "Test Message Type 2"),
-        ],
-        ondelete={"test_1": "cascade", "test_2": "cascade"},
-    )
+        ]
diff --git a/tests/test_xbus.py b/tests/test_xbus.py
--- a/tests/test_xbus.py
+++ b/tests/test_xbus.py
@@ -71,6 +71,8 @@
         )
         self.assertNotEqual(messages[0].uid, "")
         self.assertNotEqual(messages[1].uid, "")
+        self.assertEqual(base64.decodebytes(messages[0].body), b"Items")
+        self.assertEqual(base64.decodebytes(messages[1].body), b"Body")
         self.assertEqual(
             messages[0].link_ids[0].action_open_document()["res_id"], self.env.user.id
         )
# HG changeset patch
# User szeka_wong <szeka.wong@xcg-consulting.fr>
# Date 1734087237 -3600
#      Fri Dec 13 11:53:57 2024 +0100
# Branch 18.0
# Node ID 94357b1df882aa7c974812af2c934f89d480bbd6
# Parent  435ed0f781ca9ea96dac034f379381cf212f3e5f
Ruff

diff --git a/models/xbus_envelope.py b/models/xbus_envelope.py
--- a/models/xbus_envelope.py
+++ b/models/xbus_envelope.py
@@ -19,7 +19,7 @@
 ##############################################################################
 import uuid
 
-from odoo import api, fields, models, tools
+from odoo import api, fields, models, tools  # type: ignore[import-untyped]
 
 
 class XbusEnvelope(models.Model):
diff --git a/models/xbus_message.py b/models/xbus_message.py
--- a/models/xbus_message.py
+++ b/models/xbus_message.py
@@ -21,7 +21,7 @@
 import json
 import pprint
 
-from odoo import api, fields, models, tools
+from odoo import api, fields, models, tools  # type: ignore[import-untyped]
 
 
 class XbusMessage(models.Model):
@@ -75,7 +75,9 @@
             if isinstance(
                 binary_value, bytes
             ):  # Check if it is of type bytes before decoding
-                string_value = base64.b64decode(binary_value).decode()  # utf-8 by default
+                string_value = base64.b64decode(
+                    binary_value
+                ).decode()  # utf-8 by default
             else:
                 string_value = binary_value
             if string_value.startswith("{") or string_value.startswith("["):
@@ -87,7 +89,7 @@
         return False
 
     def _get_type_selection(self):
-        """ Provides a list of message type selection values.
+        """Provides a list of message type selection values.
         Should return a list of value-label tuples.
         """
         raise NotImplementedError
diff --git a/models/xbus_message_link.py b/models/xbus_message_link.py
--- a/models/xbus_message_link.py
+++ b/models/xbus_message_link.py
@@ -17,7 +17,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-from odoo import _, exceptions, fields, models
+from odoo import _, exceptions, fields, models  # type: ignore[import-untyped]
 
 
 class XbusMessageLink(models.Model):
diff --git a/tests/modelcase.py b/tests/modelcase.py
--- a/tests/modelcase.py
+++ b/tests/modelcase.py
@@ -17,9 +17,9 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-from odoo import models
-from odoo.tests import TransactionCase
-from odoo_test_helper import FakeModelLoader
+from odoo import models  # type: ignore[import-untyped]
+from odoo.tests import TransactionCase  # type: ignore[import-untyped]
+from odoo_test_helper import FakeModelLoader  # type: ignore[import-untyped]
 
 
 class ModelCase(TransactionCase):
diff --git a/tests/models.py b/tests/models.py
--- a/tests/models.py
+++ b/tests/models.py
@@ -17,7 +17,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 #############################################################################
-from odoo import fields, models
+from odoo import models  # type: ignore[import-untyped]
 
 
 class XbusMessage(models.Model):
diff --git a/tests/test_xbus.py b/tests/test_xbus.py
--- a/tests/test_xbus.py
+++ b/tests/test_xbus.py
@@ -20,7 +20,7 @@
 import base64
 import uuid
 
-from odoo import Command, exceptions
+from odoo import Command, exceptions  # type: ignore[import-untyped]
 
 from .modelcase import ModelCase