From b69cbf9d146fe64b749f4ab6e778ce9e5ff11ba3 Mon Sep 17 00:00:00 2001 From: vslg Date: Thu, 16 Jul 2020 22:19:20 -0300 Subject: [PATCH] Set users to offline before exiting Mirage --- src/backend/backend.py | 10 ++++++++++ src/backend/qml_bridge.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/backend/backend.py b/src/backend/backend.py index c8f187d2..c07a680e 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -198,6 +198,16 @@ class Backend: )) + async def logoff_all(self) -> None: + """Stop syncing and end all clients registered.""" + + await asyncio.gather(*[ + client.set_presence("offline", save=False) + for client in self.clients.values() + if client._presence != "offline" + ]) + + async def logout_client(self, user_id: str) -> None: """Log a `MatrixClient` out and unregister it from our models.""" diff --git a/src/backend/qml_bridge.py b/src/backend/qml_bridge.py index caa439df..04ef6ead 100644 --- a/src/backend/qml_bridge.py +++ b/src/backend/qml_bridge.py @@ -16,6 +16,7 @@ class. import asyncio import logging as log import os +import pyotherside import signal import traceback from concurrent.futures import Future @@ -143,6 +144,19 @@ class QMLBridge: remote_pdb.RemotePdb("127.0.0.1", 4444).set_trace() + def exit_mirage(self) -> None: + """Used to end some tasks before closing""" + + try: + # Set accounts presence to offline + asyncio.run_coroutine_threadsafe( + self.backend.logoff_all(), + self._loop, + ).result() + except Exception as e: + print(e) + + # The AppImage AppRun script overwrites some environment path variables to # correctly work, and sets RESTORE_ equivalents with the original values. # If the app is launched from an AppImage, now restore the original values @@ -162,3 +176,5 @@ except ValueError: pass BRIDGE = QMLBridge() + +pyotherside.atexit(BRIDGE.exit_mirage)