comparison src/plugins/plugin_xep_0060.py @ 1773:6e867caf4621

plugin XEP-0060, tmp(wokkel.rsm): small refactoring: - don't use getRSMResponse anymore: rsm.PubSubClient use a different signature which return items and RSMResponse, this is better than the previous method as if the requested don't call getRSMResponse, there is no memory leak anymore. - use RSMResponse directly instead of using ext_data, as other extension (like MAM) will be probably be managed with an other workflow - don't do a useless deep copy of pubsub.PubSubRequest._parameters on each instance anymore - RSMResponse.parse do now manage parsing of <set/> element directly
author Goffi <goffi@goffi.org>
date Tue, 05 Jan 2016 23:20:22 +0100
parents d17772b0fe22
children 8b18e5f55a90
comparison
equal deleted inserted replaced
1772:666ab06a9d81 1773:6e867caf4621
32 # same thing for rsm 32 # same thing for rsm
33 from wokkel import pubsub 33 from wokkel import pubsub
34 from wokkel import rsm 34 from wokkel import rsm
35 from zope.interface import implements 35 from zope.interface import implements
36 from collections import namedtuple 36 from collections import namedtuple
37 import uuid 37
38 38
39 UNSPECIFIED = "unspecified error" 39 UNSPECIFIED = "unspecified error"
40 40
41 41
42 PLUGIN_INFO = { 42 PLUGIN_INFO = {
232 - list of items 232 - list of items
233 - metadata with the following keys: 233 - metadata with the following keys:
234 - rsm_first, rsm_last, rsm_count, rsm_index: first, last, count and index value of RSMResponse 234 - rsm_first, rsm_last, rsm_count, rsm_index: first, last, count and index value of RSMResponse
235 """ 235 """
236 if rsm_request and item_ids: 236 if rsm_request and item_ids:
237 raise ValueError("items_id can't be used with rsm") 237 raise ValueError(u"items_id can't be used with rsm")
238 if extra is None: 238 if extra is None:
239 extra = {} 239 extra = {}
240 client = self.host.getClient(profile_key) 240 client = self.host.getClient(profile_key)
241 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': rsm_request} if rsm_request is not None else None 241 d = client.pubsub_client.items(service, node, max_items, item_ids, sub_id, client.pubsub_client.parent.jid, rsm_request)
242 d = client.pubsub_client.items(service, node, max_items, item_ids, sub_id, client.pubsub_client.parent.jid, ext_data)
243 242
244 try: 243 try:
245 subscribe = C.bool(extra['subscribe']) 244 subscribe = C.bool(extra['subscribe'])
246 except KeyError: 245 except KeyError:
247 subscribe = False 246 subscribe = False
248 247
249 def subscribeEb(failure, service, node): 248 def subscribeEb(failure, service, node):
250 failure.trap(error.StanzaError) 249 failure.trap(error.StanzaError)
251 log.warning("Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value))) 250 log.warning(u"Could not subscribe to node {} on service {}: {}".format(node, unicode(service), unicode(failure.value)))
252 251
253 def doSubscribe(items): 252 def doSubscribe(items):
254 self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node) 253 self.subscribe(service, node, profile_key=profile_key).addErrback(subscribeEb, service, node)
255 return items 254 return items
256 255
257 if subscribe: 256 if subscribe:
258 d.addCallback(doSubscribe) 257 d.addCallback(doSubscribe)
259 258
260 def addMetadata(items): 259 def addMetadata(result):
261 metadata = {} 260 items, rsm_response = result
262 if rsm_request is not None: 261 if rsm_request is not None and rsm_response is not None:
263 rsm_data = client.pubsub_client.getRSMResponse(ext_data['id']) 262 metadata = {'rsm_{}'.format(key): value for key, value in rsm_response.toDict().iteritems()}
264 metadata.update({'rsm_{}'.format(key): value for key, value in rsm_data.iteritems()}) 263 else:
264 metadata = {}
265 return (items, metadata) 265 return (items, metadata)
266 266
267 d.addCallback(addMetadata) 267 d.addCallback(addMetadata)
268 return d 268 return d
269 269