diff src/plugins/plugin_xep_0277.py @ 707:890fbf2d7fdd

plugin XEP-0277, groupblog: rich text management for receiving microblogs
author Goffi <goffi@goffi.org>
date Thu, 14 Nov 2013 18:36:02 +0100
parents 80e9d3ecb272
children 812dc38c0094
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Thu Nov 14 18:35:51 2013 +0100
+++ b/src/plugins/plugin_xep_0277.py	Thu Nov 14 18:36:02 2013 +0100
@@ -83,6 +83,7 @@
 
         return (service, node)
 
+    @defer.inlineCallbacks
     def item2mbdata(self, item):
         """Convert an XML Item to microblog data used in bridge API
         @param item: domish.Element of microblog item
@@ -91,11 +92,20 @@
             entry_elt = [child for child in item.elements() if child.name == "entry"][0]
         except IndexError:
             warning(_('No entry element in microblog item'))
-            return
+            raise exceptions.DataError('no entry found')
         _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8'))
         microblog_data = {}
         try:
-            microblog_data['content'] = _entry.title.text
+            try:
+                content_type =_entry.title.attrs['type'].lower()
+            except KeyError:
+                content_type = 'text'
+            if content_type == 'xhtml':
+                # TODO: proper check of body namespace
+                microblog_data['xhtml'] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(_entry.title.text)
+                microblog_data['content'] = _entry.title.text # FIXME: must use text version of the microblog, or convert XHTML to text if not available
+            else:
+                microblog_data['content'] = _entry.title.text
             if len(_entry.authors):
                 microblog_data['author'] = _entry.authors[0].name.text
             microblog_data['timestamp'] = str(int(_entry.updated.tf))
@@ -114,7 +124,7 @@
 
         except (AttributeError, KeyError):
             error(_('Error while parsing atom entry for microblogging event'))
-            return {}
+            raise exceptions.DataError
 
         ##XXX: workaround for Jappix behaviour
         if not 'author' in microblog_data:
@@ -126,12 +136,21 @@
             except:
                 error(_('Cannot find author'))
         ##end workaround Jappix
-        return microblog_data
+
+        defer.returnValue(microblog_data)
 
     def microblogCB(self, itemsEvent, profile):
+        d = defer.Deferred()
+
+        def manageItem(microblog_data):
+            self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
+
         for item in itemsEvent.items:
-            microblog_data = self.item2mbdata(item)
-            self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
+            d.addCallback(lambda ignore: self.item2mbdata(item))
+            d.addCallback(manageItem)
+
+        d.callback(None)
+        return d
 
     @defer.inlineCallbacks
     def data2entry(self, data, profile):
@@ -188,8 +207,19 @@
         @param max_items: how many microblogs we want to get
         @param profile_key: profile key
         """
+        def resultToArray(result):
+            ret = []
+            for (success, value) in result:
+                if success:
+                    ret.append(value)
+                else:
+                    error('Error while getting last microblog')
+            return ret
+
         d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
-        d.addCallback(lambda items: map(self.item2mbdata, items))
+        d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items)))
+        d.addCallback(resultToArray)
+        return d
 
     def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
         """Create a microblog node on PEP with given access