From d33edb4e443e12198315c83c47a0d52101f937d3 Mon Sep 17 00:00:00 2001 From: "Ivan I. Ovchinnikov" Date: Sat, 21 Nov 2026 14:03:22 +0300 Subject: [PATCH] feat(bridge,ui): preserve file names and use system opener - Add mattermost-download-dir custom (default ~/Downloads) - Pass file_name and download_dir in download_file request - Save files with original names in user-configured directory - Open downloaded files via xdg-open (system default application) --- .../crates/mm-bridge/src/app/handlers/mod.rs | 73 +++++++++++++++++++ bridge/crates/mm-bridge/src/app/mod.rs | 3 + bridge/crates/mm-client/src/client/mod.rs | 19 +++++ lisp/mattermost-channel.el | 46 +++++++++++- lisp/mattermost-custom.el | 5 ++ 5 files changed, 142 insertions(+), 4 deletions(-) diff --git a/bridge/crates/mm-bridge/src/app/handlers/mod.rs b/bridge/crates/mm-bridge/src/app/handlers/mod.rs index a78c9fc..35670de 100644 --- a/bridge/crates/mm-bridge/src/app/handlers/mod.rs +++ b/bridge/crates/mm-bridge/src/app/handlers/mod.rs @@ -326,3 +326,76 @@ pub async fn handle_send_post( Err(e) => protocol::Response::err(request.id, e.into()), } } + +pub async fn handle_download_file( + app_state: &AppState, + data_dir: &Path, + request: protocol::Request, +) -> protocol::Response { + tracing::debug!("Received download_file request: {:?}", request.params); + let params = match request.params.as_object() { + Some(p) => p, + None => return protocol_err(request.id, "params must be an object"), + }; + let server_id = get_server_id(params); + let file_id = match params.get("file_id").and_then(|v| v.as_str()) { + Some(id) => id, + None => return protocol_err(request.id, "missing file_id"), + }; + let file_name = params + .get("file_name") + .and_then(|v| v.as_str()) + .unwrap_or_else(|| "download.bin"); + let download_dir = params + .get("download_dir") + .and_then(|v| v.as_str()) + .map(|s| s.to_string()) + .unwrap_or_else(|| data_dir.join("downloads").to_string_lossy().to_string()); + + let client = match get_client(app_state, &server_id) { + Ok(c) => c, + Err(e) => return protocol::Response::err(request.id, e), + }; + let token = match get_token(app_state, &server_id) { + Ok(t) => t, + Err(e) => return protocol::Response::err(request.id, e), + }; + + match client.download_file(&token, file_id).await { + Ok(bytes) => { + let dir = std::path::PathBuf::from(&download_dir); + if let Err(e) = std::fs::create_dir_all(&dir) { + return protocol::Response::err( + request.id, + mm_core::BridgeError::new( + mm_core::ErrorKind::InternalError, + format!("Failed to create download dir: {}", e), + ), + ); + } + + let file_path = dir.join(file_name); + + if let Err(e) = std::fs::write(&file_path, &bytes) { + return protocol::Response::err( + request.id, + mm_core::BridgeError::new( + mm_core::ErrorKind::InternalError, + format!("Failed to write file: {}", e), + ), + ); + } + + protocol::Response::ok( + request.id, + serde_json::json!({ + "server_id": server_id, + "file_id": file_id, + "local_path": file_path.to_string_lossy(), + "size": bytes.len(), + }), + ) + } + Err(e) => protocol::Response::err(request.id, e.into()), + } +} diff --git a/bridge/crates/mm-bridge/src/app/mod.rs b/bridge/crates/mm-bridge/src/app/mod.rs index d307586..115b6a1 100644 --- a/bridge/crates/mm-bridge/src/app/mod.rs +++ b/bridge/crates/mm-bridge/src/app/mod.rs @@ -32,6 +32,9 @@ impl App { "open_self_dm" => handlers::handle_open_self_dm(&self.state, request).await, "get_history" => handlers::handle_get_history(&self.state, request).await, "send_post" => handlers::handle_send_post(&self.state, request).await, + "download_file" => { + handlers::handle_download_file(&self.state, &self.data_dir, request).await + } _ => protocol::Response::err( request.id, BridgeError::new( diff --git a/bridge/crates/mm-client/src/client/mod.rs b/bridge/crates/mm-client/src/client/mod.rs index f0878af..79a25a6 100644 --- a/bridge/crates/mm-client/src/client/mod.rs +++ b/bridge/crates/mm-client/src/client/mod.rs @@ -294,6 +294,25 @@ impl MattermostClient { let post: Post = response.json().await.map_err(ClientError::Http)?; Ok(post) } + + /// Download a file by its ID. Returns the file bytes. + pub async fn download_file(&self, token: &str, file_id: &str) -> Result, ClientError> { + let url = format!("{}/api/v4/files/{}", self.base_url, file_id); + let response = self + .client + .get(&url) + .header(AUTHORIZATION, format!("Bearer {}", token)) + .send() + .await + .map_err(ClientError::Http)?; + + if !response.status().is_success() { + return Err(api_error(response).await); + } + + let bytes = response.bytes().await.map_err(ClientError::Http)?; + Ok(bytes.to_vec()) + } } /// Accept `[fileinfo, ...]`, `{file_infos: [...]}`, or diff --git a/lisp/mattermost-channel.el b/lisp/mattermost-channel.el index 80c4aaa..c7acb41 100644 --- a/lisp/mattermost-channel.el +++ b/lisp/mattermost-channel.el @@ -7,6 +7,7 @@ (let ((map (make-sparse-keymap))) (define-key map (kbd "C-c C-s") #'mattermost-channel-send-message) (define-key map (kbd "g") #'mattermost-channel-refresh) + (define-key map (kbd "RET") #'mattermost-channel-open-link) map) "Keymap for `mattermost-channel-mode`.") @@ -88,7 +89,12 @@ (let* ((name (or (plist-get f :name) "unknown")) (size (or (plist-get f :size) 0)) (id (plist-get f :id))) - (format " πŸ“Ž %s (%s)\n" name (mattermost--format-file-size size)))) + (propertize + (format " πŸ“Ž %s (%s)\n" name (mattermost--format-file-size size)) + 'mm-file-id id + 'mm-file-name name + 'mouse-face 'highlight + 'help-echo "RET to download and open"))) files "")) "\n"))) @@ -99,11 +105,11 @@ (erase-buffer) (insert (format "Channel: %s\n" channel-name)) (insert "========================================\n\n") - + (dolist (post posts) (insert (mattermost--format-post post)) (insert "\n")) - + (goto-char (point-max)))) (defun mattermost-channel-refresh () @@ -121,7 +127,7 @@ The message will appear automatically via WebSocket fan-out; no refresh needed." (msg (read-string "Message: "))) (when (string-empty-p msg) (error "Message cannot be empty")) - + (message "[MM-DEBUG] Sending message to channel %s..." channel-id) (mattermost--send-request "send_post" @@ -133,6 +139,38 @@ The message will appear automatically via WebSocket fan-out; no refresh needed." (message "[MM] Message sent. Waiting for WS echo...") (message "[MM] Failed to send message: %S" (plist-get response :error))))))) +(defun mattermost-channel-open-link () + "Open the link under point. +If point is on a file attachment, download it and open with xdg-open. +Otherwise report that there is no openable link." + (interactive) + (let ((file-id (get-text-property (point) 'mm-file-id)) + (file-name (get-text-property (point) 'mm-file-name))) + (cond + ((and file-id file-name) + (let ((server-id mattermost--server-id) + (download-dir (expand-file-name mattermost-download-dir))) + (unless server-id + (error "No server ID associated with this buffer")) + (message "[MM] Downloading file: %s to %s..." file-name download-dir) + (mattermost--send-request + "download_file" + (list :server_id server-id + :file_id file-id + :file_name file-name + :download_dir download-dir) + (lambda (response) + (if (plist-get response :ok) + (let* ((result (plist-get response :result)) + (local-path (plist-get result :local_path))) + (message "[MM] File saved to: %s" local-path) + ;; ΠžΡ‚ΠΊΡ€Ρ‹Π²Π°Π΅ΠΌ Ρ‡Π΅Ρ€Π΅Π· xdg-open (систСмноС ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ) + (start-process "xdg-open" nil "xdg-open" local-path)) + (message "[MM] Failed to download file: %S" + (plist-get response :error))))))) + (t + (message "[MM] No downloadable link under point."))))) + (defun mattermost-channel-on-posted (params) "Handle a 'posted' event. PARAMS should contain the 'post' plist." (let* ((post (plist-get params :post)) diff --git a/lisp/mattermost-custom.el b/lisp/mattermost-custom.el index fac011c..ab20567 100644 --- a/lisp/mattermost-custom.el +++ b/lisp/mattermost-custom.el @@ -42,6 +42,11 @@ If `inline', show replies in the main buffer." :type 'string :group 'mattermost) +(defcustom mattermost-download-dir "~/Downloads" + "Directory where downloaded files are saved." + :type 'directory + :group 'mattermost) + (defface mattermost-sender-face '((t (:weight bold))) "Face used for message sender names in channel buffers."