From 351f428e574ff3db7f4406db5f83d970b07e0e86 Mon Sep 17 00:00:00 2001 From: Mojtaba Chenani Date: Wed, 26 Aug 2026 18:23:18 +0200 Subject: [PATCH] update sender info mapper --- bin/chat-cli/src/app.rs | 60 +++++++++++++++++++++-------------------- bin/chat-cli/src/ui.rs | 6 +++-- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/bin/chat-cli/src/app.rs b/bin/chat-cli/src/app.rs index 1309d12..af97d4c 100644 --- a/bin/chat-cli/src/app.rs +++ b/bin/chat-cli/src/app.rs @@ -7,21 +7,12 @@ use arboard::Clipboard; use crossbeam_channel::Receiver; use logos_chat::{ AccountDirectory, ChatClient, ChatStore, ConversationClass, Event, GroupMetadata, - MessageSender, RegistrationService, Transport, + RegistrationService, Transport, }; use serde::{Deserialize, Serialize}; use crate::utils::now; -/// Who a displayed message is attributed to. `Foreign` carries a short sender -/// label; `Own` is us (and, for now, system output). Leaves room to grow — -/// e.g. a `System` variant for membership/metadata notices. -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum MessageOrigin { - Own, - Foreign(String), -} - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct DisplayMessage { pub from_self: bool, @@ -30,32 +21,32 @@ pub struct DisplayMessage { pub message_id: Option, #[serde(default)] pub delivered_to: Vec, - /// Who sent this message; attributes incoming group messages. + /// Who this message is attributed to: our own account vs. a peer (by account + /// address). The display name is resolved from the account at render time. pub origin: MessageOrigin, } impl DisplayMessage { - fn new(from_self: bool, content: String) -> Self { + fn new(from_self: bool, content: String, origin: MessageOrigin) -> Self { Self { from_self, content, timestamp: now(), message_id: None, delivered_to: Vec::new(), - origin: MessageOrigin::Own, + origin, } } } -/// Short display label for a message's sender: the account (or device) id, -/// truncated. Friendly naming (contacts/aliases) is a later phase. -fn sender_label(sender: &MessageSender) -> String { - let id = sender - .account - .as_ref() - .map(|a| a.as_str()) - .unwrap_or_else(|| sender.local_identity.as_str()); - id[..8.min(id.len())].to_string() +/// Attribution of a displayed message. `Own` is our own account (any of our +/// devices); `Foreign` carries the sender's resolved account address, which the +/// app maps to a display name. (Client resolves credential → account; the app +/// resolves account → name.) +#[derive(Debug, Clone, Serialize, Deserialize)] +pub enum MessageOrigin { + Own, + Foreign(String), } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -241,13 +232,21 @@ where sender, } => { let chat_id = convo_id.to_string(); - let label = sender_label(&sender); + // The client resolved the credential to an account; classify by it. + let origin = match sender.account.as_ref().map(|a| a.as_str()) { + Some(account) if account == self.client.addr() => MessageOrigin::Own, + Some(account) => MessageOrigin::Foreign(account.to_string()), + // Unassociated device — no account claim; fall back to its signer id. + None => MessageOrigin::Foreign(sender.local_identity.as_str().to_string()), + }; let Some(session) = self.state.chats.get_mut(&chat_id) else { return; }; - let mut message = - DisplayMessage::new(false, String::from_utf8_lossy(&content).into_owned()); - message.origin = MessageOrigin::Foreign(label); + let message = DisplayMessage::new( + false, + String::from_utf8_lossy(&content).into_owned(), + origin, + ); session.messages.push(message); } Event::MessageAcked { @@ -324,7 +323,7 @@ where .map_err(|e| anyhow::anyhow!("{e:?}"))?; if let Some(session) = self.state.chats.get_mut(&chat_id) { - let mut message = DisplayMessage::new(true, content.to_string()); + let mut message = DisplayMessage::new(true, content.to_string(), MessageOrigin::Own); // Kept so `MessageAcked` can find this message again. message.message_id = Some(message_id); session.messages.push(message); @@ -335,8 +334,11 @@ where } fn add_system_message(&mut self, content: &str) { - self.command_output - .push(DisplayMessage::new(true, content.to_string())); + self.command_output.push(DisplayMessage::new( + true, + content.to_string(), + MessageOrigin::Own, + )); } pub fn handle_command(&mut self, cmd: &str) -> Result> { diff --git a/bin/chat-cli/src/ui.rs b/bin/chat-cli/src/ui.rs index 09b4054..e9e1d01 100644 --- a/bin/chat-cli/src/ui.rs +++ b/bin/chat-cli/src/ui.rs @@ -115,9 +115,11 @@ where let (prefix, style) = if msg.from_self { ("You".to_string(), Style::default().fg(Color::Green)) } else if is_group { + // App resolves a display name from the sender's account address — + // a truncation for now; a contacts/accounts lookup slots in here. let label = match &msg.origin { - crate::app::MessageOrigin::Foreign(name) => name.as_str(), - crate::app::MessageOrigin::Own => remote_name, + crate::app::MessageOrigin::Own => "you", + crate::app::MessageOrigin::Foreign(account) => &account[..8.min(account.len())], }; (label.to_string(), Style::default().fg(Color::Yellow)) } else {