feat(mm-client, mm-bridge): resolve sender display names in history

- Add User::display_name() helper (first+last name, fallback to username)
- Add get_users_by_ids batch endpoint in mm-client
- Add user_display_name field to Post model (skip_deserializing)
- Enrich posts in handle_get_history via single /users/ids request
- Emacs already uses user_display_name when available
This commit is contained in:
Ivan I. Ovchinnikov
2026-11-21 10:30:46 +03:00
parent d62cb61c67
commit c1f1b29135
3 changed files with 100 additions and 19 deletions
+55 -19
View File
@@ -1,8 +1,9 @@
use crate::app::state::{AppState, ConnectionState};
use crate::protocol;
use mm_client::MattermostClient;
use mm_core::{BridgeError, Channel, ErrorKind, ServerConfig, ServerId, User};
use mm_core::{BridgeError, Channel, ErrorKind, Post, ServerConfig, ServerId, User};
use mm_store::{ServerStore, TokenStore};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
pub async fn handle_connect(
@@ -160,7 +161,6 @@ pub async fn handle_list_channels(
match client.get_my_channels(&token).await {
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();
@@ -172,14 +172,7 @@ pub async fn handle_list_channels(
};
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;
ch.display_name = other_user.display_name();
}
}
}
@@ -267,19 +260,62 @@ pub async fn handle_get_history(
};
match client.get_posts(&token, channel_id, limit, before).await {
Ok(posts) => protocol::Response::ok(
request.id,
serde_json::json!({
"server_id": server_id,
"channel_id": channel_id,
"posts": posts,
"before": before,
}),
),
Ok(mut posts) => {
// Обогащаем посты именами отправителей
enrich_posts_with_display_names(&client, &token, &mut posts).await;
protocol::Response::ok(
request.id,
serde_json::json!({
"server_id": server_id,
"channel_id": channel_id,
"posts": posts,
"before": before,
}),
)
}
Err(e) => protocol::Response::err(request.id, e.into()),
}
}
/// Batch-resolve user_ids in posts to human-readable display names.
async fn enrich_posts_with_display_names(
client: &MattermostClient,
token: &str,
posts: &mut [Post],
) {
// Собираем уникальные user_id
let unique_ids: Vec<String> = posts
.iter()
.map(|p| p.user_id.0.clone())
.collect::<HashSet<_>>()
.into_iter()
.collect();
if unique_ids.is_empty() {
return;
}
// Batch-запрос профилей
match client.get_users_by_ids(token, &unique_ids).await {
Ok(users) => {
let name_map: HashMap<String, String> = users
.into_iter()
.map(|u| (u.id.0.clone(), u.display_name()))
.collect();
for post in posts.iter_mut() {
if let Some(name) = name_map.get(&post.user_id.0) {
post.user_display_name = name.clone();
}
}
}
Err(e) => {
tracing::warn!(error = %e, "Failed to resolve user display names, falling back to IDs");
}
}
}
pub async fn handle_send_post(
app_state: &AppState,
request: protocol::Request,