From 802b95aba7d80899b5321f40d93fd8270d811672 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 21 Aug 2024 13:35:01 -0500 Subject: [PATCH] Add create_matrix_client class method to AuthenticatedController --- fractal/cli/controllers/auth.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/fractal/cli/controllers/auth.py b/fractal/cli/controllers/auth.py index 40dbc19..6fa0879 100644 --- a/fractal/cli/controllers/auth.py +++ b/fractal/cli/controllers/auth.py @@ -9,7 +9,11 @@ from clicz import cli_method from django.db import transaction from fractal.cli.utils import read_user_data, write_user_data -from fractal.matrix import MatrixClient, get_homeserver_for_matrix_id +from fractal.matrix import ( + FractalAsyncClient, + MatrixClient, + get_homeserver_for_matrix_id, +) from fractal.matrix.utils import parse_matrix_id, prompt_matrix_password from fractal_database.utils import is_db_initialized from nio import LoginError, WhoamiError @@ -258,6 +262,19 @@ def get_creds(cls) -> Optional[Tuple[str, str, str]]: return access_token, homeserver_url, matrix_id + @classmethod + def create_matrix_client(cls) -> FractalAsyncClient: + """ + Creates a matrix client with the current user's credentials. + """ + creds = cls.get_creds() + if not creds: + print("You must be logged in to use this command.") + print("Login with fractal login.") + exit(1) + access_token, homeserver_url, matrix_id = creds + return FractalAsyncClient(homeserver_url, access_token) + def check_if_user_is_authenticated(self) -> bool: """ Checks to see if the user is logged in.