comparison 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
comparison
equal deleted inserted replaced
706:80e9d3ecb272 707:890fbf2d7fdd
81 if not node: 81 if not node:
82 raise exceptions.DataError('Invalid comments link') 82 raise exceptions.DataError('Invalid comments link')
83 83
84 return (service, node) 84 return (service, node)
85 85
86 @defer.inlineCallbacks
86 def item2mbdata(self, item): 87 def item2mbdata(self, item):
87 """Convert an XML Item to microblog data used in bridge API 88 """Convert an XML Item to microblog data used in bridge API
88 @param item: domish.Element of microblog item 89 @param item: domish.Element of microblog item
89 @return: microblog data (dictionary)""" 90 @return: microblog data (dictionary)"""
90 try: 91 try:
91 entry_elt = [child for child in item.elements() if child.name == "entry"][0] 92 entry_elt = [child for child in item.elements() if child.name == "entry"][0]
92 except IndexError: 93 except IndexError:
93 warning(_('No entry element in microblog item')) 94 warning(_('No entry element in microblog item'))
94 return 95 raise exceptions.DataError('no entry found')
95 _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8')) 96 _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8'))
96 microblog_data = {} 97 microblog_data = {}
97 try: 98 try:
98 microblog_data['content'] = _entry.title.text 99 try:
100 content_type =_entry.title.attrs['type'].lower()
101 except KeyError:
102 content_type = 'text'
103 if content_type == 'xhtml':
104 # TODO: proper check of body namespace
105 microblog_data['xhtml'] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(_entry.title.text)
106 microblog_data['content'] = _entry.title.text # FIXME: must use text version of the microblog, or convert XHTML to text if not available
107 else:
108 microblog_data['content'] = _entry.title.text
99 if len(_entry.authors): 109 if len(_entry.authors):
100 microblog_data['author'] = _entry.authors[0].name.text 110 microblog_data['author'] = _entry.authors[0].name.text
101 microblog_data['timestamp'] = str(int(_entry.updated.tf)) 111 microblog_data['timestamp'] = str(int(_entry.updated.tf))
102 microblog_data['id'] = item['id'] 112 microblog_data['id'] = item['id']
103 for link in _entry.links: 113 for link in _entry.links:
112 warning("Can't parse link") 122 warning("Can't parse link")
113 continue 123 continue
114 124
115 except (AttributeError, KeyError): 125 except (AttributeError, KeyError):
116 error(_('Error while parsing atom entry for microblogging event')) 126 error(_('Error while parsing atom entry for microblogging event'))
117 return {} 127 raise exceptions.DataError
118 128
119 ##XXX: workaround for Jappix behaviour 129 ##XXX: workaround for Jappix behaviour
120 if not 'author' in microblog_data: 130 if not 'author' in microblog_data:
121 from xe import NestElement 131 from xe import NestElement
122 try: 132 try:
124 author.import_xml(str(_entry)) 134 author.import_xml(str(_entry))
125 microblog_data['author'] = author.nick.text 135 microblog_data['author'] = author.nick.text
126 except: 136 except:
127 error(_('Cannot find author')) 137 error(_('Cannot find author'))
128 ##end workaround Jappix 138 ##end workaround Jappix
129 return microblog_data 139
140 defer.returnValue(microblog_data)
130 141
131 def microblogCB(self, itemsEvent, profile): 142 def microblogCB(self, itemsEvent, profile):
143 d = defer.Deferred()
144
145 def manageItem(microblog_data):
146 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
147
132 for item in itemsEvent.items: 148 for item in itemsEvent.items:
133 microblog_data = self.item2mbdata(item) 149 d.addCallback(lambda ignore: self.item2mbdata(item))
134 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) 150 d.addCallback(manageItem)
151
152 d.callback(None)
153 return d
135 154
136 @defer.inlineCallbacks 155 @defer.inlineCallbacks
137 def data2entry(self, data, profile): 156 def data2entry(self, data, profile):
138 """Convert a data dict to en entry usable to create an item 157 """Convert a data dict to en entry usable to create an item
139 @param data: data dict as given by bridge method 158 @param data: data dict as given by bridge method
186 """Get the last published microblogs 205 """Get the last published microblogs
187 @param pub_jid: jid of the publisher 206 @param pub_jid: jid of the publisher
188 @param max_items: how many microblogs we want to get 207 @param max_items: how many microblogs we want to get
189 @param profile_key: profile key 208 @param profile_key: profile key
190 """ 209 """
210 def resultToArray(result):
211 ret = []
212 for (success, value) in result:
213 if success:
214 ret.append(value)
215 else:
216 error('Error while getting last microblog')
217 return ret
218
191 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) 219 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
192 d.addCallback(lambda items: map(self.item2mbdata, items)) 220 d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items)))
221 d.addCallback(resultToArray)
222 return d
193 223
194 def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'): 224 def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
195 """Create a microblog node on PEP with given access 225 """Create a microblog node on PEP with given access
196 If the node already exists, it change options 226 If the node already exists, it change options
197 @param access: Node access model, according to xep-0060 #4.5 227 @param access: Node access model, according to xep-0060 #4.5