Mercurial > libervia-backend
comparison sat/core/sat_main.py @ 4041:2594e1951cf7
core (bridge): `action_new` now use serialised dict for extra data.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 15 May 2023 16:20:45 +0200 |
parents | 524856bd7b19 |
children | 9641ce286e07 |
comparison
equal
deleted
inserted
replaced
4040:1f967f85fc23 | 4041:2594e1951cf7 |
---|---|
34 from twisted.words.protocols.jabber import jid | 34 from twisted.words.protocols.jabber import jid |
35 from twisted.internet import reactor | 35 from twisted.internet import reactor |
36 from wokkel.xmppim import RosterItem | 36 from wokkel.xmppim import RosterItem |
37 from sat.core import xmpp | 37 from sat.core import xmpp |
38 from sat.core import exceptions | 38 from sat.core import exceptions |
39 from sat.core.core_types import SatXMPPEntity | |
39 from sat.core.log import getLogger | 40 from sat.core.log import getLogger |
40 | 41 |
41 from sat.core.constants import Const as C | 42 from sat.core.constants import Const as C |
42 from sat.memory import memory | 43 from sat.memory import memory |
43 from sat.memory import cache | 44 from sat.memory import cache |
199 self.bridge.register_method("contact_add", self._add_contact) | 200 self.bridge.register_method("contact_add", self._add_contact) |
200 self.bridge.register_method("contact_update", self._update_contact) | 201 self.bridge.register_method("contact_update", self._update_contact) |
201 self.bridge.register_method("contact_del", self._del_contact) | 202 self.bridge.register_method("contact_del", self._del_contact) |
202 self.bridge.register_method("roster_resync", self._roster_resync) | 203 self.bridge.register_method("roster_resync", self._roster_resync) |
203 self.bridge.register_method("is_connected", self.is_connected) | 204 self.bridge.register_method("is_connected", self.is_connected) |
204 self.bridge.register_method("action_launch", self.launch_callback) | 205 self.bridge.register_method("action_launch", self._action_launch) |
205 self.bridge.register_method("actions_get", self.actions_get) | 206 self.bridge.register_method("actions_get", self.actions_get) |
206 self.bridge.register_method("progress_get", self._progress_get) | 207 self.bridge.register_method("progress_get", self._progress_get) |
207 self.bridge.register_method("progress_get_all", self._progress_get_all) | 208 self.bridge.register_method("progress_get_all", self._progress_get_all) |
208 self.bridge.register_method("menus_get", self.get_menus) | 209 self.bridge.register_method("menus_get", self.get_menus) |
209 self.bridge.register_method("menu_help_get", self.get_menu_help) | 210 self.bridge.register_method("menu_help_get", self.get_menu_help) |
883 | 884 |
884 # local dirs | 885 # local dirs |
885 | 886 |
886 def get_local_path( | 887 def get_local_path( |
887 self, | 888 self, |
888 client: Optional[xmpp.SatXMPPEntity], | 889 client: Optional[SatXMPPEntity], |
889 dir_name: str, | 890 dir_name: str, |
890 *extra_path: str, | 891 *extra_path: str, |
891 component: bool = False, | 892 component: bool = False, |
892 ) -> Path: | 893 ) -> Path: |
893 """Retrieve path for local data | 894 """Retrieve path for local data |
1117 client, namespaces, identities, bare_jids, service, roster, own_jid, | 1118 client, namespaces, identities, bare_jids, service, roster, own_jid, |
1118 local_device)) | 1119 local_device)) |
1119 | 1120 |
1120 async def find_by_features( | 1121 async def find_by_features( |
1121 self, | 1122 self, |
1122 client: xmpp.SatXMPPEntity, | 1123 client: SatXMPPEntity, |
1123 namespaces: List[str], | 1124 namespaces: List[str], |
1124 identities: Optional[List[Tuple[str, str]]]=None, | 1125 identities: Optional[List[Tuple[str, str]]]=None, |
1125 bare_jids: bool=False, | 1126 bare_jids: bool=False, |
1126 service: bool=True, | 1127 service: bool=True, |
1127 roster: bool=True, | 1128 roster: bool=True, |
1275 if keep_id is not None: | 1276 if keep_id is not None: |
1276 client = self.get_client(profile) | 1277 client = self.get_client(profile) |
1277 action_timer = reactor.callLater(60 * 30, self._kill_action, keep_id, client) | 1278 action_timer = reactor.callLater(60 * 30, self._kill_action, keep_id, client) |
1278 client.actions[keep_id] = (action_data, id_, security_limit, action_timer) | 1279 client.actions[keep_id] = (action_data, id_, security_limit, action_timer) |
1279 | 1280 |
1280 self.bridge.action_new(action_data, id_, security_limit, profile) | 1281 self.bridge.action_new( |
1282 data_format.serialise(action_data), id_, security_limit, profile | |
1283 ) | |
1281 | 1284 |
1282 def actions_get(self, profile): | 1285 def actions_get(self, profile): |
1283 """Return current non answered actions | 1286 """Return current non answered actions |
1284 | 1287 |
1285 @param profile: %(doc_profile)s | 1288 @param profile: %(doc_profile)s |
1286 """ | 1289 """ |
1287 client = self.get_client(profile) | 1290 client = self.get_client(profile) |
1288 return [action_tuple[:-1] for action_tuple in client.actions.values()] | 1291 return [ |
1292 (data_format.serialise(action_tuple[0]), *action_tuple[1:-1]) | |
1293 for action_tuple in client.actions.values() | |
1294 ] | |
1289 | 1295 |
1290 def register_progress_cb( | 1296 def register_progress_cb( |
1291 self, progress_id, callback, metadata=None, profile=C.PROF_KEY_NONE | 1297 self, progress_id, callback, metadata=None, profile=C.PROF_KEY_NONE |
1292 ): | 1298 ): |
1293 """Register a callback called when progress is requested for id""" | 1299 """Register a callback called when progress is requested for id""" |
1399 | 1405 |
1400 if "one_shot" in kwargs: # One Shot callback are removed after 30 min | 1406 if "one_shot" in kwargs: # One Shot callback are removed after 30 min |
1401 | 1407 |
1402 def purge_callback(): | 1408 def purge_callback(): |
1403 try: | 1409 try: |
1404 self.removeCallback(callback_id) | 1410 self.remove_callback(callback_id) |
1405 except KeyError: | 1411 except KeyError: |
1406 pass | 1412 pass |
1407 | 1413 |
1408 reactor.callLater(1800, purge_callback) | 1414 reactor.callLater(1800, purge_callback) |
1409 | 1415 |
1410 return callback_id | 1416 return callback_id |
1411 | 1417 |
1412 def removeCallback(self, callback_id): | 1418 def remove_callback(self, callback_id): |
1413 """ Remove a previously registered callback | 1419 """ Remove a previously registered callback |
1414 @param callback_id: id returned by [register_callback] """ | 1420 @param callback_id: id returned by [register_callback] """ |
1415 log.debug("Removing callback [%s]" % callback_id) | 1421 log.debug("Removing callback [%s]" % callback_id) |
1416 del self._cb_map[callback_id] | 1422 del self._cb_map[callback_id] |
1417 | 1423 |
1418 def launch_callback(self, callback_id, data=None, profile_key=C.PROF_KEY_NONE): | 1424 def _action_launch( |
1425 self, | |
1426 callback_id: str, | |
1427 data_s: str, | |
1428 profile_key: str | |
1429 ) -> defer.Deferred: | |
1430 d = self.launch_callback( | |
1431 callback_id, | |
1432 data_format.deserialise(data_s), | |
1433 profile_key | |
1434 ) | |
1435 d.addCallback(data_format.serialise) | |
1436 return d | |
1437 | |
1438 def launch_callback( | |
1439 self, | |
1440 callback_id: str, | |
1441 data: Optional[dict] = None, | |
1442 profile_key: str = C.PROF_KEY_NONE | |
1443 ) -> defer.Deferred: | |
1419 """Launch a specific callback | 1444 """Launch a specific callback |
1420 | 1445 |
1421 @param callback_id: id of the action (callback) to launch | 1446 @param callback_id: id of the action (callback) to launch |
1422 @param data: optional data | 1447 @param data: optional data |
1423 @profile_key: %(doc_profile_key)s | 1448 @profile_key: %(doc_profile_key)s |
1426 - validated: if present, can be used to launch a callback, it can have the | 1451 - validated: if present, can be used to launch a callback, it can have the |
1427 values | 1452 values |
1428 - C.BOOL_TRUE | 1453 - C.BOOL_TRUE |
1429 - C.BOOL_FALSE | 1454 - C.BOOL_FALSE |
1430 """ | 1455 """ |
1431 # Â FIXME: security limit need to be checked here | 1456 # FIXME: is it possible to use this method without profile connected? If not, |
1457 # client must be used instead of profile_key | |
1458 # FIXME: security limit need to be checked here | |
1432 try: | 1459 try: |
1433 client = self.get_client(profile_key) | 1460 client = self.get_client(profile_key) |
1434 except exceptions.NotFound: | 1461 except exceptions.NotFound: |
1435 # client is not available yet | 1462 # client is not available yet |
1436 profile = self.memory.get_profile_name(profile_key) | 1463 profile = self.memory.get_profile_name(profile_key) |
1464 args.insert(0, data) | 1491 args.insert(0, data) |
1465 kwargs["profile"] = profile | 1492 kwargs["profile"] = profile |
1466 del kwargs["with_data"] | 1493 del kwargs["with_data"] |
1467 | 1494 |
1468 if kwargs.pop("one_shot", False): | 1495 if kwargs.pop("one_shot", False): |
1469 self.removeCallback(callback_id) | 1496 self.remove_callback(callback_id) |
1470 | 1497 |
1471 return utils.as_deferred(callback, *args, **kwargs) | 1498 return utils.as_deferred(callback, *args, **kwargs) |
1472 | 1499 |
1473 # Menus management | 1500 # Menus management |
1474 | 1501 |