summary refs log tree commit diff
path: root/app/src/commands
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/commands')
-rw-r--r--app/src/commands/accounts.rs84
-rw-r--r--app/src/commands/mod.rs1
2 files changed, 85 insertions, 0 deletions
diff --git a/app/src/commands/accounts.rs b/app/src/commands/accounts.rs
new file mode 100644
index 0000000..ef9c203
--- /dev/null
+++ b/app/src/commands/accounts.rs
@@ -0,0 +1,84 @@
+use tauri::{State, AppHandle};
+use crate::oauth::SigninResult;
+use crate::persistence::Account as PersistedAccount;
+use crate::AppState;
+use tauri_plugin_opener::OpenerExt;
+use ts_rs::TS;
+use serde::Serialize;
+
+#[derive(TS, Serialize)]
+#[ts(export)]
+pub struct Account {
+    username: String,
+    domain: String,
+}
+
+impl From<PersistedAccount> for Account {
+    fn from(value: PersistedAccount) -> Self {
+        Self {
+            username: value.username,
+            domain: value.server_domain,
+        }
+    }
+}
+
+#[tauri::command]
+pub async fn get_all_accounts(state: State<'_, AppState>) -> Result<Vec<Account>, String> {
+    Ok(state.persistence_controller.get_all_accounts().await.into_iter().map(|a| a.into()).collect())
+}
+
+#[tauri::command]
+pub async fn start_account_auth(app_handle: AppHandle, state: State<'_, AppState>, instance_domain: &str) -> Result<Vec<String>, ()> {
+    let add_result = state.oauth_controller.start_authorization(instance_domain).await;
+
+    let state_nonce = match add_result {
+        Ok(result) => {
+            let opener = app_handle.opener();
+            if let Err(_) = opener.open_url(result.auth_url, None::<&str>) {
+                println!("Could not open authentication page");
+                return Err(())
+            }
+
+            result.auth_state
+        }
+        Err(err) => {
+            println!("Error adding server: {err}");
+            return Err(())
+        }
+    };
+
+    let signin_result = state.oauth_controller.finish_authorization(state_nonce).await;
+    match signin_result {
+        Ok(SigninResult {server_domain, username}) => {
+            println!("Signed in successfully @{username}@{server_domain}");
+            Ok(vec!(server_domain, username))
+        }
+        Err(err) => {
+            println!("Error completing signin: {err}");
+            Err(())
+        }
+    }
+}
+
+#[tauri::command]
+pub async fn get_self(state: State<'_, AppState>, server_domain: String, username: String) -> Result<String, String> {
+    let client = reqwest::Client::builder().user_agent("Foxfleet v0.0.1").build().expect("Could not construct client");
+
+    let account = state.persistence_controller.get_account(&username, &server_domain).await
+        .ok_or(format!("No account for @{username}@{server_domain}"))?;
+
+    let api_key = state.persistence_controller.get_credential(account.api_credential_id).await?;
+        
+    if let Ok(result) = client.get("https://social.tempest.dev/api/v1/accounts/verify_credentials")
+        .bearer_auth(api_key)
+        .send().await {
+            if let Ok(result) = result.text().await {
+                return Ok(result)
+            } else {
+                return Err("Error decoding response".to_string());
+            }
+        } else {
+            return Err("Error fetching account".to_string());
+        }
+}
+
diff --git a/app/src/commands/mod.rs b/app/src/commands/mod.rs
new file mode 100644
index 0000000..9bb4894
--- /dev/null
+++ b/app/src/commands/mod.rs
@@ -0,0 +1 @@
+pub mod accounts;