Commit 5b89750b7f
Unsigned
Layout: unified · split
src/commands/media.rs +16 −5
| @@ -7,7 +7,7 @@ use clap::{ | |||
| 7 | use matrix_sdk::ruma::MxcUri; | 7 | use matrix_sdk::ruma::MxcUri; |
| 8 | use weechat::{buffer::Buffer, Weechat}; | 8 | use weechat::{buffer::Buffer, Weechat}; |
| 9 | 9 | ||
| 10 | use crate::Servers; | 10 | use crate::{BufferOwner, Servers}; |
| 11 | 11 | ||
| 12 | pub struct MediaCommand; | 12 | pub struct MediaCommand; |
| 13 | 13 | ||
| @@ -16,9 +16,18 @@ impl MediaCommand { | |||
| 16 | pub const COMPLETION: &'static str = "download %(matrix-media)"; | 16 | pub const COMPLETION: &'static str = "download %(matrix-media)"; |
| 17 | 17 | ||
| 18 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { | 18 | pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) { |
| 19 | let server = match servers.find_server(buffer) { | 19 | let (server, output_buffer) = match servers.buffer_owner(buffer) { |
| 20 | Some(server) => server, | 20 | BufferOwner::Room(server, room) => { |
| 21 | None => { | 21 | (server, Some(room.buffer_handle())) |
| 22 | } | ||
| 23 | BufferOwner::Server(server) => { | ||
| 24 | let output_buffer = server.server_buffer().as_ref().cloned(); | ||
| 25 | (server, output_buffer) | ||
| 26 | } | ||
| 27 | BufferOwner::Verification(server, verification) => { | ||
| 28 | (server, Some(verification.buffer())) | ||
| 29 | } | ||
| 30 | BufferOwner::None => { | ||
| 22 | Weechat::print("Must be executed on Matrix buffer"); | 31 | Weechat::print("Must be executed on Matrix buffer"); |
| 23 | return; | 32 | return; |
| 24 | } | 33 | } |
| @@ -51,7 +60,9 @@ impl MediaCommand { | |||
| 51 | }; | 60 | }; |
| 52 | 61 | ||
| 53 | Weechat::spawn(async move { | 62 | Weechat::spawn(async move { |
| 54 | server.download_media(uri.into(), file).await; | 63 | server |
| 64 | .download_media(uri.into(), file, output_buffer) | ||
| 65 | .await; | ||
| 55 | }) | 66 | }) |
| 56 | .detach(); | 67 | .detach(); |
| 57 | } | 68 | } |
src/server.rs +95 −14
| @@ -60,7 +60,7 @@ use std::{ | |||
| 60 | cmp::Reverse, | 60 | cmp::Reverse, |
| 61 | collections::HashMap, | 61 | collections::HashMap, |
| 62 | io::Write, | 62 | io::Write, |
| 63 | path::PathBuf, | 63 | path::{Path, PathBuf}, |
| 64 | rc::{Rc, Weak}, | 64 | rc::{Rc, Weak}, |
| 65 | }; | 65 | }; |
| 66 | use tracing::error; | 66 | use tracing::error; |
| @@ -669,6 +669,21 @@ impl InnerServer { | |||
| 669 | self.print(&format!("{}{}: {}", prefix, PLUGIN_NAME, message)); | 669 | self.print(&format!("{}{}: {}", prefix, PLUGIN_NAME, message)); |
| 670 | } | 670 | } |
| 671 | 671 | ||
| 672 | /// Print a message to a Matrix buffer, falling back to the server buffer if | ||
| 673 | /// the original command buffer disappeared. | ||
| 674 | fn print_with_prefix_to( | ||
| 675 | &self, | ||
| 676 | buffer: Option<&BufferHandle>, | ||
| 677 | prefix: &str, | ||
| 678 | message: &str, | ||
| 679 | ) { | ||
| 680 | if let Some(Ok(buffer)) = buffer.map(|buffer| buffer.upgrade()) { | ||
| 681 | buffer.print(&format!("{}{}: {}", prefix, PLUGIN_NAME, message)); | ||
| 682 | } else { | ||
| 683 | self.print_with_prefix(prefix, message); | ||
| 684 | } | ||
| 685 | } | ||
| 686 | |||
| 672 | /// Print an network message to the server buffer. | 687 | /// Print an network message to the server buffer. |
| 673 | pub fn print_network(&self, message: &str) { | 688 | pub fn print_network(&self, message: &str) { |
| 674 | self.print_with_prefix(&Weechat::prefix(Prefix::Network), message); | 689 | self.print_with_prefix(&Weechat::prefix(Prefix::Network), message); |
| @@ -1020,11 +1035,20 @@ impl InnerServer { | |||
| 1020 | }; | 1035 | }; |
| 1021 | } | 1036 | } |
| 1022 | 1037 | ||
| 1023 | pub async fn download_media(&self, uri: OwnedMxcUri, file: PathBuf) { | 1038 | pub async fn download_media( |
| 1039 | &self, | ||
| 1040 | uri: OwnedMxcUri, | ||
| 1041 | file: PathBuf, | ||
| 1042 | output_buffer: Option<BufferHandle>, | ||
| 1043 | ) { | ||
| 1024 | let connection = if let Some(c) = self.connection() { | 1044 | let connection = if let Some(c) = self.connection() { |
| 1025 | c | 1045 | c |
| 1026 | } else { | 1046 | } else { |
| 1027 | self.print_error("You must be connected to execute this command"); | 1047 | self.print_with_prefix_to( |
| 1048 | output_buffer.as_ref(), | ||
| 1049 | &Weechat::prefix(Prefix::Error), | ||
| 1050 | "You must be connected to execute this command", | ||
| 1051 | ); | ||
| 1028 | return; | 1052 | return; |
| 1029 | }; | 1053 | }; |
| 1030 | 1054 | ||
| @@ -1033,9 +1057,14 @@ impl InnerServer { | |||
| 1033 | source: MediaSource::Plain(uri), | 1057 | source: MediaSource::Plain(uri), |
| 1034 | format: MediaFormat::File, | 1058 | format: MediaFormat::File, |
| 1035 | }; | 1059 | }; |
| 1036 | let display_file = file.display().to_string(); | 1060 | let display_file = |
| 1061 | Self::display_download_path(&file).display().to_string(); | ||
| 1037 | 1062 | ||
| 1038 | self.print_network(&format!("Downloading media to {}", display_file)); | 1063 | self.print_with_prefix_to( |
| 1064 | output_buffer.as_ref(), | ||
| 1065 | &Weechat::prefix(Prefix::Network), | ||
| 1066 | &format!("Downloading media to {}", display_file), | ||
| 1067 | ); | ||
| 1039 | 1068 | ||
| 1040 | match connection | 1069 | match connection |
| 1041 | .spawn(async move { | 1070 | .spawn(async move { |
| @@ -1050,23 +1079,45 @@ impl InnerServer { | |||
| 1050 | .open(&file) | 1079 | .open(&file) |
| 1051 | .and_then(|mut file| file.write_all(&content)) | 1080 | .and_then(|mut file| file.write_all(&content)) |
| 1052 | { | 1081 | { |
| 1053 | self.print_error(&format!( | 1082 | self.print_with_prefix_to( |
| 1054 | "Error writing media to {}: {:#?}", | 1083 | output_buffer.as_ref(), |
| 1055 | display_file, e | 1084 | &Weechat::prefix(Prefix::Error), |
| 1056 | )); | 1085 | &format!( |
| 1086 | "Error writing media to {}: {:#?}", | ||
| 1087 | display_file, e | ||
| 1088 | ), | ||
| 1089 | ); | ||
| 1057 | } else { | 1090 | } else { |
| 1058 | self.print_network(&format!( | 1091 | self.print_with_prefix_to( |
| 1059 | "Successfully downloaded media to {}", | 1092 | output_buffer.as_ref(), |
| 1060 | display_file | 1093 | &Weechat::prefix(Prefix::Network), |
| 1061 | )); | 1094 | &format!( |
| 1095 | "Successfully downloaded media to {}", | ||
| 1096 | display_file | ||
| 1097 | ), | ||
| 1098 | ); | ||
| 1062 | } | 1099 | } |
| 1063 | } | 1100 | } |
| 1064 | Err(e) => { | 1101 | Err(e) => { |
| 1065 | self.print_error(&format!("Error downloading media {:#?}", e)); | 1102 | self.print_with_prefix_to( |
| 1103 | output_buffer.as_ref(), | ||
| 1104 | &Weechat::prefix(Prefix::Error), | ||
| 1105 | &format!("Error downloading media {:#?}", e), | ||
| 1106 | ); | ||
| 1066 | } | 1107 | } |
| 1067 | } | 1108 | } |
| 1068 | } | 1109 | } |
| 1069 | 1110 | ||
| 1111 | fn display_download_path(file: &Path) -> PathBuf { | ||
| 1112 | if file.is_absolute() { | ||
| 1113 | file.to_path_buf() | ||
| 1114 | } else { | ||
| 1115 | std::env::current_dir() | ||
| 1116 | .map(|cwd| cwd.join(file)) | ||
| 1117 | .unwrap_or_else(|_| file.to_path_buf()) | ||
| 1118 | } | ||
| 1119 | } | ||
| 1120 | |||
| 1070 | async fn list_own_devices( | 1121 | async fn list_own_devices( |
| 1071 | &self, | 1122 | &self, |
| 1072 | connection: Connection, | 1123 | connection: Connection, |
| @@ -1416,3 +1467,33 @@ impl InnerServer { | |||
| 1416 | s | 1467 | s |
| 1417 | } | 1468 | } |
| 1418 | } | 1469 | } |
| 1470 | |||
| 1471 | #[cfg(test)] | ||
| 1472 | mod tests { | ||
| 1473 | use super::InnerServer; | ||
| 1474 | use std::path::{Path, PathBuf}; | ||
| 1475 | |||
| 1476 | #[test] | ||
| 1477 | fn keeps_absolute_download_path_for_display() { | ||
| 1478 | let path = Path::new("/tmp/matrix-media"); | ||
| 1479 | |||
| 1480 | assert_eq!(InnerServer::display_download_path(path), path); | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | #[test] | ||
| 1484 | fn expands_relative_download_path_for_display() { | ||
| 1485 | let path = Path::new("matrix-media"); | ||
| 1486 | let expected = std::env::current_dir() | ||
| 1487 | .expect("current working directory") | ||
| 1488 | .join(path); | ||
| 1489 | |||
| 1490 | assert_eq!(InnerServer::display_download_path(path), expected); | ||
| 1491 | } | ||
| 1492 | |||
| 1493 | #[test] | ||
| 1494 | fn preserves_absolute_pathbuf_for_display() { | ||
| 1495 | let path = PathBuf::from("/tmp/matrix-media"); | ||
| 1496 | |||
| 1497 | assert_eq!(InnerServer::display_download_path(&path), path); | ||
| 1498 | } | ||
| 1499 | } | ||