comparison sat/plugins/plugin_comp_ap_gateway/__init__.py @ 3844:65e5718e7710

component AP gateway: `Announce` activity implementation: `Announce` and `Undo` of `Announce` are now implemented and converted to suitable XEP-0277 "repeat" items, or retract. rel 370
author Goffi <goffi@goffi.org>
date Thu, 14 Jul 2022 12:55:30 +0200
parents 17c757bd74bc
children 4f9d4650eab5
comparison
equal deleted inserted replaced
3843:17c757bd74bc 3844:65e5718e7710
1265 """Convert AP item to parsed microblog data and corresponding item element""" 1265 """Convert AP item to parsed microblog data and corresponding item element"""
1266 mb_data = await self.apItem2MBdata(ap_item) 1266 mb_data = await self.apItem2MBdata(ap_item)
1267 item_elt = await self._m.data2entry( 1267 item_elt = await self._m.data2entry(
1268 self.client, mb_data, mb_data["id"], None, self._m.namespace 1268 self.client, mb_data, mb_data["id"], None, self._m.namespace
1269 ) 1269 )
1270 item_elt["publisher"] = mb_data["author_jid"] 1270 if "repeated" in mb_data["extra"]:
1271 item_elt["publisher"] = mb_data["extra"]["repeated"]["by"]
1272 else:
1273 item_elt["publisher"] = mb_data["author_jid"]
1271 return mb_data, item_elt 1274 return mb_data, item_elt
1272 1275
1273 async def apItem2Elt(self, ap_item: dict) -> domish.Element: 1276 async def apItem2Elt(self, ap_item: dict) -> domish.Element:
1274 """Convert AP item to XMPP item element""" 1277 """Convert AP item to XMPP item element"""
1275 __, item_elt = await self.apItem2MbDataAndElt(ap_item) 1278 __, item_elt = await self.apItem2MbDataAndElt(ap_item)
1358 ) 1361 )
1359 1362
1360 async def apItem2MBdata(self, ap_item: dict) -> dict: 1363 async def apItem2MBdata(self, ap_item: dict) -> dict:
1361 """Convert AP activity or object to microblog data 1364 """Convert AP activity or object to microblog data
1362 1365
1366 @param ap_item: ActivityPub item to convert
1367 Can be either an activity of an object
1363 @return: AP Item's Object and microblog data 1368 @return: AP Item's Object and microblog data
1364 @raise exceptions.DataError: something is invalid in the AP item 1369 @raise exceptions.DataError: something is invalid in the AP item
1365 @raise NotImplemented: some AP data is not handled yet 1370 @raise NotImplemented: some AP data is not handled yet
1366 @raise error.StanzaError: error while contacting the AP server 1371 @raise error.StanzaError: error while contacting the AP server
1367 """ 1372 """
1375 ap_object = ap_item 1380 ap_object = ap_item
1376 item_id = ap_object.get("id") 1381 item_id = ap_object.get("id")
1377 if not item_id: 1382 if not item_id:
1378 log.warning(f'No "id" found in AP item: {ap_object!r}') 1383 log.warning(f'No "id" found in AP item: {ap_object!r}')
1379 raise exceptions.DataError 1384 raise exceptions.DataError
1380 mb_data = {"id": item_id} 1385 mb_data = {"id": item_id, "extra": {}}
1381 1386
1382 # content 1387 # content
1383 try: 1388 try:
1384 language, content_xhtml = ap_object["contentMap"].popitem() 1389 language, content_xhtml = ap_object["contentMap"].popitem()
1385 except (KeyError, AttributeError): 1390 except (KeyError, AttributeError):
1425 mb_data[field] = calendar.timegm( 1430 mb_data[field] = calendar.timegm(
1426 dateutil.parser.parse(str(value)).utctimetuple() 1431 dateutil.parser.parse(str(value)).utctimetuple()
1427 ) 1432 )
1428 except dateutil.parser.ParserError as e: 1433 except dateutil.parser.ParserError as e:
1429 log.warning(f"Can't parse {field!r} field: {e}") 1434 log.warning(f"Can't parse {field!r} field: {e}")
1435
1436 # repeat
1437 if "_repeated" in ap_item:
1438 mb_data["extra"]["repeated"] = ap_item["_repeated"]
1430 1439
1431 # comments 1440 # comments
1432 in_reply_to = ap_object.get("inReplyTo") 1441 in_reply_to = ap_object.get("inReplyTo")
1433 __, comments_node = await self.getCommentsNodes(item_id, in_reply_to) 1442 __, comments_node = await self.getCommentsNodes(item_id, in_reply_to)
1434 if comments_node is not None: 1443 if comments_node is not None:
2192 2201
2193 @param destinee: jid of the destinee, 2202 @param destinee: jid of the destinee,
2194 @param node: XMPP pubsub node 2203 @param node: XMPP pubsub node
2195 @param activity: parent AP activity 2204 @param activity: parent AP activity
2196 @param item: AP object payload 2205 @param item: AP object payload
2206 only the "id" field is used
2197 """ 2207 """
2198 item_id = item.get("id") 2208 item_id = item.get("id")
2199 if not item_id: 2209 if not item_id:
2200 raise exceptions.DataError('"id" attribute is missing in item') 2210 raise exceptions.DataError('"id" attribute is missing in item')
2201 if not item_id.startswith("http"): 2211 if not item_id.startswith("http"):