Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Chikage0o0 committed Oct 15, 2023
1 parent 2191c9b commit 9947e96
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 53 deletions.
107 changes: 77 additions & 30 deletions matrix_bot_core/src/matrix/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
);
}
}
}

Expand All @@ -142,16 +139,30 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec<EventHandlerHandle>, 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
);
}
}
},
));

Expand All @@ -168,7 +179,18 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec<EventHandlerHandle>, 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
);
}
}
}
},
));
Expand Down Expand Up @@ -203,16 +225,30 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec<EventHandlerHandle>, 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
);
}
}
}
},
));
Expand All @@ -230,7 +266,18 @@ pub fn sync(client: &Client) -> matrix_sdk::Result<(Vec<EventHandlerHandle>, 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
);
}
}
}
},
));
Expand Down
2 changes: 1 addition & 1 deletion plugins/qbittorrent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static API: OnceLock<Qbit> = OnceLock::new();

#[allow(unused_variables)]
pub async fn run(client: Client, plugin_folder: impl AsRef<std::path::Path>) -> Result<()> {
log::info!("start yande_popular");
log::info!("start qbittorrent plugin");

let setting = Setting::get_or_init(plugin_folder)?;

Expand Down
38 changes: 16 additions & 22 deletions plugins/qbittorrent/src/qbit/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String>) -> Option<&String> {
if let Some(s) = s {
if s.is_empty() {
None
} else {
Some(s)
}
} else {
None
}
}

0 comments on commit 9947e96

Please sign in to comment.