diff sat/plugins/plugin_xep_0277.py @ 3715:b9718216a1c0 0.9

merge bookmark 0.9
author Goffi <goffi@goffi.org>
date Wed, 01 Dec 2021 16:13:31 +0100
parents 68f2a9c171d1
children a0c08fcfe11e
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0277.py	Tue Nov 30 23:31:09 2021 +0100
+++ b/sat/plugins/plugin_xep_0277.py	Wed Dec 01 16:13:31 2021 +0100
@@ -40,7 +40,7 @@
 from sat.core.constants import Const as C
 from sat.core.log import getLogger
 from sat.core import exceptions
-from sat.core.xmpp import SatXMPPEntity
+from sat.core.core_types import SatXMPPEntity
 from sat.tools import xml_tools
 from sat.tools import sat_defer
 from sat.tools import utils
@@ -54,8 +54,8 @@
 
 NS_MICROBLOG = "urn:xmpp:microblog:0"
 NS_ATOM = "http://www.w3.org/2005/Atom"
-NS_PUBSUB_EVENT = "{}{}".format(pubsub.NS_PUBSUB, "#event")
-NS_COMMENT_PREFIX = "{}:comments/".format(NS_MICROBLOG)
+NS_PUBSUB_EVENT = f"{pubsub.NS_PUBSUB}#event"
+NS_COMMENT_PREFIX = f"{NS_MICROBLOG}:comments/"
 
 
 PLUGIN_INFO = {
@@ -64,7 +64,7 @@
     C.PI_TYPE: "XEP",
     C.PI_PROTOCOLS: ["XEP-0277"],
     C.PI_DEPENDENCIES: ["XEP-0163", "XEP-0060", "TEXT_SYNTAXES"],
-    C.PI_RECOMMENDATIONS: ["XEP-0059", "EXTRA-PEP"],
+    C.PI_RECOMMENDATIONS: ["XEP-0059", "EXTRA-PEP", "PUBSUB_CACHE"],
     C.PI_MAIN: "XEP_0277",
     C.PI_HANDLER: "yes",
     C.PI_DESCRIPTION: _("""Implementation of microblogging Protocol"""),
@@ -85,6 +85,19 @@
         self._p = self.host.plugins[
             "XEP-0060"
         ]  # this facilitate the access to pubsub plugin
+        ps_cache = self.host.plugins.get("PUBSUB_CACHE")
+        if ps_cache is not None:
+            ps_cache.registerAnalyser(
+                {
+                    "name": "XEP-0277",
+                    "node": NS_MICROBLOG,
+                    "namespace": NS_ATOM,
+                    "type": "blog",
+                    "to_sync": True,
+                    "parser": self.item2mbdata,
+                    "match_cb": self._cacheNodeMatchCb,
+                }
+            )
         self.rt_sessions = sat_defer.RTDeferredSessions()
         self.host.plugins["XEP-0060"].addManagedNode(
             NS_MICROBLOG, items_cb=self._itemsReceived
@@ -117,7 +130,7 @@
         host.bridge.addMethod(
             "mbGet",
             ".plugin",
-            in_sign="ssiasa{ss}s",
+            in_sign="ssiasss",
             out_sign="s",
             method=self._mbGet,
             async_=True,
@@ -179,6 +192,15 @@
     def getHandler(self, client):
         return XEP_0277_handler()
 
+    def _cacheNodeMatchCb(
+        self,
+        client: SatXMPPEntity,
+        analyse: dict,
+    ) -> None:
+        """Check is analysed node is a comment and fill analyse accordingly"""
+        if analyse["node"].startswith(NS_COMMENT_PREFIX):
+            analyse["subtype"] = "comment"
+
     def _checkFeaturesCb(self, available):
         return {"available": C.BOOL_TRUE}
 
@@ -681,7 +703,7 @@
         @param item_id(unicode): id of the parent item
         @return (unicode): comment node to use
         """
-        return "{}{}".format(NS_COMMENT_PREFIX, item_id)
+        return f"{NS_COMMENT_PREFIX}{item_id}"
 
     def getCommentsService(self, client, parent_service=None):
         """Get prefered PubSub service to create comment node
@@ -930,7 +952,7 @@
         metadata['items'] = items
         return data_format.serialise(metadata)
 
-    def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra_dict=None,
+    def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra="",
                profile_key=C.PROF_KEY_NONE):
         """
         @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
@@ -939,14 +961,15 @@
         client = self.host.getClient(profile_key)
         service = jid.JID(service) if service else None
         max_items = None if max_items == C.NO_LIMIT else max_items
-        extra = self._p.parseExtra(extra_dict)
-        d = self.mbGet(client, service, node or None, max_items, item_ids,
+        extra = self._p.parseExtra(data_format.deserialise(extra))
+        d = defer.ensureDeferred(
+            self.mbGet(client, service, node or None, max_items, item_ids,
                        extra.rsm_request, extra.extra)
+        )
         d.addCallback(self._mbGetSerialise)
         return d
 
-    @defer.inlineCallbacks
-    def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None,
+    async def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None,
               rsm_request=None, extra=None):
         """Get some microblogs
 
@@ -954,6 +977,7 @@
             None to get profile's PEP
         @param node(unicode, None): node to get (or microblog node if None)
         @param max_items(int): maximum number of item to get, None for no limit
+            ignored if rsm_request is set
         @param item_ids (list[unicode]): list of item IDs
         @param rsm_request (rsm.RSMRequest): RSM request data
         @param extra (dict): extra data
@@ -962,7 +986,9 @@
         """
         if node is None:
             node = NS_MICROBLOG
-        items_data = yield self._p.getItems(
+        if rsm_request:
+            max_items = None
+        items_data = await self._p.getItems(
             client,
             service,
             node,
@@ -971,9 +997,9 @@
             rsm_request=rsm_request,
             extra=extra,
         )
-        mb_data = yield self._p.transItemsDataD(
+        mb_data = await self._p.transItemsDataD(
             items_data, partial(self.item2mbdata, client, service=service, node=node))
-        defer.returnValue(mb_data)
+        return mb_data
 
     def _mbRename(self, service, node, item_id, new_id, profile_key):
         return defer.ensureDeferred(self.mbRename(
@@ -1370,13 +1396,15 @@
                         service = jid.JID(service_s)
                         node = item["{}{}".format(prefix, "_node")]
                         # time to get the comments
-                        d = self._p.getItems(
-                            client,
-                            service,
-                            node,
-                            max_comments,
-                            rsm_request=rsm_comments,
-                            extra=extra_comments,
+                        d = defer.ensureDeferred(
+                            self._p.getItems(
+                                client,
+                                service,
+                                node,
+                                max_comments,
+                                rsm_request=rsm_comments,
+                                extra=extra_comments,
+                            )
                         )
                         # then serialise
                         d.addCallback(
@@ -1418,9 +1446,9 @@
 
         deferreds = {}
         for service, node in node_data:
-            d = deferreds[(service, node)] = self._p.getItems(
+            d = deferreds[(service, node)] = defer.ensureDeferred(self._p.getItems(
                 client, service, node, max_items, rsm_request=rsm_request, extra=extra
-            )
+            ))
             d.addCallback(
                 lambda items_data: self._p.transItemsDataD(
                     items_data,