diff sat/plugins/plugin_xep_0277.py @ 2807:0b7ce5daee9b

plugin XEP-0277: blog items data are now entirely serialised before going to bridge: So far, and for historical reasons, blog items data where serialised using a unicode: unicode dict, which was causing trouble for many types of values (timestamps, booleans, lists). This patch changes it by serialising the whole items before going to bridge, and deserialising it when going back. This way, complex data can be used easily in items. This impact psEvent and serItemsData* methods which are renamed transItemsData* because there are not always serialising anymore (a new argument "serialise" allows to specify it). When editing a blog post in jp, metadata are now more easy to manipulate, specially lists like tags.
author Goffi <goffi@goffi.org>
date Sat, 23 Feb 2019 18:59:00 +0100
parents 85d3240a400f
children ab2696e34d29
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0277.py	Wed Feb 20 19:42:35 2019 +0100
+++ b/sat/plugins/plugin_xep_0277.py	Sat Feb 23 18:59:00 2019 +0100
@@ -85,7 +85,7 @@
         host.bridge.addMethod(
             "mbSend",
             ".plugin",
-            in_sign="ssa{ss}s",
+            in_sign="ssss",
             out_sign="",
             method=self._mbSend,
             async=True,
@@ -102,7 +102,7 @@
             "mbGet",
             ".plugin",
             in_sign="ssiasa{ss}s",
-            out_sign="(aa{ss}a{ss})",
+            out_sign="(asa{ss})",
             method=self._mbGet,
             async=True,
         )
@@ -125,7 +125,7 @@
             "mbGetFromManyRTResult",
             ".plugin",
             in_sign="ss",
-            out_sign="(ua(sssaa{ss}a{ss}))",
+            out_sign="(ua(sssasa{ss}))",
             method=self._mbGetFromManyRTResult,
             async=True,
         )
@@ -140,7 +140,7 @@
             "mbGetFromManyWithCommentsRTResult",
             ".plugin",
             in_sign="ss",
-            out_sign="(ua(sssa(a{ss}a(sssaa{ss}a{ss}))a{ss}))",
+            out_sign="(ua(sssa(sa(sssasa{ss}))a{ss}))",
             method=self._mbGetFromManyWithCommentsRTResult,
             async=True,
         )
@@ -178,7 +178,7 @@
                 itemsEvent.sender.full(),
                 itemsEvent.nodeIdentifier,
                 event,
-                data,
+                data_format.serialise(data),
                 client.profile,
             )
 
@@ -196,7 +196,7 @@
 
     @defer.inlineCallbacks
     def item2mbdata(self, item_elt):
-        """Convert an XML Item to microblog data used in bridge API
+        """Convert an XML Item to microblog data
 
         @param item_elt: domish.Element of microblog item
         @return: microblog data (dictionary)
@@ -238,7 +238,8 @@
                 if data_elt is None:
                     raise failure.Failure(
                         exceptions.DataError(
-                            u"XHML content not wrapped in a <div/> element, this is not standard !"
+                            u"XHML content not wrapped in a <div/> element, this is not "
+                            u"standard !"
                         )
                     )
                 if data_elt.uri != C.NS_XHTML:
@@ -281,9 +282,8 @@
         try:
             id_elt = entry_elt.elements(NS_ATOM, "id").next()
         except StopIteration:
-            msg = u"No atom id found in the pubsub item {}, this is not standard !".format(
-                id_
-            )
+            msg = (u"No atom id found in the pubsub item {}, this is not standard !"
+                   .format(id_))
             log.warning(msg)
             microblog_data[u"atom_id"] = ""
         else:
@@ -343,18 +343,16 @@
         except StopIteration:
             msg = u"No atom updated element found in the pubsub item {}".format(id_)
             raise failure.Failure(exceptions.DataError(msg))
-        microblog_data[u"updated"] = unicode(
-            calendar.timegm(dateutil.parser.parse(unicode(updated_elt)).utctimetuple())
+        microblog_data[u"updated"] = calendar.timegm(
+            dateutil.parser.parse(unicode(updated_elt)).utctimetuple()
         )
         try:
             published_elt = entry_elt.elements(NS_ATOM, "published").next()
         except StopIteration:
             microblog_data[u"published"] = microblog_data[u"updated"]
         else:
-            microblog_data[u"published"] = unicode(
-                calendar.timegm(
-                    dateutil.parser.parse(unicode(published_elt)).utctimetuple()
-                )
+            microblog_data[u"published"] = calendar.timegm(
+                dateutil.parser.parse(unicode(published_elt)).utctimetuple()
             )
 
         # links
@@ -420,16 +418,17 @@
 
                 if not publisher:
                     log.debug(u"No publisher attribute, we can't verify author jid")
-                    microblog_data[u"author_jid_verified"] = C.BOOL_FALSE
+                    microblog_data[u"author_jid_verified"] = False
                 elif jid.JID(publisher).userhostJID() == jid.JID(uri).userhostJID():
-                    microblog_data[u"author_jid_verified"] = C.BOOL_TRUE
+                    microblog_data[u"author_jid_verified"] = True
                 else:
                     log.warning(
-                        u"item atom:uri differ from publisher attribute, spoofing attempt ? atom:uri = {} publisher = {}".format(
+                        u"item atom:uri differ from publisher attribute, spoofing "
+                        u"attempt ? atom:uri = {} publisher = {}".format(
                             uri, item_elt.getAttribute("publisher")
                         )
                     )
-                    microblog_data[u"author_jid_verified"] = C.BOOL_FALSE
+                    microblog_data[u"author_jid_verified"] = False
             # email
             try:
                 email_elt = author_elt.elements(NS_ATOM, "email").next()
@@ -439,11 +438,11 @@
                 microblog_data[u"author_email"] = unicode(email_elt)
 
             # categories
-            categories = (
+            categories = [
                 category_elt.getAttribute("term", "")
                 for category_elt in entry_elt.elements(NS_ATOM, "category")
-            )
-            data_format.iter2dict("tag", categories, microblog_data)
+            ]
+            microblog_data[u"tags"] = categories
 
         ## the trigger ##
         # if other plugins have things to add or change
@@ -572,7 +571,7 @@
         )
 
         ## categories ##
-        for tag in data_format.dict2iter("tag", data):
+        for tag in data.get('tags', []):
             category_elt = entry_elt.addElement("category")
             category_elt["term"] = tag
 
@@ -655,12 +654,16 @@
         @param access(unicode, None): access model
             None to use same access model as parent item
         """
-        # FIXME: if 'comments' already exists in mb_data, it is not used to create the Node
-        allow_comments = C.bool(mb_data.pop("allow_comments", "false"))
-        if not allow_comments:
+        # FIXME: if 'comments' already exists in mb_data,
+        #        it is not used to create the Node
+        allow_comments = mb_data.pop("allow_comments", None)
+        if allow_comments is None:
+            return
+        elif allow_comments == False:
             if "comments" in mb_data:
                 log.warning(
-                    u"comments are not allowed but there is already a comments node, it may be lost: {uri}".format(
+                    u"comments are not allowed but there is already a comments node, "
+                    u"it may be lost: {uri}".format(
                         uri=mb_data["comments"]
                     )
                 )
@@ -737,7 +740,8 @@
                 )
             if "comments_node" in mb_data or "comments_service" in mb_data:
                 raise exceptions.DataError(
-                    u"You can't use comments_service/comments_node and comments at the same time"
+                    u"You can't use comments_service/comments_node and comments at the "
+                    u"same time"
                 )
         else:
             mb_data["comments"] = self._p.getNodeURI(comments_service, comments_node)
@@ -746,6 +750,7 @@
         service = jid.JID(service) if service else None
         node = node if node else NS_MICROBLOG
         client = self.host.getClient(profile_key)
+        data = data_format.deserialise(data)
         return self.send(client, data, service, node)
 
     @defer.inlineCallbacks
@@ -760,7 +765,7 @@
             None is equivalend as using default value
         """
         # TODO: check that all data keys are used, this would avoid sending publicly a private message
-        #       by accident (e.g. if group pluging is not loaded, and "grou*" key are not used)
+        #       by accident (e.g. if group plugin is not loaded, and "group*" key are not used)
         if node is None:
             node = NS_MICROBLOG
 
@@ -788,6 +793,11 @@
 
     ## get ##
 
+    def _mbGetSerialise(self, data):
+        items, metadata = data
+        items = [data_format.serialise(item) for item in items]
+        return items, metadata
+
     def _mbGet(self, service="", node="", max_items=10, item_ids=None, extra_dict=None,
                profile_key=C.PROF_KEY_NONE):
         """
@@ -798,15 +808,10 @@
         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)
-        return self.mbGet(
-            client,
-            service,
-            node or None,
-            max_items,
-            item_ids,
-            extra.rsm_request,
-            extra.extra,
-        )
+        d = 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,
@@ -834,8 +839,8 @@
             rsm_request=rsm_request,
             extra=extra,
         )
-        serialised = yield self._p.serItemsDataD(items_data, self.item2mbdata)
-        defer.returnValue(serialised)
+        mb_data = yield self._p.transItemsDataD(items_data, self.item2mbdata)
+        defer.returnValue(mb_data)
 
     def parseCommentUrl(self, node_url):
         """Parse a XMPP URI
@@ -922,7 +927,8 @@
             C.ALL: get all jids from roster, publishers is not used
             C.GROUP: get jids from groups
             C.JID: use publishers directly as list of jids
-        @param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids)
+        @param publishers: list of publishers, according to "publishers_type" (None,
+            list of groups or list of jids)
         @param profile_key: %(doc_profile_key)s
         """
         client = self.host.getClient(profile_key)
@@ -931,7 +937,8 @@
         else:
             jids_set = client.roster.getJidsSet(publishers_type, publishers)
             if publishers_type == C.ALL:
-                try:  # display messages from salut-a-toi@libervia.org or other PEP services
+                try:
+                    # display messages from salut-a-toi@libervia.org or other PEP services
                     services = self.host.plugins["EXTRA-PEP"].getFollowedEntities(
                         profile_key
                     )
@@ -987,7 +994,8 @@
             C.ALL: get all jids from roster, publishers is not used
             C.GROUP: get jids from groups
             C.JID: use publishers directly as list of jids
-        @param publishers: list of publishers, according to "publishers_type" (None, list of groups or list of jids)
+        @param publishers: list of publishers, according to "publishers_type" (None, list
+            of groups or list of jids)
         @param profile: %(doc_profile)s
         @return (str): session id
         """
@@ -1017,7 +1025,7 @@
 
         def onSuccess(items_data):
             """convert items elements to list of microblog data in items_data"""
-            d = self._p.serItemsDataD(items_data, self.item2mbdata)
+            d = self._p.transItemsDataD(items_data, self.item2mbdata, serialise=True)
             d.addCallback(lambda serialised: ("", serialised))
             return d
 
@@ -1041,14 +1049,8 @@
         )
         return d
 
-    def _mbGetFromMany(
-        self,
-        publishers_type,
-        publishers,
-        max_items=10,
-        extra_dict=None,
-        profile_key=C.PROF_KEY_NONE,
-    ):
+    def _mbGetFromMany(self, publishers_type, publishers, max_items=10, extra_dict=None,
+                       profile_key=C.PROF_KEY_NONE):
         """
         @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
         """
@@ -1064,19 +1066,14 @@
             profile_key,
         )
 
-    def mbGetFromMany(
-        self,
-        publishers_type,
-        publishers,
-        max_items=None,
-        rsm_request=None,
-        extra=None,
-        profile_key=C.PROF_KEY_NONE,
-    ):
+    def mbGetFromMany(self, publishers_type, publishers, max_items=None, rsm_request=None,
+                      extra=None, profile_key=C.PROF_KEY_NONE):
         """Get the published microblogs for a list of groups or jids
 
-        @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
-        @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
+        @param publishers_type (str): type of the list of publishers (one of "GROUP" or
+            "JID" or "ALL")
+        @param publishers (list): list of publishers, according to publishers_type (list
+            of groups or list of jids)
         @param max_items (int): optional limit on the number of retrieved items.
         @param rsm_request (rsm.RSMRequest): RSM request data, common to all publishers
         @param extra (dict): Extra data
@@ -1093,9 +1090,31 @@
 
     # comments #
 
-    def _mbGetFromManyWithCommentsRTResult(
-        self, session_id, profile_key=C.PROF_KEY_DEFAULT
-    ):
+    def _mbGetFromManyWithCommentsRTResultSerialise(self, data):
+        """Serialisation of result
+
+        This is probably the longest method name of whole SàT ecosystem ^^
+        @param data(dict): data as received by rt_sessions
+        @return (tuple): see [_mbGetFromManyWithCommentsRTResult]
+        """
+        ret = []
+        data_iter = data[1].iteritems()
+        for (service, node), (success, (failure_, (items_data, metadata))) in data_iter:
+            items = []
+            for item, item_metadata in items_data:
+                item = data_format.serialise(item)
+                items.append((item, item_metadata))
+            ret.append((
+                service.full(),
+                node,
+                failure_,
+                items,
+                metadata))
+
+        return data[0], ret
+
+    def _mbGetFromManyWithCommentsRTResult(self, session_id,
+                                           profile_key=C.PROF_KEY_DEFAULT):
         """Get real-time results for [mbGetFromManyWithComments] session
 
         @param session_id: id of the real-time deferred session
@@ -1118,32 +1137,16 @@
         """
         profile = self.host.getClient(profile_key).profile
         d = self.rt_sessions.getResults(session_id, profile=profile)
-        d.addCallback(
-            lambda ret: (
-                ret[0],
-                [
-                    (service.full(), node, failure, items, metadata)
-                    for (service, node), (success, (failure, (items, metadata))) in ret[
-                        1
-                    ].iteritems()
-                ],
-            )
-        )
+        d.addCallback(self._mbGetFromManyWithCommentsRTResultSerialise)
         return d
 
-    def _mbGetFromManyWithComments(
-        self,
-        publishers_type,
-        publishers,
-        max_items=10,
-        max_comments=C.NO_LIMIT,
-        extra_dict=None,
-        extra_comments_dict=None,
-        profile_key=C.PROF_KEY_NONE,
-    ):
+    def _mbGetFromManyWithComments(self, publishers_type, publishers, max_items=10,
+                                   max_comments=C.NO_LIMIT, extra_dict=None,
+                                   extra_comments_dict=None, profile_key=C.PROF_KEY_NONE):
         """
         @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
-        @param max_comments(int): maximum number of comments to get, C.NO_LIMIT for no limit
+        @param max_comments(int): maximum number of comments to get, C.NO_LIMIT for no
+            limit
         """
         max_items = None if max_items == C.NO_LIMIT else max_items
         max_comments = None if max_comments == C.NO_LIMIT else max_comments
@@ -1162,22 +1165,16 @@
             profile_key,
         )
 
-    def mbGetFromManyWithComments(
-        self,
-        publishers_type,
-        publishers,
-        max_items=None,
-        max_comments=None,
-        rsm_request=None,
-        extra=None,
-        rsm_comments=None,
-        extra_comments=None,
-        profile_key=C.PROF_KEY_NONE,
-    ):
+    def mbGetFromManyWithComments(self, publishers_type, publishers, max_items=None,
+                                  max_comments=None, rsm_request=None, extra=None,
+                                  rsm_comments=None, extra_comments=None,
+                                  profile_key=C.PROF_KEY_NONE):
         """Helper method to get the microblogs and their comments in one shot
 
-        @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
-        @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
+        @param publishers_type (str): type of the list of publishers (one of "GROUP" or
+            "JID" or "ALL")
+        @param publishers (list): list of publishers, according to publishers_type (list
+            of groups or list of jids)
         @param max_items (int): optional limit on the number of retrieved items.
         @param max_comments (int): maximum number of comments to retrieve
         @param rsm_request (rsm.RSMRequest): RSM request for initial items only
@@ -1223,8 +1220,8 @@
                         )
                         # then serialise
                         d.addCallback(
-                            lambda items_data: self._p.serItemsDataD(
-                                items_data, self.item2mbdata
+                            lambda items_data: self._p.transItemsDataD(
+                                items_data, self.item2mbdata, serialise=True
                             )
                         )
                         # with failure handling
@@ -1232,7 +1229,8 @@
                             lambda serialised_items_data: ("",) + serialised_items_data
                         )
                         d.addErrback(lambda failure: (unicode(failure.value), [], {}))
-                        # and associate with service/node (needed if there are several comments nodes)
+                        # and associate with service/node (needed if there are several
+                        # comments nodes)
                         d.addCallback(
                             lambda serialised, service_s=service_s, node=node: (
                                 service_s,
@@ -1260,7 +1258,7 @@
                 client, service, node, max_items, rsm_request=rsm_request, extra=extra
             )
             d.addCallback(
-                lambda items_data: self._p.serItemsDataD(items_data, self.item2mbdata)
+                lambda items_data: self._p.transItemsDataD(items_data, self.item2mbdata)
             )
             d.addCallback(getComments)
             d.addCallback(lambda items_comments_data: ("", items_comments_data))