Commit 5b89750b7f

5b89750b7f4c4c6dcd19d03ba85fd9f6d8e316ad

parent: 3fa8fbdee5

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-03 07:43 UTC
committer: <strk@kbt.io>

fix: report media downloads in source buffer

Layout: unified · split

src/commands/media.rs +16 −5
@@ -7,7 +7,7 @@ use clap::{
77use matrix_sdk::ruma::MxcUri;
88use weechat::{buffer::Buffer, Weechat};
99
10use crate::Servers;
10use crate::{BufferOwner, Servers};
1111
1212pub struct MediaCommand;
1313
@@ -16,9 +16,18 @@ impl MediaCommand {
1616 pub const COMPLETION: &'static str = "download %(matrix-media)";
1717
1818 pub fn run(buffer: &Buffer, servers: &Servers, args: &ArgMatches) {
19 let server = match servers.find_server(buffer) {
20 Some(server) => server,
21 None => {
19 let (server, output_buffer) = match servers.buffer_owner(buffer) {
20 BufferOwner::Room(server, room) => {
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 => {
2231 Weechat::print("Must be executed on Matrix buffer");
2332 return;
2433 }
@@ -51,7 +60,9 @@ impl MediaCommand {
5160 };
5261
5362 Weechat::spawn(async move {
54 server.download_media(uri.into(), file).await;
63 server
64 .download_media(uri.into(), file, output_buffer)
65 .await;
5566 })
5667 .detach();
5768 }
src/server.rs +95 −14
@@ -60,7 +60,7 @@ use std::{
6060 cmp::Reverse,
6161 collections::HashMap,
6262 io::Write,
63 path::PathBuf,
63 path::{Path, PathBuf},
6464 rc::{Rc, Weak},
6565};
6666use tracing::error;
@@ -669,6 +669,21 @@ impl InnerServer {
669669 self.print(&format!("{}{}: {}", prefix, PLUGIN_NAME, message));
670670 }
671671
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
672687 /// Print an network message to the server buffer.
673688 pub fn print_network(&self, message: &str) {
674689 self.print_with_prefix(&Weechat::prefix(Prefix::Network), message);
@@ -1020,11 +1035,20 @@ impl InnerServer {
10201035 };
10211036 }
10221037
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 ) {
10241044 let connection = if let Some(c) = self.connection() {
10251045 c
10261046 } 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 );
10281052 return;
10291053 };
10301054
@@ -1033,9 +1057,14 @@ impl InnerServer {
10331057 source: MediaSource::Plain(uri),
10341058 format: MediaFormat::File,
10351059 };
1036 let display_file = file.display().to_string();
1060 let display_file =
1061 Self::display_download_path(&file).display().to_string();
10371062
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 );
10391068
10401069 match connection
10411070 .spawn(async move {
@@ -1050,23 +1079,45 @@ impl InnerServer {
10501079 .open(&file)
10511080 .and_then(|mut file| file.write_all(&content))
10521081 {
1053 self.print_error(&format!(
1054 "Error writing media to {}: {:#?}",
1055 display_file, e
1056 ));
1082 self.print_with_prefix_to(
1083 output_buffer.as_ref(),
1084 &Weechat::prefix(Prefix::Error),
1085 &format!(
1086 "Error writing media to {}: {:#?}",
1087 display_file, e
1088 ),
1089 );
10571090 } else {
1058 self.print_network(&format!(
1059 "Successfully downloaded media to {}",
1060 display_file
1061 ));
1091 self.print_with_prefix_to(
1092 output_buffer.as_ref(),
1093 &Weechat::prefix(Prefix::Network),
1094 &format!(
1095 "Successfully downloaded media to {}",
1096 display_file
1097 ),
1098 );
10621099 }
10631100 }
10641101 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 );
10661107 }
10671108 }
10681109 }
10691110
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
10701121 async fn list_own_devices(
10711122 &self,
10721123 connection: Connection,
@@ -1416,3 +1467,33 @@ impl InnerServer {
14161467 s
14171468 }
14181469}
1470
1471#[cfg(test)]
1472mod 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}