comparison src/plugins/plugin_xep_0277.py @ 1452:5116d70ddd1c

plugin xep-0060,xep-0277: added getFromMany using new RTDeferredSession mechanism
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:22:34 +0200
parents 9b88b19b1ca8
children d5e72362ee91
comparison
equal deleted inserted replaced
1451:9b88b19b1ca8 1452:5116d70ddd1c
26 from twisted.python import failure 26 from twisted.python import failure
27 from sat.core import exceptions 27 from sat.core import exceptions
28 from sat.tools.xml_tools import ElementParser 28 from sat.tools.xml_tools import ElementParser
29 29
30 from wokkel import pubsub 30 from wokkel import pubsub
31 from wokkel import rsm
31 from feed import atom, date 32 from feed import atom, date
32 import uuid 33 import uuid
33 from time import time 34 from time import time
34 import urlparse 35 import urlparse
35 from cgi import escape 36 from cgi import escape
75 host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='', 76 host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='',
76 method=self.setMicroblogAccess, 77 method=self.setMicroblogAccess,
77 async=True) 78 async=True)
78 host.bridge.addMethod("mBSubscribeToMany", ".plugin", in_sign='sass', out_sign='s', 79 host.bridge.addMethod("mBSubscribeToMany", ".plugin", in_sign='sass', out_sign='s',
79 method=self._mBSubscribeToMany) 80 method=self._mBSubscribeToMany)
81 host.bridge.addMethod("mBGetFromManyRTResult", ".plugin", in_sign='ss', out_sign='(ua(sssaa{ss}a{ss}))',
82 method=self._mBGetFromManyRTResult, async=True)
83 host.bridge.addMethod("mBGetFromMany", ".plugin", in_sign='sasia{ss}s', out_sign='s',
84 method=self._mBGetFromMany)
80 85
81 ## plugin management methods ## 86 ## plugin management methods ##
82 87
83 def microblogCB(self, itemsEvent, profile): 88 def microblogCB(self, itemsEvent, profile):
84 """Callback to "MICROBLOG" PEP event.""" 89 """Callback to "MICROBLOG" PEP event."""
327 @param max_items: how many microblogs we want to get 332 @param max_items: how many microblogs we want to get
328 @param profile_key: profile key 333 @param profile_key: profile key
329 334
330 @return: a deferred couple with the list of items and metadatas. 335 @return: a deferred couple with the list of items and metadatas.
331 """ 336 """
332 items, metadata = yield self.host.plugins["XEP-0060"].getItems(pub_jid, NS_MICROBLOG, max_items=max_items, profile_key=profile_key) 337 items_data = yield self._p.getItems(pub_jid, NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
333 dlist_result = yield defer.DeferredList(map(self.item2mbdata, items), consumeErrors=True) 338 serialised = yield self._p.serItemsDataD(items_data, self.item2mbdata)
334 items_data = [] 339 defer.returnValue(serialised)
335 for success, value in dlist_result:
336 if success:
337 items_data.append(value)
338 else:
339 log.warning(u"Error while parsing microblog data: {}".format(value.value))
340 defer.returnValue((items_data, metadata))
341 340
342 def parseCommentUrl(self, node_url): 341 def parseCommentUrl(self, node_url):
343 """Parse a XMPP URI 342 """Parse a XMPP URI
344 343
345 Determine the fields comments_service and comments_node of a microblog data 344 Determine the fields comments_service and comments_node of a microblog data
466 @return (str): session id 465 @return (str): session id
467 """ 466 """
468 client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key) 467 client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
469 return self._p.subscribeToMany(node_data, client.jid.userhostJID(), profile_key=profile_key) 468 return self._p.subscribeToMany(node_data, client.jid.userhostJID(), profile_key=profile_key)
470 469
470 # get #
471
472 def _mBGetFromManyRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT):
473 """Get real-time results for mBGetFromMany session
474
475 @param session_id: id of the real-time deferred session
476 @param return (tuple): (remaining, results) where:
477 - remaining is the number of still expected results
478 - results is a list of tuple with
479 - service (unicode): pubsub service
480 - node (unicode): pubsub node
481 - failure (unicode): empty string in case of success, error message else
482 - items_data(tuple): data tuple as returned by [getLastMicroblogs]
483 @param profile_key: %(doc_profile_key)s
484 """
485 def onSuccess(items_data):
486 """convert items elements to list of microblog data in items_data"""
487 d = self._p.serItemsDataD(items_data, self.item2mbdata)
488 d.addCallback(lambda serialised:('', serialised))
489 return d
490
491 profile = self.host.getClient(profile_key).profile
492 d = self._p.getRTResults(session_id,
493 on_success = onSuccess,
494 on_error = lambda failure: (unicode(failure.value), ([],{})),
495 profile = profile)
496 d.addCallback(lambda ret: (ret[0],
497 [(service.full(), node, failure, items, metadata)
498 for (service, node), (success, (failure, (items, metadata))) in ret[1].iteritems()]))
499 return d
500
501 def _mBGetFromMany(self, publishers_type, publishers, max_item=10, rsm_dict=None, profile_key=C.PROF_KEY_NONE):
502 """
503 @param max_item(int): maximum number of item to get, C.NO_LIMIT for no limit
504 """
505 max_item = None if max_item == C.NO_LIMIT else max_item
506 publishers_type, publishers = self._checkPublishers(publishers_type, publishers)
507 return self.mBGetFromMany(publishers_type, publishers, max_item, rsm.RSMRequest(**rsm_dict) if rsm_dict else None, profile_key)
508
509 def mBGetFromMany(self, publishers_type, publishers, max_item=None, rsm_data=None, profile_key=C.PROF_KEY_NONE):
510 """Get the published microblogs for a list of groups or jids
511
512 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
513 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
514 @param max_items (int): optional limit on the number of retrieved items.
515 @param rsm_data (rsm.RSMRequest): RSM request data, common to all publishers
516 @param profile_key: profile key
517 @return: a deferred dict with:
518 - key: publisher (unicode)
519 - value: couple (list[dict], dict) with:
520 - the microblogs data
521 - RSM response data
522 """
523 client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
524 return self._p.getFromMany(node_data, max_item, rsm_data, profile_key=profile_key)