diff --git a/src-tauri/src/database.rs b/src-tauri/src/database.rs index 38ccabb3..d4abbea1 100644 --- a/src-tauri/src/database.rs +++ b/src-tauri/src/database.rs @@ -111,6 +111,23 @@ pub(crate) async fn remove_server(pool: &SqlitePool, id: i64) -> DBResult<()> { Ok(()) } +pub(crate) async fn update_server(pool: &SqlitePool, server: entities::Server) -> DBResult<()> { + let mut tx = pool.begin().await?; + + sqlx::query("UPDATE servers SET domain = ?, base_url = ?, sns = ?, favicon = ? WHERE id = ?") + .bind(server.domain) + .bind(server.base_url) + .bind(server.sns) + .bind(server.favicon) + .bind(server.id) + .execute(&mut *tx) + .await?; + + tx.commit().await?; + + Ok(()) +} + pub(crate) async fn add_account( pool: &SqlitePool, server: &entities::Server, diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9dec29a9..613d4bf1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -510,6 +510,23 @@ async fn list_fonts() -> Result, String> { Ok(fonts) } +async fn update_favicon(sqlite_pool: &sqlx::SqlitePool) -> Result<(), String> { + let servers = database::list_servers(sqlite_pool) + .await + .map_err(|e| e.to_string())?; + for (mut server, _) in servers { + let url = server.base_url.clone(); + let icon = favicon::get_favicon_url(&url).await; + tracing::info!("The favicon for {} is {:#?}", &url, icon); + server.favicon = icon; + let _ = database::update_server(sqlite_pool, server) + .await + .map_err(|e| e.to_string())?; + } + + Ok(()) +} + async fn start_timeline_streaming( app_handle: &AppHandle, sqlite_pool: &sqlx::SqlitePool, @@ -726,6 +743,13 @@ pub fn run() -> Result<(), Box> { }); } + { + let sqlite_pool = sqlite_pool.clone(); + tauri::async_runtime::spawn(async move { + let _ = update_favicon(&sqlite_pool).await; + }); + } + app.manage(sqlite_pool); app.manage(Mutex::new(app_handle));