diff libervia/backend/plugins/plugin_xep_0313.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 1c30d574df2b
children
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0313.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/plugins/plugin_xep_0313.py	Wed Jun 19 18:44:57 2024 +0200
@@ -73,28 +73,36 @@
         self._last_stanza_id_d = defer.Deferred()
         self._last_stanza_id_d.callback(None)
         host.bridge.add_method(
-            "mam_get", ".plugin", in_sign='sss',
-            out_sign='(a(sdssa{ss}a{ss}ss)ss)', method=self._get_archives,
-            async_=True)
+            "mam_get",
+            ".plugin",
+            in_sign="sss",
+            out_sign="(a(sdssa{ss}a{ss}ss)ss)",
+            method=self._get_archives,
+            async_=True,
+        )
 
     async def resume(self, client):
         """Retrieve one2one messages received since the last we have in local storage"""
         stanza_id_data = await self.host.memory.storage.get_privates(
-            mam.NS_MAM, [KEY_LAST_STANZA_ID], profile=client.profile)
+            mam.NS_MAM, [KEY_LAST_STANZA_ID], profile=client.profile
+        )
         stanza_id = stanza_id_data.get(KEY_LAST_STANZA_ID)
         rsm_req = None
         if stanza_id is None:
             log.info("can't retrieve last stanza ID, checking history")
             last_mess = await self.host.memory.history_get(
-                None, None, limit=1, filters={'not_types': C.MESS_TYPE_GROUPCHAT,
-                                              'last_stanza_id': True},
-                profile=client.profile)
+                None,
+                None,
+                limit=1,
+                filters={"not_types": C.MESS_TYPE_GROUPCHAT, "last_stanza_id": True},
+                profile=client.profile,
+            )
             if not last_mess:
                 log.info(_("It seems that we have no MAM history yet"))
                 stanza_id = None
                 rsm_req = rsm.RSMRequest(max_=50, before="")
             else:
-                stanza_id = last_mess[0][-1]['stanza_id']
+                stanza_id = last_mess[0][-1]["stanza_id"]
         if rsm_req is None:
             rsm_req = rsm.RSMRequest(max_=100, after=stanza_id)
         mam_req = mam.MAMRequest(rsm_=rsm_req)
@@ -102,8 +110,9 @@
         count = 0
         while not complete:
             try:
-                mam_data = await self.get_archives(client, mam_req,
-                                                  service=client.jid.userhostJID())
+                mam_data = await self.get_archives(
+                    client, mam_req, service=client.jid.userhostJID()
+                )
             except StanzaError as e:
                 log.warning(
                     f"Can't retrieve MAM archives: {e}\n"
@@ -112,9 +121,7 @@
                 )
                 return
             except Exception as e:
-                log.exception(
-                    f"Can't retrieve retrieve MAM archive"
-                )
+                log.exception(f"Can't retrieve retrieve MAM archive")
                 return
             elt_list, rsm_response, mam_response = mam_data
             complete = mam_response["complete"]
@@ -130,12 +137,13 @@
             for idx, mess_elt in enumerate(elt_list):
                 try:
                     fwd_message_elt = self.get_message_from_result(
-                        client, mess_elt, mam_req)
+                        client, mess_elt, mam_req
+                    )
                 except exceptions.DataError:
                     continue
 
                 try:
-                    destinee = jid.JID(fwd_message_elt['to'])
+                    destinee = jid.JID(fwd_message_elt["to"])
                 except KeyError:
                     log.warning(_('missing "to" attribute in forwarded message'))
                     destinee = client.jid
@@ -146,15 +154,17 @@
                 else:
                     # this message should be from us, we just add it to history
                     try:
-                        from_jid = jid.JID(fwd_message_elt['from'])
+                        from_jid = jid.JID(fwd_message_elt["from"])
                     except KeyError:
                         log.warning(_('missing "from" attribute in forwarded message'))
                         from_jid = client.jid
                     if from_jid.userhostJID() != client.jid.userhostJID():
-                        log.warning(_(
-                            'was expecting a message sent by our jid, but this one if '
-                            'from {from_jid}, ignoring\n{xml}').format(
-                                from_jid=from_jid.full(), xml=mess_elt.toXml()))
+                        log.warning(
+                            _(
+                                "was expecting a message sent by our jid, but this one if "
+                                "from {from_jid}, ignoring\n{xml}"
+                            ).format(from_jid=from_jid.full(), xml=mess_elt.toXml())
+                        )
                         continue
                     # adding message to history
                     mess_data = client.messageProt.parse_message(fwd_message_elt)
@@ -162,11 +172,14 @@
                         await client.messageProt.add_to_history(mess_data)
                     except exceptions.CancelError as e:
                         log.warning(
-                            "message has not been added to history: {e}".format(e=e))
+                            "message has not been added to history: {e}".format(e=e)
+                        )
                     except Exception as e:
                         log.error(
-                            "can't add message to history: {e}\n{xml}"
-                            .format(e=e, xml=mess_elt.toXml()))
+                            "can't add message to history: {e}\n{xml}".format(
+                                e=e, xml=mess_elt.toXml()
+                            )
+                        )
                     if complete and idx == len(elt_list) - 1:
                         # We are at the last message from archive, we store the ID to not
                         # ask again the same messages next time.
@@ -176,7 +189,7 @@
                                 namespace=mam.NS_MAM,
                                 key=KEY_LAST_STANZA_ID,
                                 value=stanza_id,
-                                profile=client.profile
+                                profile=client.profile,
                             )
                         except Exception:
                             log.exception("Can't store last stanza ID")
@@ -184,8 +197,11 @@
         if not count:
             log.info(_("We have received no message while offline"))
         else:
-            log.info(_("We have received {num_mess} message(s) while offline.")
-                .format(num_mess=count))
+            log.info(
+                _("We have received {num_mess} message(s) while offline.").format(
+                    num_mess=count
+                )
+            )
 
     def profile_connected(self, client):
         defer.ensureDeferred(self.resume(client))
@@ -209,22 +225,24 @@
                 value = extra.pop(MAM_PREFIX + arg)
                 form_args[arg] = datetime.fromtimestamp(float(value), tz.tzutc())
             except (TypeError, ValueError):
-                log.warning("Bad value for {arg} filter ({value}), ignoring".format(
-                    arg=arg, value=value))
+                log.warning(
+                    "Bad value for {arg} filter ({value}), ignoring".format(
+                        arg=arg, value=value
+                    )
+                )
             except KeyError:
                 continue
 
         try:
-            form_args["with_jid"] = jid.JID(extra.pop(
-                MAM_PREFIX + "with"))
-        except (jid.InvalidFormat):
+            form_args["with_jid"] = jid.JID(extra.pop(MAM_PREFIX + "with"))
+        except jid.InvalidFormat:
             log.warning("Bad value for jid filter")
         except KeyError:
             pass
 
         for name, value in extra.items():
             if name.startswith(FILTER_PREFIX):
-                var = name[len(FILTER_PREFIX):]
+                var = name[len(FILTER_PREFIX) :]
                 extra_fields = form_args.setdefault("extra_fields", [])
                 extra_fields.append(data_form.Field(var=var, value=value))
 
@@ -264,18 +282,22 @@
         @return domish.Element): <message/> that can be used directly with onMessage
         """
         if mess_elt.name != "message":
-            log.warning("unexpected stanza in archive: {xml}".format(
-                xml=mess_elt.toXml()))
+            log.warning(
+                "unexpected stanza in archive: {xml}".format(xml=mess_elt.toXml())
+            )
             raise exceptions.DataError("Invalid element")
         service_jid = client.jid.userhostJID() if service is None else service
         mess_from = mess_elt.getAttribute("from") or client.jid.userhost()
         # we check that the message has been sent by the right service
         # if service is None (i.e. message expected from our own server)
         # from can be server jid or user's bare jid
-        if (mess_from != service_jid.full()
-            and not (service is None and mess_from == client.jid.host)):
-            log.error("Message is not from our server, something went wrong: "
-                      "{xml}".format(xml=mess_elt.toXml()))
+        if mess_from != service_jid.full() and not (
+            service is None and mess_from == client.jid.host
+        ):
+            log.error(
+                "Message is not from our server, something went wrong: "
+                "{xml}".format(xml=mess_elt.toXml())
+            )
             raise exceptions.DataError("Invalid element")
         try:
             result_elt = next(mess_elt.elements(mam.NS_MAM, "result"))
@@ -287,16 +309,19 @@
                 delay_elt = None
             fwd_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, "message"))
         except StopIteration:
-            log.warning("Invalid message received from MAM: {xml}".format(
-                xml=mess_elt.toXml()))
+            log.warning(
+                "Invalid message received from MAM: {xml}".format(xml=mess_elt.toXml())
+            )
             raise exceptions.DataError("Invalid element")
         else:
             if not result_elt["queryid"] == mam_req.query_id:
-                log.error("Unexpected query id (was expecting {query_id}): {xml}"
-                    .format(query_id=mam.query_id, xml=mess_elt.toXml()))
+                log.error(
+                    "Unexpected query id (was expecting {query_id}): {xml}".format(
+                        query_id=mam.query_id, xml=mess_elt.toXml()
+                    )
+                )
                 raise exceptions.DataError("Invalid element")
-            stanza_id = self._sid.get_stanza_id(fwd_message_elt,
-                                              service_jid)
+            stanza_id = self._sid.get_stanza_id(fwd_message_elt, service_jid)
             if stanza_id is None:
                 # not stanza-id element is present, we add one so message
                 # will be archived with it, and we won't request several times
@@ -304,10 +329,15 @@
                 try:
                     stanza_id = result_elt["id"]
                 except AttributeError:
-                    log.warning('Invalid MAM result: missing "id" attribute: {xml}'
-                                .format(xml=result_elt.toXml()))
+                    log.warning(
+                        'Invalid MAM result: missing "id" attribute: {xml}'.format(
+                            xml=result_elt.toXml()
+                        )
+                    )
                     raise exceptions.DataError("Invalid element")
-                self._sid.add_stanza_id(client, fwd_message_elt, stanza_id, by=service_jid)
+                self._sid.add_stanza_id(
+                    client, fwd_message_elt, stanza_id, by=service_jid
+                )
 
             if delay_elt is not None:
                 fwd_message_elt.addChild(delay_elt)
@@ -345,8 +375,10 @@
         except StopIteration:
             raise exceptions.DataError("Invalid MAM result")
 
-        mam_response = {"complete": C.bool(fin_elt.getAttribute("complete", C.BOOL_FALSE)),
-                        "stable": C.bool(fin_elt.getAttribute("stable", C.BOOL_TRUE))}
+        mam_response = {
+            "complete": C.bool(fin_elt.getAttribute("complete", C.BOOL_FALSE)),
+            "stable": C.bool(fin_elt.getAttribute("stable", C.BOOL_TRUE)),
+        }
 
         try:
             rsm_response = rsm.RSMResponse.fromElement(fin_elt)
@@ -359,14 +391,12 @@
         elt_list, rsm_response, mam_response = data
         mess_list = []
         for elt in elt_list:
-            fwd_message_elt = self.get_message_from_result(client, elt, mam_req,
-                                                        service=service)
+            fwd_message_elt = self.get_message_from_result(
+                client, elt, mam_req, service=service
+            )
             mess_data = client.messageProt.parse_message(fwd_message_elt)
             mess_list.append(client.message_get_bridge_args(mess_data))
-        metadata = {
-            'rsm': self._rsm.response2dict(rsm_response),
-            'mam': mam_response
-        }
+        metadata = {"rsm": self._rsm.response2dict(rsm_response), "mam": mam_response}
         return mess_list, data_format.serialise(metadata), client.profile
 
     def _get_archives(self, service, extra_ser, profile_key):
@@ -420,8 +450,14 @@
         # http://xmpp.org/extensions/xep-0313.html#prefs
         return client._mam.queryPrefs(service)
 
-    def _set_prefs(self, service_s=None, default="roster", always=None, never=None,
-                  profile_key=C.PROF_KEY_NONE):
+    def _set_prefs(
+        self,
+        service_s=None,
+        default="roster",
+        always=None,
+        never=None,
+        profile_key=C.PROF_KEY_NONE,
+    ):
         service = jid.JID(service_s) if service_s else None
         always_jid = [jid.JID(entity) for entity in always]
         never_jid = [jid.JID(entity) for entity in never]
@@ -463,7 +499,9 @@
                     namespace=mam.NS_MAM,
                     key=KEY_LAST_STANZA_ID,
                     value=stanza_id,
-                    profile=client.profile))
+                    profile=client.profile,
+                )
+            )
 
 
 @implementer(disco.IDisco)
@@ -478,7 +516,8 @@
 
     def connectionInitialized(self):
         observer_xpath = MESSAGE_STANZA_ID.format(
-            ns_stanza_id=self.host.ns_map['stanza_id'])
+            ns_stanza_id=self.host.ns_map["stanza_id"]
+        )
         self.xmlstream.addObserver(
             observer_xpath, self.plugin_parent.on_message_stanza_id, client=self.parent
         )