changeset 1516:afa0894dcc71

plugin XEP-0277, misc_groupblog: move getGroupBlogsAtom to XEP-0277, rename to mbGetAtom
author souliane <souliane@mailoo.org>
date Thu, 10 Sep 2015 15:06:01 +0200
parents 30ba7cac1dd2
children 4eca4f5e21ec
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0277.py
diffstat 2 files changed, 71 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Thu Sep 10 15:04:16 2015 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Thu Sep 10 15:06:01 2015 +0200
@@ -28,7 +28,6 @@
 from wokkel import disco, data_form, iwokkel
 from wokkel import rsm
 from zope.interface import implements
-from feed import date
 # import uuid
 
 try:
@@ -116,11 +115,6 @@
                               method=self.getGroupBlogsWithComments,
                               async=True)
 
-        host.bridge.addMethod("getGroupBlogsAtom", ".plugin",
-                              in_sign='sa{ss}s', out_sign='s',
-                              method=self.getGroupBlogsAtom,
-                              async=True)
-
         # host.bridge.addMethod("getMassiveGroupBlogs", ".plugin",
         #                       in_sign='sasa{ss}s', out_sign='a{s(aa{ss}a{ss})}',
         #                       method=self._getMassiveGroupBlogs,
@@ -616,47 +610,6 @@
         assert max_comments > 0  # otherwise the return signature is not the same
         return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm_data=rsm_data, max_comments=max_comments, profile_key=profile_key)
 
-    def getGroupBlogsAtom(self, pub_jid_s, rsm_data=None, profile_key=C.PROF_KEY_NONE):
-        """Get the atom feed of the last published microblogs
-        @param pub_jid: jid of the publisher
-        @param profile_key: profile key
-        @return: a deferred unicode (atom XML feed)
-        """
-        pub_jid = jid.JID(pub_jid_s)
-
-        def removeAllURIs(element):
-            """Recursively remove the URIs of the element and its children.
-            Without that, the entry would still be valid but not displayed
-            by Firefox nor Thunderbird (and probably more readers)"""
-            element.uri = element.defaultUri = None
-            for child in element.children:
-                if isinstance(child, Element):
-                    removeAllURIs(child)
-
-        def items2feed(items, pub_jid, client):
-            feed = """<?xml version="1.0" encoding="utf-8"?>
-<feed xmlns="http://www.w3.org/2005/Atom">
-    <title>%(user)s's blogposts</title>
-    <link href="%(feed)s" rel="self" />
-    <link href="%(blog)s" />
-    <id>%(id)s</id>
-    <updated>%(date)s</updated>\n""" % {'user': pub_jid.user,
-                                        'feed': 'http://%s/blog/%s/atom.xml' % (client.jid.host, pub_jid.user),
-                                        'blog': 'http://%s/blog/%s' % (client.jid.host, pub_jid.user),
-                                        'id': self.getNodeName(pub_jid),
-                                        'date': date.rfc3339.timestamp_from_tf(date.rfc3339.tf_utc())}
-            for item in items:
-                entry = item.firstChildElement()
-                removeAllURIs(entry)
-                feed += "    " + entry.toXml() + "\n"
-            return feed + "</feed>"
-
-        def cb(items, client):
-            return items2feed(items, pub_jid, client)
-
-        d = DeferredItems(self, cb, lambda dummy: [''], profile_key).get(self.getNodeName(pub_jid), rsm_data=rsm_data)
-        return d.addCallback(lambda res: res[0])
-
     # def _getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE):
     #     if publishers_type == 'JID':
     #         publishers_jids = [jid.JID(publisher) for publisher in publishers]
--- a/src/plugins/plugin_xep_0277.py	Thu Sep 10 15:04:16 2015 +0200
+++ b/src/plugins/plugin_xep_0277.py	Thu Sep 10 15:06:01 2015 +0200
@@ -96,7 +96,10 @@
                               method=self._mbGetFromMany)
         host.bridge.addMethod("mbGetFromManyWithCommentsRTResult", ".plugin", in_sign='ss', out_sign='(ua(sssa(a{ss}a(sssaa{ss}a{ss}))a{ss}))',
                               method=self._mbGetFromManyWithCommentsRTResult, async=True)
-        host.bridge.addMethod("mbGetFromManyWithComments", ".plugin", in_sign='sasiia{ss}a{ss}s', out_sign='s', method=self._mbGetFromManyWithComments)
+        host.bridge.addMethod("mbGetFromManyWithComments", ".plugin", in_sign='sasiia{ss}a{ss}s', out_sign='s',
+                              method=self._mbGetFromManyWithComments)
+        host.bridge.addMethod("mbGetAtom", ".plugin", in_sign='ssiasa{ss}s', out_sign='s',
+                              method=self._mbGetAtom, async=True)
 
     ## plugin management methods ##
 
@@ -498,14 +501,15 @@
 
     @defer.inlineCallbacks
     def mbGet(self, service_jid, node=None, max_items=None, item_ids=None, rsm_request=None, extra=None, profile_key=C.PROF_KEY_NONE):
-        """Get the last published microblogs
+        """Get some microblogs
 
         @param service_jid(jid.JID): jid of the publisher
         @param node(unicode, None): node to get (or microblog node if None)
         @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
         @param item_ids (list[unicode]): list of item IDs
         @param rsm_request (rsm.RSMRequest): RSM request data
-        @param profile_key: profile key
+        @param extra (dict): extra data
+        @param profile_key: %(doc_profile_key)s
 
         @return: a deferred couple with the list of items and metadatas.
         """
@@ -815,3 +819,67 @@
             d.addErrback(lambda failure: (unicode(failure.value), ([],{})))
 
         return self.rt_sessions.newSession(deferreds, client.profile)
+
+    # atom feed
+
+    def _mbGetAtom(self,  service_jid_s, node="", max_items=10, item_ids=None, extra_dict=None, profile_key=C.PROF_KEY_NONE):
+        """Get the atom feed of the last published microblogs
+
+        @param service_jid(jid.JID): jid of the publisher
+        @param node(unicode, None): node to get (or microblog node if None)
+        @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
+        @param item_ids (list[unicode]): list of item IDs
+        @param rsm_request (rsm.RSMRequest): RSM request data
+        @param extra (dict): extra data
+        @param profile_key: %(doc_profile_key)s
+        @return: a deferred unicode (atom XML feed)
+        """
+        max_items = None if max_items == C.NO_LIMIT else max_items
+        extra = self._p.parseExtra(extra_dict)
+        return self.mbGetAtom(jid.JID(service_jid_s), node or None, max_items, item_ids, extra.rsm_request, extra.extra, profile_key)
+
+    @defer.inlineCallbacks
+    def mbGetAtom(self, service_jid, node=None, max_items=None, item_ids=None, rsm_request=None, extra=None, profile_key=C.PROF_KEY_NONE):
+        """Get the atom feed of the last published microblogs
+
+        @param service_jid(jid.JID): jid of the publisher
+        @param node(unicode, None): node to get (or microblog node if None)
+        @param max_items(int): maximum number of item to get, C.NO_LIMIT for no limit
+        @param item_ids (list[unicode]): list of item IDs
+        @param rsm_request (rsm.RSMRequest): RSM request data
+        @param extra (dict): extra data
+        @param profile_key: %(doc_profile_key)s
+
+        @return: a deferred couple with the list of items and metadatas.
+        """
+        if node is None:
+            node = NS_MICROBLOG
+        items, metadata = yield self._p.getItems(service_jid, node, max_items=max_items, item_ids=item_ids, rsm_request=rsm_request, extra=extra, profile_key=profile_key)
+
+        feed = """<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <title>%(user)s's blogposts</title>
+    <link href="%(feed)s" rel="self" />
+    <link href="%(blog)s" />
+    <id>%(id)s</id>
+    <updated>%(date)s</updated>\n""" % {'user': service_jid.user,
+                                        'feed': 'http://%s/blog/%s/atom.xml' % (service_jid.host, service_jid.user),
+                                        'blog': 'http://%s/blog/%s' % (service_jid.host, service_jid.user),
+                                        'id': node,
+                                        'date': rfc3339.timestamp_from_tf(rfc3339.tf_utc())}
+
+        def removeAllURIs(element):
+            """Recursively remove the URIs of the element and its children.
+            Without that, the entry would still be valid but not displayed
+            by Firefox nor Thunderbird (and probably more readers)"""
+            element.uri = element.defaultUri = None
+            for child in element.children:
+                if isinstance(child, domish.Element):
+                    removeAllURIs(child)
+
+        for item in items:
+            entry = item.firstChildElement()
+            removeAllURIs(entry)
+            feed += "    " + entry.toXml() + "\n"
+        defer.returnValue(feed + "</feed>")
+