diff libervia/backend/plugins/plugin_xep_0359.py @ 4197:9cda0347e0ac

plugin XEP-0359: if no origin ID is found, use <message> ID instead: This way, we always have the right ID to check for non groupchat message in `History.origin_id`.
author Goffi <goffi@goffi.org>
date Wed, 13 Dec 2023 22:00:25 +0100
parents 6784d07b99c8
children 0d7bb4df2343
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0359.py	Wed Dec 13 22:00:25 2023 +0100
+++ b/libervia/backend/plugins/plugin_xep_0359.py	Wed Dec 13 22:00:25 2023 +0100
@@ -1,8 +1,7 @@
 #!/usr/bin/env python3
 
-
-# SAT plugin for Message Archive Management (XEP-0359)
-# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
+# Libervia plugin for Message Archive Management (XEP-0359)
+# Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
 
 # This program is free software: you can redistribute it and/or modify
@@ -20,14 +19,18 @@
 
 from typing import Optional
 import uuid
-from zope.interface import implementer
+
 from twisted.words.protocols.jabber import xmlstream
+from twisted.words.xish import domish
 from wokkel import disco
+from zope.interface import implementer
+
+from libervia.backend.core import exceptions
 from libervia.backend.core.constants import Const as C
-from libervia.backend.core import exceptions
+from libervia.backend.core.core_types import SatXMPPEntity
 from libervia.backend.core.i18n import _
 from libervia.backend.core.log import getLogger
-from twisted.words.xish import domish
+from libervia.backend.models.core import MessageData
 
 log = getLogger(__name__)
 
@@ -55,13 +58,21 @@
         host.trigger.add("message_parse", self._message_parse_trigger)
         host.trigger.add("send_message_data", self._send_message_data_trigger)
 
-    def _message_parse_trigger(self, client, message_elt, mess_data):
+    def _message_parse_trigger(
+        self,
+        client: SatXMPPEntity,
+        message_elt: domish.Element,
+        mess_data: MessageData
+    ) -> bool:
         """Check if message has a stanza-id"""
         stanza_id = self.get_stanza_id(message_elt, client.jid.userhostJID())
         if stanza_id is not None:
             mess_data['extra']['stanza_id'] = stanza_id
-        origin_id = self.get_origin_id(message_elt)
-        if origin_id is not None:
+        try:
+            origin_id = self.get_origin_id(message_elt) or message_elt["id"]
+        except KeyError:
+            pass
+        else:
             mess_data['extra']['origin_id'] = origin_id
         return True