Mercurial > libervia-backend
comparison sat/plugins/plugin_exp_events.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 | be6d91572633 |
children | 09f5ac48ffe3 |
comparison
equal
deleted
inserted
replaced
3583:16ade4ad63f3 | 3584:edc79cefe968 |
---|---|
210 data["item"] = link_elt["item"] | 210 data["item"] = link_elt["item"] |
211 if event_elt.getAttribute("creator") == "true": | 211 if event_elt.getAttribute("creator") == "true": |
212 data["creator"] = True | 212 data["creator"] = True |
213 return timestamp, data | 213 return timestamp, data |
214 | 214 |
215 @defer.inlineCallbacks | 215 async def getEventElement(self, client, service, node, id_): |
216 def getEventElement(self, client, service, node, id_): | |
217 """Retrieve event element | 216 """Retrieve event element |
218 | 217 |
219 @param service(jid.JID): pubsub service | 218 @param service(jid.JID): pubsub service |
220 @param node(unicode): pubsub node | 219 @param node(unicode): pubsub node |
221 @param id_(unicode, None): event id | 220 @param id_(unicode, None): event id |
222 @return (domish.Element): event element | 221 @return (domish.Element): event element |
223 @raise exceptions.NotFound: no event element found | 222 @raise exceptions.NotFound: no event element found |
224 """ | 223 """ |
225 if not id_: | 224 if not id_: |
226 id_ = NS_EVENT | 225 id_ = NS_EVENT |
227 items, metadata = yield self._p.getItems(client, service, node, item_ids=[id_]) | 226 items, metadata = await self._p.getItems(client, service, node, item_ids=[id_]) |
228 try: | 227 try: |
229 event_elt = next(items[0].elements(NS_EVENT, "event")) | 228 event_elt = next(items[0].elements(NS_EVENT, "event")) |
230 except StopIteration: | 229 except StopIteration: |
231 raise exceptions.NotFound(_("No event element has been found")) | 230 raise exceptions.NotFound(_("No event element has been found")) |
232 except IndexError: | 231 except IndexError: |
233 raise exceptions.NotFound(_("No event with this id has been found")) | 232 raise exceptions.NotFound(_("No event with this id has been found")) |
234 defer.returnValue(event_elt) | 233 return event_elt |
235 | 234 |
236 def _eventGet(self, service, node, id_="", profile_key=C.PROF_KEY_NONE): | 235 def _eventGet(self, service, node, id_="", profile_key=C.PROF_KEY_NONE): |
237 service = jid.JID(service) if service else None | 236 service = jid.JID(service) if service else None |
238 node = node if node else NS_EVENT | 237 node = node if node else NS_EVENT |
239 client = self.host.getClient(profile_key) | 238 client = self.host.getClient(profile_key) |
240 return self.eventGet(client, service, node, id_) | 239 return defer.ensureDeferred( |
241 | 240 self.eventGet(client, service, node, id_) |
242 @defer.inlineCallbacks | 241 ) |
243 def eventGet(self, client, service, node, id_=NS_EVENT): | 242 |
243 async def eventGet(self, client, service, node, id_=NS_EVENT): | |
244 """Retrieve event data | 244 """Retrieve event data |
245 | 245 |
246 @param service(unicode, None): PubSub service | 246 @param service(unicode, None): PubSub service |
247 @param node(unicode): PubSub node of the event | 247 @param node(unicode): PubSub node of the event |
248 @param id_(unicode): id_ with even data | 248 @param id_(unicode): id_ with even data |
251 - event metadata where key can be: | 251 - event metadata where key can be: |
252 location: location of the event | 252 location: location of the event |
253 image: URL of a picture to use to represent event | 253 image: URL of a picture to use to represent event |
254 background-image: URL of a picture to use in background | 254 background-image: URL of a picture to use in background |
255 """ | 255 """ |
256 event_elt = yield self.getEventElement(client, service, node, id_) | 256 event_elt = await self.getEventElement(client, service, node, id_) |
257 | 257 |
258 defer.returnValue(self._parseEventElt(event_elt)) | 258 return self._parseEventElt(event_elt) |
259 | 259 |
260 def _eventCreate( | 260 def _eventCreate( |
261 self, timestamp, data, service, node, id_="", profile_key=C.PROF_KEY_NONE | 261 self, timestamp, data, service, node, id_="", profile_key=C.PROF_KEY_NONE |
262 ): | 262 ): |
263 service = jid.JID(service) if service else None | 263 service = jid.JID(service) if service else None |
434 def _eventInviteeGet(self, service, node, invitee_jid_s, profile_key): | 434 def _eventInviteeGet(self, service, node, invitee_jid_s, profile_key): |
435 service = jid.JID(service) if service else None | 435 service = jid.JID(service) if service else None |
436 node = node if node else NS_EVENT | 436 node = node if node else NS_EVENT |
437 client = self.host.getClient(profile_key) | 437 client = self.host.getClient(profile_key) |
438 invitee_jid = jid.JID(invitee_jid_s) if invitee_jid_s else None | 438 invitee_jid = jid.JID(invitee_jid_s) if invitee_jid_s else None |
439 return self.eventInviteeGet(client, service, node, invitee_jid) | 439 return defer.ensureDeferred( |
440 | 440 self.eventInviteeGet(client, service, node, invitee_jid) |
441 @defer.inlineCallbacks | 441 ) |
442 def eventInviteeGet(self, client, service, node, invitee_jid=None): | 442 |
443 async def eventInviteeGet(self, client, service, node, invitee_jid=None): | |
443 """Retrieve attendance from event node | 444 """Retrieve attendance from event node |
444 | 445 |
445 @param service(unicode, None): PubSub service | 446 @param service(unicode, None): PubSub service |
446 @param node(unicode): PubSub node of the event's invitees | 447 @param node(unicode): PubSub node of the event's invitees |
447 @param invitee_jid(jid.JId, None): jid of the invitee to retrieve (None to | 448 @param invitee_jid(jid.JId, None): jid of the invitee to retrieve (None to |
450 an empty dict is returned if nothing has been answered yed | 451 an empty dict is returned if nothing has been answered yed |
451 """ | 452 """ |
452 if invitee_jid is None: | 453 if invitee_jid is None: |
453 invitee_jid = client.jid | 454 invitee_jid = client.jid |
454 try: | 455 try: |
455 items, metadata = yield self._p.getItems( | 456 items, metadata = await self._p.getItems( |
456 client, service, node, item_ids=[invitee_jid.userhost()] | 457 client, service, node, item_ids=[invitee_jid.userhost()] |
457 ) | 458 ) |
458 event_elt = next(items[0].elements(NS_EVENT, "invitee")) | 459 event_elt = next(items[0].elements(NS_EVENT, "invitee")) |
459 except (exceptions.NotFound, IndexError): | 460 except (exceptions.NotFound, IndexError): |
460 # no item found, event data are not set yet | 461 # no item found, event data are not set yet |
461 defer.returnValue({}) | 462 return {} |
462 data = {} | 463 data = {} |
463 for key in ("attend", "guests"): | 464 for key in ("attend", "guests"): |
464 try: | 465 try: |
465 data[key] = event_elt[key] | 466 data[key] = event_elt[key] |
466 except KeyError: | 467 except KeyError: |
467 continue | 468 continue |
468 defer.returnValue(data) | 469 return data |
469 | 470 |
470 def _eventInviteeSet(self, service, node, event_data, profile_key): | 471 def _eventInviteeSet(self, service, node, event_data, profile_key): |
471 service = jid.JID(service) if service else None | 472 service = jid.JID(service) if service else None |
472 node = node if node else NS_EVENT | 473 node = node if node else NS_EVENT |
473 client = self.host.getClient(profile_key) | 474 client = self.host.getClient(profile_key) |
474 return self.eventInviteeSet(client, service, node, event_data) | 475 return defer.ensureDeferred( |
475 | 476 self.eventInviteeSet(client, service, node, event_data) |
476 def eventInviteeSet(self, client, service, node, data): | 477 ) |
478 | |
479 async def eventInviteeSet(self, client, service, node, data): | |
477 """Set or update attendance data in event node | 480 """Set or update attendance data in event node |
478 | 481 |
479 @param service(unicode, None): PubSub service | 482 @param service(unicode, None): PubSub service |
480 @param node(unicode): PubSub node of the event | 483 @param node(unicode): PubSub node of the event |
481 @param data(dict[unicode, unicode]): data to update | 484 @param data(dict[unicode, unicode]): data to update |
488 try: | 491 try: |
489 event_elt[key] = data.pop(key) | 492 event_elt[key] = data.pop(key) |
490 except KeyError: | 493 except KeyError: |
491 pass | 494 pass |
492 item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt) | 495 item_elt = pubsub.Item(id=client.jid.userhost(), payload=event_elt) |
493 return self._p.publish(client, service, node, items=[item_elt]) | 496 return await self._p.publish(client, service, node, items=[item_elt]) |
494 | 497 |
495 def _eventInviteesList(self, service, node, profile_key): | 498 def _eventInviteesList(self, service, node, profile_key): |
496 service = jid.JID(service) if service else None | 499 service = jid.JID(service) if service else None |
497 node = node if node else NS_EVENT | 500 node = node if node else NS_EVENT |
498 client = self.host.getClient(profile_key) | 501 client = self.host.getClient(profile_key) |
499 return self.eventInviteesList(client, service, node) | 502 return defer.ensureDeferred( |
500 | 503 self.eventInviteesList(client, service, node) |
501 @defer.inlineCallbacks | 504 ) |
502 def eventInviteesList(self, client, service, node): | 505 |
506 async def eventInviteesList(self, client, service, node): | |
503 """Retrieve attendance from event node | 507 """Retrieve attendance from event node |
504 | 508 |
505 @param service(unicode, None): PubSub service | 509 @param service(unicode, None): PubSub service |
506 @param node(unicode): PubSub node of the event | 510 @param node(unicode): PubSub node of the event |
507 @return (dict): a dict with current attendance status, | 511 @return (dict): a dict with current attendance status, |
508 an empty dict is returned if nothing has been answered yed | 512 an empty dict is returned if nothing has been answered yed |
509 """ | 513 """ |
510 items, metadata = yield self._p.getItems(client, service, node) | 514 items, metadata = await self._p.getItems(client, service, node) |
511 invitees = {} | 515 invitees = {} |
512 for item in items: | 516 for item in items: |
513 try: | 517 try: |
514 event_elt = next(item.elements(NS_EVENT, "invitee")) | 518 event_elt = next(item.elements(NS_EVENT, "invitee")) |
515 except StopIteration: | 519 except StopIteration: |
523 try: | 527 try: |
524 data[key] = event_elt[key] | 528 data[key] = event_elt[key] |
525 except KeyError: | 529 except KeyError: |
526 continue | 530 continue |
527 invitees[item["id"]] = data | 531 invitees[item["id"]] = data |
528 defer.returnValue(invitees) | 532 return invitees |
529 | 533 |
530 async def invitePreflight( | 534 async def invitePreflight( |
531 self, | 535 self, |
532 client: SatXMPPEntity, | 536 client: SatXMPPEntity, |
533 invitee_jid: jid.JID, | 537 invitee_jid: jid.JID, |