changeset 891:a7b2aacf22ac

plugin XEP-0060, groupblog: added nodeIdentifiers attribute to getItems in order to retrieve items by ids
author souliane <souliane@mailoo.org>
date Tue, 25 Feb 2014 17:49:15 +0100
parents 10bb8574ab11
children 58107179cd97
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0060.py
diffstat 2 files changed, 79 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Tue Feb 25 11:32:21 2014 +0100
+++ b/src/plugins/plugin_misc_groupblog.py	Tue Feb 25 17:49:15 2014 +0100
@@ -101,6 +101,11 @@
                               method=self.sendGroupBlogComment,
                               async=True)
 
+        host.bridge.addMethod("getGroupBlogs", ".plugin",
+                              in_sign='sass', out_sign='aa{ss}',
+                              method=self.getGroupBlogs,
+                              async=True)
+
         host.bridge.addMethod("getLastGroupBlogs", ".plugin",
                               in_sign='sis', out_sign='aa{ss}',
                               method=self.getLastGroupBlogs,
@@ -504,10 +509,11 @@
                     warning("Missing key for comments")
         defer.returnValue(ret)
 
-    def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
-        """Get the last published microblogs
+    def __getGroupBlogs(self, pub_jid_s, max_items=10, item_ids=None, profile_key='@NONE@'):
+        """Retrieve previously published items from a publish subscribe node.
         @param pub_jid_s: jid of the publisher
         @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
+        @param item_ids: list of microblogs items IDs
         @param profile_key: profile key
         @return: list of microblog data (dict)
         """
@@ -516,7 +522,7 @@
         def initialised(result):
             profile, client = result
             d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(pub_jid),
-                                                       max_items=max_items, profile_key=profile_key)
+                                                       max_items=max_items, item_ids=item_ids, profile_key=profile_key)
             d.addCallback(self._itemsConstruction, pub_jid, client)
             d.addErrback(lambda ignore: {})  # TODO: more complete error management (log !)
             return d
@@ -524,6 +530,26 @@
         #TODO: we need to use the server corresponding the the host of the jid
         return self._initialise(profile_key).addCallback(initialised)
 
+    def getGroupBlogs(self, pub_jid_s, item_ids=None, profile_key='@NONE@'):
+        """Get the published microblogs of the specified IDs. If item_ids is
+        None, the result would be the same than calling getLastGroupBlogs
+        with the default value for the attribute max_items.
+        @param pub_jid_s: jid of the publisher
+        @param item_ids: list of microblogs items IDs
+        @param profile_key: profile key
+        @return: list of microblog data (dict)
+        """
+        return self.__getGroupBlogs(pub_jid_s, item_ids=item_ids, profile_key=profile_key)
+
+    def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
+        """Get the last published microblogs
+        @param pub_jid_s: jid of the publisher
+        @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
+        @param profile_key: profile key
+        @return: list of microblog data (dict)
+        """
+        return self.__getGroupBlogs(pub_jid_s, max_items=max_items, profile_key=profile_key)
+
     def getLastGroupBlogsAtom(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
         """Get the atom feed of the last published microblogs
         @param pub_jid: jid of the publisher
--- a/src/plugins/plugin_xep_0060.py	Tue Feb 25 11:32:21 2014 +0100
+++ b/src/plugins/plugin_xep_0060.py	Tue Feb 25 17:49:15 2014 +0100
@@ -99,9 +99,9 @@
         profile, client = self.__getClientNProfile(profile_key, 'publish item')
         return client.publish(service, nodeIdentifier, items, client.parent.jid)
 
-    def getItems(self, service, node, max_items=None, sub_id=None, profile_key='@DEFAULT@'):
+    def getItems(self, service, node, max_items=None, item_ids=None, sub_id=None, profile_key='@DEFAULT@'):
         profile, client = self.__getClientNProfile(profile_key, 'get items')
-        return client.items(service, node, max_items, sub_id, client.parent.jid)
+        return client.items(service, node, max_items, item_ids, sub_id, client.parent.jid)
 
     def getOptions(self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None, profile_key='@DEFAULT@'):
         profile, client = self.__getClientNProfile(profile_key, 'get options')
@@ -139,7 +139,54 @@
     def connectionInitialized(self):
         pubsub.PubSubClient.connectionInitialized(self)
 
-    # XXX: this should be done in wokkel
+    # FIXME: we have to temporary override this method here just
+    # to set the attributes itemIdentifiers which is not used
+    # in pubsub.PubSubClient.items
+    def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
+              subscriptionIdentifier=None, sender=None):
+        """
+        Retrieve previously published items from a publish subscribe node.
+
+        @param service: The publish subscribe service that keeps the node.
+        @type service: L{JID<twisted.words.protocols.jabber.jid.JID>}
+
+        @param nodeIdentifier: The identifier of the node.
+        @type nodeIdentifier: C{unicode}
+
+        @param maxItems: Optional limit on the number of retrieved items.
+        @type maxItems: C{int}
+
+        @param itemIdentifiers: Identifiers of the items to be retracted.
+        @type itemIdentifiers: C{set}
+
+        @param subscriptionIdentifier: Optional subscription identifier. In
+            case the node has been subscribed to multiple times, this narrows
+            the results to the specific subscription.
+        @type subscriptionIdentifier: C{unicode}
+        """
+        NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
+
+        request = PubSubRequest('items')
+        request.recipient = service
+        request.nodeIdentifier = nodeIdentifier
+        if maxItems:
+            request.maxItems = str(int(maxItems))
+        request.subscriptionIdentifier = subscriptionIdentifier
+        request.sender = sender
+        request.itemIdentifiers = itemIdentifiers  # XXX: this line has been added
+
+        def cb(iq):
+            items = []
+            for element in iq.pubsub.items.elements():
+                if element.uri == NS_PUBSUB and element.name == 'item':
+                    items.append(element)
+            return items
+
+        d = request.send(self.xmlstream)
+        d.addCallback(cb)
+        return d
+
+    # FIXME: this should be done in wokkel
     def retractItems(self, service, nodeIdentifier, itemIdentifiers, sender=None):
         """
         Retract items from a publish subscribe node.