comparison sat/plugins/plugin_xep_0277.py @ 4023:78b5f356900c

component AP gateway: handle attachments
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 15:42:21 +0100
parents d8a1219e913f
children 524856bd7b19
comparison
equal deleted inserted replaced
4022:cdb7de398c85 4023:78b5f356900c
95 "name": "XEP-0277", 95 "name": "XEP-0277",
96 "node": NS_MICROBLOG, 96 "node": NS_MICROBLOG,
97 "namespace": NS_ATOM, 97 "namespace": NS_ATOM,
98 "type": "blog", 98 "type": "blog",
99 "to_sync": True, 99 "to_sync": True,
100 "parser": self.item2mbdata, 100 "parser": self.item_2_mb_data,
101 "match_cb": self._cacheNodeMatchCb, 101 "match_cb": self._cacheNodeMatchCb,
102 } 102 }
103 ) 103 )
104 self.rt_sessions = sat_defer.RTDeferredSessions() 104 self.rt_sessions = sat_defer.RTDeferredSessions()
105 self.host.plugins["XEP-0060"].addManagedNode( 105 self.host.plugins["XEP-0060"].addManagedNode(
247 ) 247 )
248 248
249 for item in itemsEvent.items: 249 for item in itemsEvent.items:
250 if item.name == C.PS_ITEM: 250 if item.name == C.PS_ITEM:
251 # FIXME: service and node should be used here 251 # FIXME: service and node should be used here
252 self.item2mbdata(client, item, None, None).addCallbacks( 252 self.item_2_mb_data(client, item, None, None).addCallbacks(
253 manageItem, lambda failure: None, (C.PS_PUBLISH,) 253 manageItem, lambda failure: None, (C.PS_PUBLISH,)
254 ) 254 )
255 elif item.name == C.PS_RETRACT: 255 elif item.name == C.PS_RETRACT:
256 manageItem({"id": item["id"]}, C.PS_RETRACT) 256 manageItem({"id": item["id"]}, C.PS_RETRACT)
257 else: 257 else:
258 raise exceptions.InternalError("Invalid event value") 258 raise exceptions.InternalError("Invalid event value")
259 259
260 ## data/item transformation ## 260 ## data/item transformation ##
261 261
262 @defer.inlineCallbacks 262 @defer.inlineCallbacks
263 def item2mbdata( 263 def item_2_mb_data(
264 self, 264 self,
265 client: SatXMPPEntity, 265 client: SatXMPPEntity,
266 item_elt: domish.Element, 266 item_elt: domish.Element,
267 service: Optional[jid.JID], 267 service: Optional[jid.JID],
268 # FIXME: node is Optional until all calls to item2mbdata set properly service 268 # FIXME: node is Optional until all calls to item_2_mb_data set properly service
269 # and node. Once done, the Optional must be removed here 269 # and node. Once done, the Optional must be removed here
270 node: Optional[str] 270 node: Optional[str]
271 ) -> dict: 271 ) -> dict:
272 """Convert an XML Item to microblog data 272 """Convert an XML Item to microblog data
273 273
613 "XEP-0277_item2data", item_elt, entry_elt, microblog_data 613 "XEP-0277_item2data", item_elt, entry_elt, microblog_data
614 ) 614 )
615 615
616 defer.returnValue(microblog_data) 616 defer.returnValue(microblog_data)
617 617
618 async def data2entry(self, client, mb_data, item_id, service, node): 618 async def mb_data_2_entry_elt(self, client, mb_data, item_id, service, node):
619 """Convert a data dict to en entry usable to create an item 619 """Convert a data dict to en entry usable to create an item
620 620
621 @param mb_data: data dict as given by bridge method. 621 @param mb_data: data dict as given by bridge method.
622 @param item_id(unicode): id of the item to use 622 @param item_id(unicode): id of the item to use
623 @param service(jid.JID, None): pubsub service where the item is sent 623 @param service(jid.JID, None): pubsub service where the item is sent
1034 1034
1035 try: 1035 try:
1036 await self._manageComments(client, data, service, node, item_id, access=None) 1036 await self._manageComments(client, data, service, node, item_id, access=None)
1037 except error.StanzaError: 1037 except error.StanzaError:
1038 log.warning("Can't create comments node for item {}".format(item_id)) 1038 log.warning("Can't create comments node for item {}".format(item_id))
1039 item = await self.data2entry(client, data, item_id, service, node) 1039 item = await self.mb_data_2_entry_elt(client, data, item_id, service, node)
1040 1040
1041 if not await self.host.trigger.asyncPoint( 1041 if not await self.host.trigger.asyncPoint(
1042 "XEP-0277_send", client, service, node, item, data 1042 "XEP-0277_send", client, service, node, item, data
1043 ): 1043 ):
1044 return None 1044 return None
1163 node = NS_MICROBLOG 1163 node = NS_MICROBLOG
1164 1164
1165 item_id = data.get("id", "") 1165 item_id = data.get("id", "")
1166 1166
1167 # we have to serialise then deserialise to be sure that all triggers are called 1167 # we have to serialise then deserialise to be sure that all triggers are called
1168 item_elt = await self.data2entry(client, data, item_id, service, node) 1168 item_elt = await self.mb_data_2_entry_elt(client, data, item_id, service, node)
1169 item_elt.uri = pubsub.NS_PUBSUB 1169 item_elt.uri = pubsub.NS_PUBSUB
1170 return await self.item2mbdata(client, item_elt, service, node) 1170 return await self.item_2_mb_data(client, item_elt, service, node)
1171 1171
1172 1172
1173 ## retract ## 1173 ## retract ##
1174 1174
1175 def _mbRetract(self, service_jid_s, nodeIdentifier, itemIdentifier, profile_key): 1175 def _mbRetract(self, service_jid_s, nodeIdentifier, itemIdentifier, profile_key):
1241 item_ids=item_ids, 1241 item_ids=item_ids,
1242 rsm_request=rsm_request, 1242 rsm_request=rsm_request,
1243 extra=extra, 1243 extra=extra,
1244 ) 1244 )
1245 mb_data_list, metadata = await self._p.transItemsDataD( 1245 mb_data_list, metadata = await self._p.transItemsDataD(
1246 items_data, partial(self.item2mbdata, client, service=service, node=node)) 1246 items_data, partial(self.item_2_mb_data, client, service=service, node=node))
1247 encrypted = metadata.pop("encrypted", None) 1247 encrypted = metadata.pop("encrypted", None)
1248 if encrypted is not None: 1248 if encrypted is not None:
1249 for mb_data in mb_data_list: 1249 for mb_data in mb_data_list:
1250 try: 1250 try:
1251 mb_data["encrypted"] = encrypted[mb_data["id"]] 1251 mb_data["encrypted"] = encrypted[mb_data["id"]]
1459 def onSuccess(items_data): 1459 def onSuccess(items_data):
1460 """convert items elements to list of microblog data in items_data""" 1460 """convert items elements to list of microblog data in items_data"""
1461 d = self._p.transItemsDataD( 1461 d = self._p.transItemsDataD(
1462 items_data, 1462 items_data,
1463 # FIXME: service and node should be used here 1463 # FIXME: service and node should be used here
1464 partial(self.item2mbdata, client), 1464 partial(self.item_2_mb_data, client),
1465 serialise=True 1465 serialise=True
1466 ) 1466 )
1467 d.addCallback(lambda serialised: ("", serialised)) 1467 d.addCallback(lambda serialised: ("", serialised))
1468 return d 1468 return d
1469 1469
1661 # then serialise 1661 # then serialise
1662 d.addCallback( 1662 d.addCallback(
1663 lambda items_data: self._p.transItemsDataD( 1663 lambda items_data: self._p.transItemsDataD(
1664 items_data, 1664 items_data,
1665 partial( 1665 partial(
1666 self.item2mbdata, client, service=service, node=node 1666 self.item_2_mb_data, client, service=service, node=node
1667 ), 1667 ),
1668 serialise=True 1668 serialise=True
1669 ) 1669 )
1670 ) 1670 )
1671 # with failure handling 1671 # with failure handling
1702 client, service, node, max_items, rsm_request=rsm_request, extra=extra 1702 client, service, node, max_items, rsm_request=rsm_request, extra=extra
1703 )) 1703 ))
1704 d.addCallback( 1704 d.addCallback(
1705 lambda items_data: self._p.transItemsDataD( 1705 lambda items_data: self._p.transItemsDataD(
1706 items_data, 1706 items_data,
1707 partial(self.item2mbdata, client, service=service, node=node), 1707 partial(self.item_2_mb_data, client, service=service, node=node),
1708 ) 1708 )
1709 ) 1709 )
1710 d.addCallback(getComments) 1710 d.addCallback(getComments)
1711 d.addCallback(lambda items_comments_data: ("", items_comments_data)) 1711 d.addCallback(lambda items_comments_data: ("", items_comments_data))
1712 d.addErrback(lambda failure: (str(failure.value), ([], {}))) 1712 d.addErrback(lambda failure: (str(failure.value), ([], {})))