diff --git a/DiscordActionBase.py b/DiscordActionBase.py index db9b563..c4c4829 100644 --- a/DiscordActionBase.py +++ b/DiscordActionBase.py @@ -1,5 +1,7 @@ +from loguru import logger as log from gi.repository import Gtk, Adw import gi +import threading from src.backend.PluginManager.ActionBase import ActionBase @@ -14,8 +16,16 @@ def __init__(self, *args, **kwargs): self.client_secret: str = "" def get_config_rows(self) -> list: + authed = self.plugin_base.backend.is_authed() + if not authed: + label = "actions.base.credentials.no-credentials" + css_style = "discord-controller-red" + else: + label = "actions.base.credentials.authenticated" + css_style = "discord-controller-green" + self.status_label = Gtk.Label( - label=self.plugin_base.lm.get("actions.base.no-credentials")) + label=self.plugin_base.lm.get(label), css_classes=[css_style]) self.client_id_row = Adw.EntryRow( title=self.plugin_base.lm.get("actions.base.client_id"), text=self.client_id) self.client_secret_row = Adw.PasswordEntryRow( @@ -35,6 +45,7 @@ def get_config_rows(self) -> list: "actions.base.credentials.title")) group.add(self.client_id_row) group.add(self.client_secret_row) + group.add(self.status_label) group.add(self.auth_button) self.load_config() @@ -67,5 +78,29 @@ def on_auth_clicked(self, _): settings = self.plugin_base.get_settings() client_id = settings.get('client_id') client_secret = settings.get('client_secret') - self.plugin_base.backend.update_client_credentials( - client_id, client_secret) + self.auth_button.set_sensitive(False) + self.plugin_base.auth_callback_fn = self.on_auth_completed + threading.Thread(target=self.plugin_base.backend.update_client_credentials, daemon=True, + name="update_client_credentials", args=[client_id, client_secret]).start() + + def _set_status(self, message: str, is_error: bool = False): + self.status_label.set_label(message) + if is_error: + self.status_label.remove_css_class("discord-controller-green") + self.status_label.add_css_class("discord-controller-red") + else: + self.status_label.remove_css_class("discord-controller-red") + self.status_label.add_css_class("discord-controller-green") + + def on_auth_completed(self, success: bool, message: str = None): + self.auth_button.set_sensitive(True) + if success: + self._set_status(self.plugin_base.lm.get( + "actions.base.credentials.authenticated"), False) + else: + if self.plugin_base.lm.get(message): + message = self.plugin_base.lm.get(message) + elif not message: + message = self.plugin_base.lm.get( + "actions.base.credentials.failed") + self._set_status(message, True) diff --git a/backend.py b/backend.py index 553b700..61d925b 100644 --- a/backend.py +++ b/backend.py @@ -1,4 +1,3 @@ -import uuid import json from streamcontroller_plugin_tools import BackendBase @@ -16,6 +15,7 @@ def __init__(self): self.access_token: str = None self.discord_client: AsyncDiscord = None self.callbacks: dict = {} + self._is_authed: bool = False def discord_callback(self, code, event): if code == 0: @@ -29,6 +29,8 @@ def discord_callback(self, code, event): self.discord_client.authenticate(self.access_token) self.frontend.save_access_token(self.access_token) case commands.AUTHENTICATE: + self.frontend.on_auth_callback(True) + self._is_authed = True for k in self.callbacks: self.discord_client.subscribe(k) case commands.DISPATCH: @@ -45,21 +47,28 @@ def setup_client(self): else: self.discord_client.authenticate(self.access_token) except Exception as ex: + self.frontend.on_auth_callback(False, str(ex)) log.error("failed to setup discord client: {0}", ex) def update_client_credentials(self, client_id: str, client_secret: str, access_token: str = ""): if None in (client_id, client_secret) or "" in (client_id, client_secret): + self.frontend.on_auth_callback( + False, "actions.base.credentials.missing_client_info") return self.client_id = client_id self.client_secret = client_secret self.access_token = access_token self.setup_client() + def is_authed(self) -> bool: + return self._is_authed + def register_callback(self, key: str, callback: callable): callbacks = self.callbacks.get(key, []) callbacks.append(callback) self.callbacks[key] = callbacks - self.discord_client.subscribe(key) + if self._is_authed: + self.discord_client.subscribe(key) def set_mute(self, muted: bool) -> bool: if self.discord_client is None or not self.discord_client.is_connected(): diff --git a/locales/en_US.json b/locales/en_US.json index a333d59..b13b6a0 100644 --- a/locales/en_US.json +++ b/locales/en_US.json @@ -1,6 +1,6 @@ { "plugin.name": "Discord", - "actions.base.no-credentials": "Missing Credentials", + "actions.base.credentials.no-credentials": "Missing Credentials", "actions.base.client_id": "Client ID", "actions.base.client_secret": "Client Secret", "actions.base.validate": "Validate", @@ -8,5 +8,8 @@ "actions.info.link.label": "Checkout how to configure this plugin on", "actions.info.link.text": "GitHub", "actions.mute.choice.title": "Button Press Action", - "actions.mute.label_choice.title": "Display Location" + "actions.mute.label_choice.title": "Display Location", + "actions.base.credentials.authenticated": "Authenticated successfully", + "actions.base.credentials.failed": "Failed to authenticate", + "actions.base.credentials.missing_client_info": "Missing Client ID or Client Secret" } diff --git a/main.py b/main.py index ab918e3..e574fb8 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,8 @@ def __init__(self): self.callbacks = {} + self.auth_callback_fn: callable = None + self.lm = self.locale_manager self.lm.set_to_os_default() @@ -30,7 +32,7 @@ def __init__(self): backend_path = os.path.join(self.PATH, 'backend.py') self.launch_backend(backend_path=backend_path, open_in_terminal=False, venv_path=os.path.join(self.PATH, '.venv')) - self.wait_for_backend(10) + self.backend.update_client_credentials( client_id, client_secret, access_token) @@ -88,3 +90,7 @@ def add_callback(self, key: str, callback: callable): def handle_callback(self, key: str, data: any): for callback in self.callbacks.get(key): callback(data) + + def on_auth_callback(self, success: bool, message: str = None): + if self.auth_callback_fn: + self.auth_callback_fn(success, message) diff --git a/manifest.json b/manifest.json index 395c66c..ecb5b89 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { - "version": "1.0.1", - "thumbnail": "store/thumbnail.jpg", + "version": "1.0.3", + "thumbnail": "store/thumbnail.png", "id": "com_imdevinc_StreamControllerDiscordPlugin", "name": "Discord", "author": "ImDevinC", diff --git a/store/thumbnail.jpg b/store/thumbnail.jpg deleted file mode 100644 index 72c8229..0000000 Binary files a/store/thumbnail.jpg and /dev/null differ diff --git a/store/thumbnail.png b/store/thumbnail.png new file mode 100644 index 0000000..0d13675 Binary files /dev/null and b/store/thumbnail.png differ diff --git a/style.css b/style.css index e69de29..aeb23da 100644 --- a/style.css +++ b/style.css @@ -0,0 +1,7 @@ +.discord-controller-green { + color: green; +} + +.discord-controller-red { + color: red; +}