A decentralized music tracking and discovery platform built on AT Protocol 🎵
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: refactor user profile retrieval in save_scrobble to use thread for async handling

+28 -13
+28 -13
crates/feed/src/repo/duckdb/scrobble.rs
··· 1 - use std::sync::Arc; 1 + use std::{sync::Arc, thread}; 2 2 3 3 use anyhow::Error; 4 4 use duckdb::{params, OptionalExt}; 5 5 use owo_colors::OwoColorize; 6 6 use std::sync::Mutex; 7 7 8 - use crate::{did::did_to_profile, types::ScrobbleRecord}; 8 + use crate::{ 9 + did::did_to_profile, 10 + types::{Profile, ScrobbleRecord}, 11 + }; 9 12 10 13 pub fn save_scrobble( 11 14 conn: Arc<Mutex<duckdb::Connection>>, ··· 23 26 let mut user = conn.prepare("SELECT id FROM users WHERE did = ?")?; 24 27 let user_id: Option<String> = user.query_row(params![did], |row| row.get(0)).optional()?; 25 28 29 + let did_clone = did.clone(); 26 30 if user_id.is_none() { 27 - let rt = tokio::runtime::Runtime::new()?; 28 - let profile = rt.block_on(did_to_profile(&did))?; 31 + let handle = thread::spawn(move || { 32 + let rt = tokio::runtime::Runtime::new()?; 33 + let profile = rt.block_on(did_to_profile(&did_clone))?; 29 34 30 - let avatar = profile.avatar.map(|blob| { 31 - format!( 32 - "https://cdn.bsky.app/img/avatar/plain/{}/{}@{}", 33 - did, 34 - blob.r#ref.link, 35 - blob.mime_type.split('/').last().unwrap_or("jpeg") 36 - ) 35 + let avatar = profile.avatar.map(|blob| { 36 + format!( 37 + "https://cdn.bsky.app/img/avatar/plain/{}/{}@{}", 38 + did_clone, 39 + blob.r#ref.link, 40 + blob.mime_type.split('/').last().unwrap_or("jpeg") 41 + ) 42 + }); 43 + Ok::<(Option<String>, Option<String>, Option<String>), Error>(( 44 + profile.display_name, 45 + profile.handle, 46 + avatar, 47 + )) 37 48 }); 49 + 50 + let (display_name, handle, avatar) = handle 51 + .join() 52 + .map_err(|e| anyhow::anyhow!("Thread panicked: {:?}", e))??; 38 53 39 54 conn.execute( 40 55 "INSERT OR IGNORE INTO users ( ··· 51 66 ?)", 52 67 params![ 53 68 xid::new().to_string(), 54 - profile.display_name.unwrap_or_default(), 69 + display_name.unwrap_or_default(), 55 70 did, 56 - profile.handle.unwrap_or_default(), 71 + handle.unwrap_or_default(), 57 72 avatar, 58 73 ], 59 74 )?;