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:
@@ -1,8 +1,9 @@
|
|||||||
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, Channel, ErrorKind, ServerConfig, ServerId, User};
|
use mm_core::{BridgeError, Channel, ErrorKind, Post, ServerConfig, ServerId, User};
|
||||||
use mm_store::{ServerStore, TokenStore};
|
use mm_store::{ServerStore, TokenStore};
|
||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
pub async fn handle_connect(
|
pub async fn handle_connect(
|
||||||
@@ -160,7 +161,6 @@ pub async fn handle_list_channels(
|
|||||||
|
|
||||||
match client.get_my_channels(&token).await {
|
match client.get_my_channels(&token).await {
|
||||||
Ok(mut channels) => {
|
Ok(mut channels) => {
|
||||||
// Пост-обработка: разрешение имён для DM-каналов
|
|
||||||
for ch in &mut channels {
|
for ch in &mut channels {
|
||||||
if ch.channel_type == "D" && ch.display_name.is_empty() {
|
if ch.channel_type == "D" && ch.display_name.is_empty() {
|
||||||
let parts: Vec<&str> = ch.name.split("__").collect();
|
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 {
|
if let Ok(other_user) = client.get_user(&token, other_user_id).await {
|
||||||
let name = if !other_user.first_name.is_empty() {
|
ch.display_name = other_user.display_name();
|
||||||
format!("{} {}", other_user.first_name, other_user.last_name)
|
|
||||||
.trim()
|
|
||||||
.to_string()
|
|
||||||
} else {
|
|
||||||
other_user.username
|
|
||||||
};
|
|
||||||
ch.display_name = name;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -267,7 +260,11 @@ pub async fn handle_get_history(
|
|||||||
};
|
};
|
||||||
|
|
||||||
match client.get_posts(&token, channel_id, limit, before).await {
|
match client.get_posts(&token, channel_id, limit, before).await {
|
||||||
Ok(posts) => protocol::Response::ok(
|
Ok(mut posts) => {
|
||||||
|
// Обогащаем посты именами отправителей
|
||||||
|
enrich_posts_with_display_names(&client, &token, &mut posts).await;
|
||||||
|
|
||||||
|
protocol::Response::ok(
|
||||||
request.id,
|
request.id,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
"server_id": server_id,
|
"server_id": server_id,
|
||||||
@@ -275,11 +272,50 @@ pub async fn handle_get_history(
|
|||||||
"posts": posts,
|
"posts": posts,
|
||||||
"before": before,
|
"before": before,
|
||||||
}),
|
}),
|
||||||
),
|
)
|
||||||
|
}
|
||||||
Err(e) => protocol::Response::err(request.id, e.into()),
|
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(
|
pub async fn handle_send_post(
|
||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
request: protocol::Request,
|
request: protocol::Request,
|
||||||
|
|||||||
@@ -84,6 +84,33 @@ impl MattermostClient {
|
|||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Batch-fetch users by their IDs. Mattermost limit is 1000 per request.
|
||||||
|
pub async fn get_users_by_ids(
|
||||||
|
&self,
|
||||||
|
token: &str,
|
||||||
|
user_ids: &[String],
|
||||||
|
) -> Result<Vec<User>, ClientError> {
|
||||||
|
if user_ids.is_empty() {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
let url = format!("{}/api/v4/users/ids", self.base_url);
|
||||||
|
let response = self
|
||||||
|
.client
|
||||||
|
.post(&url)
|
||||||
|
.header(AUTHORIZATION, format!("Bearer {}", token))
|
||||||
|
.json(user_ids)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(ClientError::Http)?;
|
||||||
|
|
||||||
|
if !response.status().is_success() {
|
||||||
|
return Err(api_error(response).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
let users: Vec<User> = response.json().await.map_err(ClientError::Http)?;
|
||||||
|
Ok(users)
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -51,6 +51,20 @@ pub struct User {
|
|||||||
pub last_name: String,
|
pub last_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl User {
|
||||||
|
/// Human-readable display name: "First Last", falling back to username.
|
||||||
|
pub fn display_name(&self) -> String {
|
||||||
|
let full = format!("{} {}", self.first_name, self.last_name)
|
||||||
|
.trim()
|
||||||
|
.to_string();
|
||||||
|
if full.is_empty() {
|
||||||
|
self.username.clone()
|
||||||
|
} else {
|
||||||
|
full
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Persisted per-server configuration (no secrets).
|
/// Persisted per-server configuration (no secrets).
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
@@ -88,4 +102,8 @@ pub struct Post {
|
|||||||
pub delete_at: i64,
|
pub delete_at: i64,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub file_ids: Vec<String>,
|
pub file_ids: Vec<String>,
|
||||||
|
/// Populated by the bridge after resolving user_id via /users/ids.
|
||||||
|
/// Skipped during deserialization from Mattermost API.
|
||||||
|
#[serde(default, skip_deserializing)]
|
||||||
|
pub user_display_name: String,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user