comparison sat/plugins/plugin_xep_0277.py @ 3584:edc79cefe968

plugin XEP-0060: `getItem(s)`, `publish` and `(un)subscribe` are now coroutines
author Goffi <goffi@goffi.org>
date Wed, 30 Jun 2021 16:19:14 +0200
parents 7df12ffa6620
children 5f65f4e9f8cb
comparison
equal deleted inserted replaced
3583:16ade4ad63f3 3584:edc79cefe968
939 """ 939 """
940 client = self.host.getClient(profile_key) 940 client = self.host.getClient(profile_key)
941 service = jid.JID(service) if service else None 941 service = jid.JID(service) if service else None
942 max_items = None if max_items == C.NO_LIMIT else max_items 942 max_items = None if max_items == C.NO_LIMIT else max_items
943 extra = self._p.parseExtra(extra_dict) 943 extra = self._p.parseExtra(extra_dict)
944 d = self.mbGet(client, service, node or None, max_items, item_ids, 944 d = defer.ensureDeferred(
945 self.mbGet(client, service, node or None, max_items, item_ids,
945 extra.rsm_request, extra.extra) 946 extra.rsm_request, extra.extra)
947 )
946 d.addCallback(self._mbGetSerialise) 948 d.addCallback(self._mbGetSerialise)
947 return d 949 return d
948 950
949 @defer.inlineCallbacks 951 async def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None,
950 def mbGet(self, client, service=None, node=None, max_items=10, item_ids=None,
951 rsm_request=None, extra=None): 952 rsm_request=None, extra=None):
952 """Get some microblogs 953 """Get some microblogs
953 954
954 @param service(jid.JID, None): jid of the publisher 955 @param service(jid.JID, None): jid of the publisher
955 None to get profile's PEP 956 None to get profile's PEP
961 962
962 @return: a deferred couple with the list of items and metadatas. 963 @return: a deferred couple with the list of items and metadatas.
963 """ 964 """
964 if node is None: 965 if node is None:
965 node = NS_MICROBLOG 966 node = NS_MICROBLOG
966 items_data = yield self._p.getItems( 967 items_data = await self._p.getItems(
967 client, 968 client,
968 service, 969 service,
969 node, 970 node,
970 max_items=max_items, 971 max_items=max_items,
971 item_ids=item_ids, 972 item_ids=item_ids,
972 rsm_request=rsm_request, 973 rsm_request=rsm_request,
973 extra=extra, 974 extra=extra,
974 ) 975 )
975 mb_data = yield self._p.transItemsDataD( 976 mb_data = await self._p.transItemsDataD(
976 items_data, partial(self.item2mbdata, client, service=service, node=node)) 977 items_data, partial(self.item2mbdata, client, service=service, node=node))
977 defer.returnValue(mb_data) 978 return mb_data
978 979
979 def _mbRename(self, service, node, item_id, new_id, profile_key): 980 def _mbRename(self, service, node, item_id, new_id, profile_key):
980 return defer.ensureDeferred(self.mbRename( 981 return defer.ensureDeferred(self.mbRename(
981 self.host.getClient(profile_key), 982 self.host.getClient(profile_key),
982 jid.JID(service) if service else None, 983 jid.JID(service) if service else None,
1370 prefix = key[: key.find("_")] 1371 prefix = key[: key.find("_")]
1371 service_s = value 1372 service_s = value
1372 service = jid.JID(service_s) 1373 service = jid.JID(service_s)
1373 node = item["{}{}".format(prefix, "_node")] 1374 node = item["{}{}".format(prefix, "_node")]
1374 # time to get the comments 1375 # time to get the comments
1375 d = self._p.getItems( 1376 d = defer.ensureDeferred(
1376 client, 1377 self._p.getItems(
1377 service, 1378 client,
1378 node, 1379 service,
1379 max_comments, 1380 node,
1380 rsm_request=rsm_comments, 1381 max_comments,
1381 extra=extra_comments, 1382 rsm_request=rsm_comments,
1383 extra=extra_comments,
1384 )
1382 ) 1385 )
1383 # then serialise 1386 # then serialise
1384 d.addCallback( 1387 d.addCallback(
1385 lambda items_data: self._p.transItemsDataD( 1388 lambda items_data: self._p.transItemsDataD(
1386 items_data, 1389 items_data,
1418 items_d.addCallback(lambda items_completed: (items_completed, metadata)) 1421 items_d.addCallback(lambda items_completed: (items_completed, metadata))
1419 return items_d 1422 return items_d
1420 1423
1421 deferreds = {} 1424 deferreds = {}
1422 for service, node in node_data: 1425 for service, node in node_data:
1423 d = deferreds[(service, node)] = self._p.getItems( 1426 d = deferreds[(service, node)] = defer.ensureDeferred(self._p.getItems(
1424 client, service, node, max_items, rsm_request=rsm_request, extra=extra 1427 client, service, node, max_items, rsm_request=rsm_request, extra=extra
1425 ) 1428 ))
1426 d.addCallback( 1429 d.addCallback(
1427 lambda items_data: self._p.transItemsDataD( 1430 lambda items_data: self._p.transItemsDataD(
1428 items_data, 1431 items_data,
1429 partial(self.item2mbdata, client, service=service, node=node), 1432 partial(self.item2mbdata, client, service=service, node=node),
1430 ) 1433 )