diff --git a/matrix_bot_core/src/matrix/e2ee.rs b/matrix_bot_core/src/matrix/e2ee.rs index b1b6476..bd3df84 100644 --- a/matrix_bot_core/src/matrix/e2ee.rs +++ b/matrix_bot_core/src/matrix/e2ee.rs @@ -94,7 +94,7 @@ async fn wait_for_confirmation(client: matrix_sdk::Client, sas: SasVerification) log::warn!("msg: {}", msg); if msg == code { println!("Code matches"); - sas.confirm().await.unwrap(); + let _ = sas.confirm().await; if sas.is_done() { print_result(&sas); @@ -120,20 +120,17 @@ fn print_result(sas: &SasVerification) { async fn print_devices(user_id: &UserId, client: &matrix_sdk::Client) { println!("Devices of user {}", user_id); - - for device in client - .encryption() - .get_user_devices(user_id) - .await - .unwrap() - .devices() - { - println!( - " {:<10} {:<30} {:<}", - device.device_id(), - device.display_name().unwrap_or("-"), - device.is_verified() - ); + let clients = client.encryption().get_user_devices(user_id).await; + + if let Ok(clients) = clients { + for device in clients.devices() { + println!( + " {:<10} {:<30} {:<}", + device.device_id(), + device.display_name().unwrap_or("-"), + device.is_verified() + ); + } } } @@ -142,16 +139,30 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec, Joi let mut handlers = Vec::new(); handlers.push(client.add_event_handler( |ev: ToDeviceKeyVerificationRequestEvent, client: matrix_sdk::Client| async move { - let request = client + let request = match client .encryption() .get_verification_request(&ev.sender, &ev.content.transaction_id) .await - .expect("Request object wasn't created"); + { + Some(request) => request, + None => { + log::error!("Can't find request object for {}", &ev.sender); + return; + } + }; - request - .accept() - .await - .expect("Can't accept verification request"); + match request.accept().await { + Ok(_) => { + log::info!("Accepted verification request from {}", &ev.sender); + } + Err(e) => { + log::error!( + "Can't accept verification request from {}: {}", + &ev.sender, + e + ); + } + } }, )); @@ -168,7 +179,18 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec, Joi &sas.other_device().device_id() ); print_devices(&ev.sender, &client).await; - sas.accept().await.unwrap(); + match sas.accept().await { + Ok(_) => { + log::info!("Accepted verification request from {}", &ev.sender); + } + Err(e) => { + log::error!( + "Can't accept verification request from {}: {}", + &ev.sender, + e + ); + } + } } }, )); @@ -203,16 +225,30 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec, Joi handlers.push(client.add_event_handler( |ev: OriginalSyncRoomMessageEvent, client: matrix_sdk::Client| async move { if let MessageType::VerificationRequest(_) = &ev.content.msgtype { - let request = client + let request = match client .encryption() .get_verification_request(&ev.sender, &ev.event_id) .await - .expect("Request object wasn't created"); - - request - .accept() - .await - .expect("Can't accept verification request"); + { + Some(request) => request, + None => { + log::error!("Can't find request object for {}", &ev.sender); + return; + } + }; + + match request.accept().await { + Ok(_) => { + log::info!("Accepted verification request from {}", &ev.sender); + } + Err(e) => { + log::error!( + "Can't accept verification request from {}: {}", + &ev.sender, + e + ); + } + } } }, )); @@ -230,7 +266,18 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec, Joi &sas.other_device().device_id() ); print_devices(&ev.sender, &client).await; - sas.accept().await.unwrap(); + match sas.accept().await { + Ok(_) => { + log::info!("Accepted verification request from {}", &ev.sender); + } + Err(e) => { + log::error!( + "Can't accept verification request from {}: {}", + &ev.sender, + e + ); + } + } } }, )); diff --git a/plugins/qbittorrent/src/lib.rs b/plugins/qbittorrent/src/lib.rs index 7572355..92f7b94 100644 --- a/plugins/qbittorrent/src/lib.rs +++ b/plugins/qbittorrent/src/lib.rs @@ -20,7 +20,7 @@ static API: OnceLock = OnceLock::new(); #[allow(unused_variables)] pub async fn run(client: Client, plugin_folder: impl AsRef) -> Result<()> { - log::info!("start yande_popular"); + log::info!("start qbittorrent plugin"); let setting = Setting::get_or_init(plugin_folder)?; diff --git a/plugins/qbittorrent/src/qbit/ops.rs b/plugins/qbittorrent/src/qbit/ops.rs index 950cf39..ca9b834 100644 --- a/plugins/qbittorrent/src/qbit/ops.rs +++ b/plugins/qbittorrent/src/qbit/ops.rs @@ -37,7 +37,7 @@ pub async fn add_torrent( let xt = url .query_pairs() .find(|(k, _)| k == "xt") - .map(|(_, v)| v.to_string()); + .map(|(_, v)| v.to_string().to_ascii_lowercase()); // 检测是否已经添加过 let torrents = api.get_torrent_list(GetTorrentListArg::default()).await?; @@ -47,7 +47,7 @@ pub async fn add_torrent( let torrent_xt = torrent_url .query_pairs() .find(|(k, _)| k == "xt") - .map(|(_, v)| v.to_string()); + .map(|(_, v)| v.to_string().to_ascii_lowercase()); if xt == torrent_xt { return Err(anyhow::anyhow!("torrent already exists")); } @@ -129,8 +129,8 @@ pub async fn scan_torrent(api: &Qbit) -> Result<(ExpireTorrents, UploadTorrents) pub async fn expire_torrents(api: &Qbit, torrents: &ExpireTorrents) -> Result<()> { for (hash, torrent) in torrents { - let event_id = torrent.tags.as_ref(); - let room_id = torrent.category.as_ref(); + let event_id = extract_non_empty_str(&torrent.tags); + let room_id = extract_non_empty_str(&torrent.category); api.delete_torrents(vec![hash.clone()], true).await?; if room_id.is_some() { @@ -173,14 +173,14 @@ pub async fn expire_torrents(api: &Qbit, torrents: &ExpireTorrents) -> Result<() pub async fn upload_torrents(api: &Qbit, torrents: &UploadTorrents) -> Result<()> { for (hash, torrent) in torrents { log::info!("upload torrent: {}", &hash); - let event_id = torrent.tags.as_ref(); - let room_id = torrent.category.as_ref(); + let event_id = extract_non_empty_str(&torrent.tags); + let room_id = extract_non_empty_str(&torrent.category); let file_path = torrent.content_path.as_ref().unwrap().clone(); let file_path = std::path::Path::new(&file_path); let download_page = upload::gofile::upload(file_path).await?; - if event_id.is_some() && room_id.is_some() { + if room_id.is_some() { let room_id = room_id.unwrap(); let room = ROOM_MAP.get().unwrap().get(room_id); @@ -295,20 +295,14 @@ pub async fn show_status( Ok((msg, html_msg)) } -#[cfg(test)] -mod tests { - - use super::*; - - #[tokio::test] - async fn test_show_status() { - let api = login("admin", "adminadmin", "http://127.0.0.1:8080") - .await - .unwrap(); - let status = api - .get_torrent_list(GetTorrentListArg::default()) - .await - .unwrap(); - println!("{:#?}", status); +fn extract_non_empty_str(s: &Option) -> Option<&String> { + if let Some(s) = s { + if s.is_empty() { + None + } else { + Some(s) + } + } else { + None } }