diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index 77e4d78..15cc1a5 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -1,6 +1,7 @@ use ldap3::{LdapConn, Scope, SearchEntry}; use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config}; use sqlx::{Pool, Sqlite}; +use std::collections::HashMap; use wolves_oxidised::WolvesUser; #[async_std::main] @@ -8,9 +9,13 @@ async fn main() -> tide::Result<()> { let config = get_config(); let db = db_init(&config).await.unwrap(); + dbg!("&config"); update_wolves(&config, &db).await; + dbg!("complete - update_wolves"); update_wolves_id(&db).await; + dbg!("complete - update_wolves_id"); update_ldap(&config, &db).await; + dbg!("complete - update_ldap"); Ok(()) } @@ -46,6 +51,31 @@ async fn update_wolves_id(db: &Pool) { } } +async fn get_account_hashmap(db: &Pool) -> HashMap { + // get all users, toss in hashmap then query for differences? + let mut users_existing = HashMap::new(); + match sqlx::query_as::<_, Accounts>( + r#" + SELECT * + FROM accounts + "#, + ) + .fetch_all(db) + .await + { + Ok(res) => { + for account in &res { + users_existing.insert(account.user.to_owned(), account.to_owned()); + } + } + Err(e) => { + dbg!(e); + } + } + + users_existing +} + async fn update_ldap(config: &Config, db: &Pool) { let mut ldap = match LdapConn::new(&config.ldap_host) { Ok(s) => s, @@ -63,6 +93,8 @@ async fn update_ldap(config: &Config, db: &Pool) { } } + let existing_accounts = get_account_hashmap(db).await; + // use this to pre load a large chunk of data if let Ok(x) = ldap.search( "ou=users,dc=skynet,dc=ie", @@ -101,20 +133,34 @@ async fn update_ldap(config: &Config, db: &Pool) { } if !tmp_account.user.is_empty() { - sqlx::query_as::<_, Accounts>( - " - INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure) - VALUES (?1, ?2, ?3, ?4, ?5) - ", - ) - .bind(&tmp_account.user) - .bind(tmp_account.uid) - .bind(&tmp_account.mail) - .bind(&tmp_account.student_id) - .bind(tmp_account.secure) - .fetch_optional(db) - .await - .ok(); + match existing_accounts.get(&tmp_account.user) { + None => { + update_accounts(db, &tmp_account).await; + } + Some(old) => { + let mut differnt = false; + + if old.mail != tmp_account.mail { + differnt = true; + } + if old.secure != tmp_account.secure { + differnt = true; + } + if old.uid != tmp_account.uid { + differnt = true; + } + if old.student_id != tmp_account.student_id { + differnt = true; + } + if old.id_wolves != tmp_account.id_wolves { + differnt = true; + } + + if differnt { + update_accounts(db, &tmp_account).await; + } + } + } } } } @@ -124,6 +170,23 @@ async fn update_ldap(config: &Config, db: &Pool) { ldap.unbind().unwrap(); } +async fn update_accounts(db: &Pool, tmp_account: &Accounts) { + sqlx::query_as::<_, Accounts>( + " + INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure) + VALUES (?1, ?2, ?3, ?4, ?5) + ", + ) + .bind(&tmp_account.user) + .bind(tmp_account.uid) + .bind(&tmp_account.mail) + .bind(&tmp_account.student_id) + .bind(tmp_account.secure) + .fetch_optional(db) + .await + .ok(); +} + async fn update_account(db: &Pool, account: &WolvesUser) { sqlx::query_as::<_, AccountWolves>( "