From 8f1252e7e3a23a4fc1e1d4fc946c55b75675baa5 Mon Sep 17 00:00:00 2001 From: "Ivan I. Ovchinnikov" Date: Sat, 21 Nov 2026 09:22:40 +0300 Subject: [PATCH] feat(mm-client, mm-bridge): resolve DM channel display names - Add get_user method to mm-client to fetch user profile by ID - Update handle_list_channels to post-process DM channels - Parse 'user1__user2' format to find the other participant - Fetch other participant's profile and populate display_name - Fallback to username if first_name/last_name are empty --- bridge/crates/mm-bridge/src/app/handlers.rs | 48 +++++++++++++++++---- bridge/crates/mm-client/src/client/mod.rs | 19 +++++++- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/bridge/crates/mm-bridge/src/app/handlers.rs b/bridge/crates/mm-bridge/src/app/handlers.rs index 99fa178..a719de4 100644 --- a/bridge/crates/mm-bridge/src/app/handlers.rs +++ b/bridge/crates/mm-bridge/src/app/handlers.rs @@ -1,7 +1,7 @@ use crate::app::state::{AppState, ConnectionState}; use crate::protocol; use mm_client::MattermostClient; -use mm_core::{BridgeError, ErrorKind, ServerConfig, ServerId, User}; +use mm_core::{BridgeError, Channel, ErrorKind, ServerConfig, ServerId, User}; use mm_store::{ServerStore, TokenStore}; use std::path::{Path, PathBuf}; @@ -63,7 +63,6 @@ pub async fn handle_connect( }, ); - // Start WebSocket in background tracing::info!(server_id = %server_id, "Connection established, starting WebSocket"); if let Err(e) = app_state.start_ws(&server_id).await { tracing::error!(server_id = %server_id, "Failed to start WebSocket: {}", e); @@ -154,15 +153,46 @@ pub async fn handle_list_channels( Ok(t) => t, Err(e) => return protocol::Response::err(request.id, e), }; + let current_user_id = match get_user_id(app_state, &server_id) { + Ok(id) => id.0, + Err(e) => return protocol::Response::err(request.id, e), + }; match client.get_my_channels(&token).await { - Ok(channels) => protocol::Response::ok( - request.id, - serde_json::json!({ - "server_id": server_id, - "channels": channels, - }), - ), + Ok(mut channels) => { + // Пост-обработка: разрешение имён для DM-каналов + for ch in &mut channels { + if ch.channel_type == "D" && ch.display_name.is_empty() { + let parts: Vec<&str> = ch.name.split("__").collect(); + if parts.len() == 2 { + let other_user_id = if parts[0] == current_user_id { + parts[1] + } else { + parts[0] + }; + + if let Ok(other_user) = client.get_user(&token, other_user_id).await { + let name = if !other_user.first_name.is_empty() { + format!("{} {}", other_user.first_name, other_user.last_name) + .trim() + .to_string() + } else { + other_user.username + }; + ch.display_name = name; + } + } + } + } + + protocol::Response::ok( + request.id, + serde_json::json!({ + "server_id": server_id, + "channels": channels, + }), + ) + } Err(e) => protocol::Response::err(request.id, e.into()), } } diff --git a/bridge/crates/mm-client/src/client/mod.rs b/bridge/crates/mm-client/src/client/mod.rs index 06b596a..9756644 100644 --- a/bridge/crates/mm-client/src/client/mod.rs +++ b/bridge/crates/mm-client/src/client/mod.rs @@ -66,6 +66,24 @@ impl MattermostClient { Ok(user) } + pub async fn get_user(&self, token: &str, user_id: &str) -> Result { + let url = format!("{}/api/v4/users/{}", self.base_url, user_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 user: User = response.json().await.map_err(ClientError::Http)?; + Ok(user) + } + pub async fn get_teams(&self, token: &str) -> Result, ClientError> { let url = format!("{}/api/v4/users/me/teams", self.base_url); let response = self @@ -130,7 +148,6 @@ impl MattermostClient { Ok(ChannelId::new(ch.id)) } - /// Get posts for a channel. `before` = oldest known post id for pagination. pub async fn get_posts( &self, token: &str,