plugin XEP-0277: added mBGetFromManyWithComments to have items + comments in one method call

This commit is contained in:
Goffi 2015-08-15 22:24:38 +02:00
commit 1e21730bfe

View file

@ -26,6 +26,7 @@ from twisted.internet import defer
from twisted.python import failure
from sat.core import exceptions
from sat.tools.xml_tools import ElementParser
from sat.tools import sat_defer
from wokkel import pubsub
from wokkel import rsm
@ -63,6 +64,7 @@ class XEP_0277(object):
log.info(_("Microblogging plugin initialization"))
self.host = host
self._p = self.host.plugins["XEP-0060"] # this facilitate the access to pubsub plugin
self.rt_sessions = sat_defer.RTDeferredSessions()
self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog, notify=False)
host.bridge.addMethod("getLastMicroblogs", ".plugin",
in_sign='sis', out_sign='(aa{ss}a{ss})',
@ -82,6 +84,9 @@ class XEP_0277(object):
method=self._mBGetFromManyRTResult, async=True)
host.bridge.addMethod("mBGetFromMany", ".plugin", in_sign='sasia{ss}s', out_sign='s',
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)
## plugin management methods ##
@ -565,3 +570,116 @@ class XEP_0277(object):
"""
client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
return self._p.getFromMany(node_data, max_item, rsm_data, profile_key=profile_key)
# comments #
def _mBGetFromManyWithCommentsRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT):
"""Get real-time results for [mBGetFromManyWithComments] session
@param session_id: id of the real-time deferred session
@param return (tuple): (remaining, results) where:
- remaining is the number of still expected results
- results is a list of tuple with
- service (unicode): pubsub service
- node (unicode): pubsub node
- success (bool): True if the getItems was successful
- failure (unicode): empty string in case of success, error message else
- items(list): list of items with:
- item(dict): item microblog data
- comments_list(list): list of comments with
- service (unicode): pubsub service where the comments node is
- node (unicode): comments node
- failure (unicode): empty in case of success, else error message
- comments(list[dict]): list of microblog data
- comments_metadata(dict): metadata of the comment node
- metadata(dict): original node metadata
@param profile_key: %(doc_profile_key)s
"""
profile = self.host.getClient(profile_key).profile
d = self.rt_sessions.getResults(session_id, profile=profile)
d.addCallback(lambda ret: (ret[0],
[(service.full(), node, failure, items, metadata)
for (service, node), (success, (failure, (items, metadata))) in ret[1].iteritems()]))
return d
def _mBGetFromManyWithComments(self, publishers_type, publishers, max_item=10, max_comments=C.NO_LIMIT, rsm_dict=None, rsm_comments_dict=None, profile_key=C.PROF_KEY_NONE):
"""
@param max_item(int): maximum number of item to get, C.NO_LIMIT for no limit
@param max_comments(int): maximum number of comments to get, C.NO_LIMIT for no limit
"""
max_item = None if max_item == C.NO_LIMIT else max_item
max_comments = None if max_comments == C.NO_LIMIT else max_comments
publishers_type, publishers = self._checkPublishers(publishers_type, publishers)
return self.mBGetFromManyWithComments(publishers_type, publishers, max_item, max_comments,
rsm.RSMRequest(**rsm_dict) if rsm_dict else None,
rsm.RSMRequest(**rsm_comments_dict) if rsm_comments_dict else None,
profile_key)
def mBGetFromManyWithComments(self, publishers_type, publishers, max_item=None, max_comments=None, rsm_request=None, rsm_comments=None, profile_key=C.PROF_KEY_NONE):
"""Helper method to get the microblogs and their comments in one shot
@param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
@param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
@param max_items (int): optional limit on the number of retrieved items.
@param max_comments (int): maximum number of comments to retrieve
@param rsm_request (rsm.RSMRequest): RSM request for initial items only
@param rsm_comments (rsm.RSMRequest): RSM request for comments only
@param profile_key: profile key
@return: a deferred dict with:
- key: publisher (unicode)
- value: couple (list[dict], dict) with:
- the microblogs data
- RSM response data
"""
# XXX: this method seems complicated because it do a couple of treatments
# to serialise and associate the data, but it make life in frontends side
# a lot easier
def getComments(items_data):
"""Retrieve comments and add them to the items_data
@param items_data: serialised items data
@return (defer.Deferred): list of items where each item is associated
with a list of comments data (service, node, list of items, metadata)
"""
items, metadata = items_data
items_dlist = [] # deferred list for items
for item in items:
dlist = [] # deferred list for comments
for key, value in item.iteritems():
# we look for comments
if key.startswith('comments') and key.endswith('_service'):
prefix = key[:key.find('_')]
service_s = value
node = item["{}{}".format(prefix, "_node")]
# time to get the comments
d = self._p.getItems(jid.JID(service_s), node, max_comments, rsm_request=rsm_comments, profile_key=profile_key)
# then serialise
d.addCallback(lambda items_data: self._p.serItemsDataD(items_data, self.item2mbdata))
# with failure handling
d.addCallback(lambda serialised_items_data: ('',) + serialised_items_data)
d.addErrback(lambda failure: (unicode(failure.value), [], {}))
# and associate with service/node (needed if there are several comments nodes)
d.addCallback(lambda serialised: (service_s, node) + serialised)
dlist.append(d)
# we get the comments
comments_d = defer.gatherResults(dlist)
# and add them to the item data
comments_d.addCallback(lambda comments_data: (item, comments_data))
items_dlist.append(comments_d)
# we gather the items + comments in a list
items_d = defer.gatherResults(items_dlist)
# and add the metadata
items_d.addCallback(lambda items: (items, metadata))
return items_d
client, node_data = self._getClientAndNodeData(publishers_type, publishers, profile_key)
deferreds = {}
for service, node in node_data:
d = deferreds[(service, node)] = self._p.getItems(service, node, max_item, rsm_request=rsm_request, profile_key=profile_key)
d.addCallback(lambda items_data: self._p.serItemsDataD(items_data, self.item2mbdata))
d.addCallback(getComments)
d.addCallback(lambda items_comments_data: ('', items_comments_data))
d.addErrback(lambda failure: (unicode(failure.value), ([],{})))
return self.rt_sessions.newSession(deferreds, client.profile)