Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_comp_ap_gateway/events.py @ 4259:49019947cc76
component AP Gateway: implement HTTP GET signature.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 05 Jun 2024 22:34:09 +0200 |
parents | 4b842c1fb686 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4258:ba28ca268f4a | 4259:49019947cc76 |
---|---|
102 self, event_data: dict, author_jid: jid.JID, is_new: bool=True | 102 self, event_data: dict, author_jid: jid.JID, is_new: bool=True |
103 ) -> dict: | 103 ) -> dict: |
104 """Convert event data to AP activity | 104 """Convert event data to AP activity |
105 | 105 |
106 @param event_data: event data as used in [plugin_exp_events] | 106 @param event_data: event data as used in [plugin_exp_events] |
107 @param author_jid: jid of the published of the event | 107 @param author_jid: jid of the publisher of the event |
108 @param is_new: if True, the item is a new one (no instance has been found in | 108 @param is_new: if True, the item is a new one (no instance has been found in |
109 cache). | 109 cache). |
110 If True, a "Create" activity will be generated, otherwise an "Update" one will | 110 If True, a "Create" activity will be generated, otherwise an "Update" one will |
111 be | 111 be |
112 @return: AP activity wrapping an Event object | 112 @return: AP activity wrapping an Event object |
234 "Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item | 234 "Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item |
235 ) | 235 ) |
236 activity["@context"].append(AP_EVENTS_CONTEXT) | 236 activity["@context"].append(AP_EVENTS_CONTEXT) |
237 return activity | 237 return activity |
238 | 238 |
239 async def ap_item_2_event_data(self, ap_item: dict) -> dict: | 239 async def ap_item_2_event_data( |
240 self, | |
241 requestor_actor_id: str, | |
242 ap_item: dict | |
243 ) -> dict: | |
240 """Convert AP activity or object to event data | 244 """Convert AP activity or object to event data |
241 | 245 |
246 @param requestor_actor_id: ID of the actor doing the request. | |
242 @param ap_item: ActivityPub item to convert | 247 @param ap_item: ActivityPub item to convert |
243 Can be either an activity of an object | 248 Can be either an activity of an object |
244 @return: AP Item's Object and event data | 249 @return: AP Item's Object and event data |
245 @raise exceptions.DataError: something is invalid in the AP item | 250 @raise exceptions.DataError: something is invalid in the AP item |
246 """ | 251 """ |
247 is_activity = self.apg.is_activity(ap_item) | 252 is_activity = self.apg.is_activity(ap_item) |
248 if is_activity: | 253 if is_activity: |
249 ap_object = await self.apg.ap_get_object(ap_item, "object") | 254 ap_object = await self.apg.ap_get_object(requestor_actor_id, ap_item, "object") |
250 if not ap_object: | 255 if not ap_object: |
251 log.warning(f'No "object" found in AP item {ap_item!r}') | 256 log.warning(f'No "object" found in AP item {ap_item!r}') |
252 raise exceptions.DataError | 257 raise exceptions.DataError |
253 else: | 258 else: |
254 ap_object = ap_item | 259 ap_object = ap_item |
266 | 271 |
267 if ap_item["type"] != TYPE_EVENT: | 272 if ap_item["type"] != TYPE_EVENT: |
268 raise exceptions.DataError("AP Object is not an event") | 273 raise exceptions.DataError("AP Object is not an event") |
269 | 274 |
270 # author | 275 # author |
271 actor = await self.apg.ap_get_sender_actor(ap_object) | 276 actor = await self.apg.ap_get_sender_actor(requestor_actor_id, ap_object) |
272 | 277 |
273 account = await self.apg.get_ap_account_from_id(actor) | 278 account = await self.apg.get_ap_account_from_id(requestor_actor_id, actor) |
274 author_jid = self.apg.get_local_jid_from_account(account).full() | 279 author_jid = self.apg.get_local_jid_from_account(account).full() |
275 | 280 |
276 # name, start, end | 281 # name, start, end |
277 event_data = { | 282 event_data = { |
278 "id": object_id, | 283 "id": object_id, |
368 }) | 373 }) |
369 | 374 |
370 # comments | 375 # comments |
371 | 376 |
372 if ap_object.get("commentsEnabled"): | 377 if ap_object.get("commentsEnabled"): |
373 __, comments_node = await self.apg.get_comments_nodes(object_id, None) | 378 __, comments_node = await self.apg.get_comments_nodes( |
379 requestor_actor_id, | |
380 object_id, | |
381 None | |
382 ) | |
374 event_data["comments"] = { | 383 event_data["comments"] = { |
375 "service": author_jid, | 384 "service": author_jid, |
376 "node": comments_node, | 385 "node": comments_node, |
377 } | 386 } |
378 | 387 |
389 | 398 |
390 return event_data | 399 return event_data |
391 | 400 |
392 async def ap_item_2_event_data_and_elt( | 401 async def ap_item_2_event_data_and_elt( |
393 self, | 402 self, |
403 requestor_actor_id: str, | |
394 ap_item: dict | 404 ap_item: dict |
395 ) -> Tuple[dict, domish.Element]: | 405 ) -> tuple[dict, domish.Element]: |
396 """Convert AP item to parsed event data and corresponding item element""" | 406 """Convert AP item to parsed event data and corresponding item element""" |
397 event_data = await self.ap_item_2_event_data(ap_item) | 407 event_data = await self.ap_item_2_event_data( |
408 requestor_actor_id, | |
409 ap_item | |
410 ) | |
398 event_elt = self._events.event_data_2_event_elt(event_data) | 411 event_elt = self._events.event_data_2_event_elt(event_data) |
399 item_elt = domish.Element((None, "item")) | 412 item_elt = domish.Element((None, "item")) |
400 item_elt["id"] = event_data["id"] | 413 item_elt["id"] = event_data["id"] |
401 item_elt.addChild(event_elt) | 414 item_elt.addChild(event_elt) |
402 return event_data, item_elt | 415 return event_data, item_elt |
403 | 416 |
404 async def ap_item_2_event_elt(self, ap_item: dict) -> domish.Element: | 417 async def ap_item_2_event_elt( |
418 self, | |
419 requestor_actor_id: str, | |
420 ap_item: dict | |
421 ) -> domish.Element: | |
405 """Convert AP item to XMPP item element""" | 422 """Convert AP item to XMPP item element""" |
406 __, item_elt = await self.ap_item_2_event_data_and_elt(ap_item) | 423 __, item_elt = await self.ap_item_2_event_data_and_elt( |
424 requestor_actor_id, | |
425 ap_item | |
426 ) | |
407 return item_elt | 427 return item_elt |