diff src/plugins/plugin_xep_0277.py @ 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 955221487a3e
children 94901070478e
line wrap: on
line diff
--- 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>")
+