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
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use crate::app::state::{AppState, ConnectionState};
|
use crate::app::state::{AppState, ConnectionState};
|
||||||
use crate::protocol;
|
use crate::protocol;
|
||||||
use mm_client::MattermostClient;
|
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 mm_store::{ServerStore, TokenStore};
|
||||||
use std::path::{Path, PathBuf};
|
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");
|
tracing::info!(server_id = %server_id, "Connection established, starting WebSocket");
|
||||||
if let Err(e) = app_state.start_ws(&server_id).await {
|
if let Err(e) = app_state.start_ws(&server_id).await {
|
||||||
tracing::error!(server_id = %server_id, "Failed to start WebSocket: {}", e);
|
tracing::error!(server_id = %server_id, "Failed to start WebSocket: {}", e);
|
||||||
@@ -154,15 +153,46 @@ pub async fn handle_list_channels(
|
|||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(e) => return protocol::Response::err(request.id, e),
|
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 {
|
match client.get_my_channels(&token).await {
|
||||||
Ok(channels) => protocol::Response::ok(
|
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,
|
request.id,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"server_id": server_id,
|
"server_id": server_id,
|
||||||
"channels": channels,
|
"channels": channels,
|
||||||
}),
|
}),
|
||||||
),
|
)
|
||||||
|
}
|
||||||
Err(e) => protocol::Response::err(request.id, e.into()),
|
Err(e) => protocol::Response::err(request.id, e.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,24 @@ impl MattermostClient {
|
|||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_user(&self, token: &str, user_id: &str) -> Result<User, ClientError> {
|
||||||
|
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<Vec<mm_core::Team>, ClientError> {
|
pub async fn get_teams(&self, token: &str) -> Result<Vec<mm_core::Team>, ClientError> {
|
||||||
let url = format!("{}/api/v4/users/me/teams", self.base_url);
|
let url = format!("{}/api/v4/users/me/teams", self.base_url);
|
||||||
let response = self
|
let response = self
|
||||||
@@ -130,7 +148,6 @@ impl MattermostClient {
|
|||||||
Ok(ChannelId::new(ch.id))
|
Ok(ChannelId::new(ch.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get posts for a channel. `before` = oldest known post id for pagination.
|
|
||||||
pub async fn get_posts(
|
pub async fn get_posts(
|
||||||
&self,
|
&self,
|
||||||
token: &str,
|
token: &str,
|
||||||
|
|||||||
Reference in New Issue
Block a user