From 62c21523df365850df6a0d28909adc959feb9738 Mon Sep 17 00:00:00 2001
From: yggverse <yggverse@project>
Date: Wed, 20 Nov 2024 19:02:12 +0200
Subject: [PATCH] implement auth remove action

---
 .../window/tab/item/identity/gemini.rs        | 52 ++++++++++++-------
 src/profile/identity/gemini/auth.rs           | 50 ++++++++++--------
 2 files changed, 62 insertions(+), 40 deletions(-)

diff --git a/src/app/browser/window/tab/item/identity/gemini.rs b/src/app/browser/window/tab/item/identity/gemini.rs
index 013888e4..dbfca40f 100644
--- a/src/app/browser/window/tab/item/identity/gemini.rs
+++ b/src/app/browser/window/tab/item/identity/gemini.rs
@@ -82,27 +82,43 @@ impl Gemini {
         widget.on_apply({
             let widget = widget.clone();
             move |response| {
-                // Get record ID depending of user selection
-                let profile_identity_gemini_id = match response {
-                    Value::PROFILE_IDENTITY_GEMINI_ID(value) => value,
-                    Value::USE_GUEST_SESSION => todo!(),
-                    Value::GENERATE_NEW_AUTH => profile
-                        .identity
-                        .gemini
-                        .create(None, widget.form.name.value().as_deref())
-                        .unwrap(), // @TODO
+                // Get option match user choice
+                let option = match response {
+                    Value::PROFILE_IDENTITY_GEMINI_ID(value) => Some(value),
+                    Value::USE_GUEST_SESSION => None,
+                    Value::GENERATE_NEW_AUTH => Some(
+                        profile
+                            .identity
+                            .gemini
+                            .create(None, widget.form.name.value().as_deref())
+                            .unwrap(), // @TODO handle result,
+                    ),
                 };
 
-                // Activate identity for given `auth_uri`
-                match profile
-                    .identity
-                    .gemini
-                    .auth
-                    .activate(profile_identity_gemini_id, auth_url.as_str())
-                {
-                    Ok(_) => action.reload().activate(),
-                    Err(reason) => todo!("{:?}", reason),
+                // Apply
+                match option {
+                    // Activate identity for `auth_uri`
+                    Some(profile_identity_gemini_id) => {
+                        profile
+                            .identity
+                            .gemini
+                            .auth
+                            .add(profile_identity_gemini_id, auth_url.as_str())
+                            .unwrap();
+                    }
+                    // Remove all identity auths for `auth_uri`
+                    None => {
+                        profile
+                            .identity
+                            .gemini
+                            .auth
+                            .remove(auth_url.as_str())
+                            .unwrap();
+                    }
                 }
+
+                // Update page
+                action.reload().activate();
             }
         });
 
diff --git a/src/profile/identity/gemini/auth.rs b/src/profile/identity/gemini/auth.rs
index 4114685e..3e2955f0 100644
--- a/src/profile/identity/gemini/auth.rs
+++ b/src/profile/identity/gemini/auth.rs
@@ -41,38 +41,44 @@ impl Auth {
     /// * deactivate active auth by remove previous records from `Self` database
     /// * reindex `Self` memory index on success
     /// * return last insert `profile_identity_gemini_auth_id` on success
-    pub fn activate(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
-        // Get all records match request
+    pub fn add(&self, profile_identity_gemini_id: i64, url: &str) -> Result<i64, Error> {
+        // Cleanup records match `url` (unauthorize)
+        self.remove(url)?;
+
+        // Create new record (auth)
+        let profile_identity_gemini_auth_id =
+            match self.database.add(profile_identity_gemini_id, url) {
+                Ok(id) => id,
+                Err(reason) => {
+                    return Err(Error::DatabaseRecordCreate(
+                        profile_identity_gemini_id,
+                        url.to_string(),
+                        reason,
+                    ))
+                }
+            };
+
+        // Reindex
+        self.index()?;
+
+        // Done
+        Ok(profile_identity_gemini_auth_id)
+    }
+
+    /// Remove all records match request (unauthorize)
+    pub fn remove(&self, url: &str) -> Result<(), Error> {
         match self.database.records(Some(url)) {
             Ok(records) => {
-                // Cleanup records match `url` (unauth)
                 for record in records {
                     if let Err(reason) = self.database.delete(record.id) {
                         return Err(Error::DatabaseRecordDelete(record.id, reason));
                     }
                 }
-
-                // Create new record (auth)
-                let profile_identity_gemini_auth_id =
-                    match self.database.add(profile_identity_gemini_id, url) {
-                        Ok(id) => id,
-                        Err(reason) => {
-                            return Err(Error::DatabaseRecordCreate(
-                                profile_identity_gemini_id,
-                                url.to_string(),
-                                reason,
-                            ))
-                        }
-                    };
-
-                // Reindex
-                self.index()?;
-
-                // Done
-                Ok(profile_identity_gemini_auth_id)
             }
             Err(reason) => return Err(Error::DatabaseRecordsRead(url.to_string(), reason)),
         }
+        self.index()?;
+        Ok(())
     }
 
     /// Create new `Memory` index from `Database` for `Self`