fix(chats): a departed partner keeps their name instead of (only you)
- name from stored members or messages, else Unknown User - a stored real title wins over both fallbacks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -3,13 +3,15 @@ use std::collections::HashMap;
3
3
use chatsvc::BotProfile;
4
4
use chrono::Utc;
5
5
use graph::Chat;
6
use store::{AvatarRecord, BOT_MRI_PREFIX, ChatRecord, MemberRecord};
6
use store::{AvatarRecord, BOT_MRI_PREFIX, ChatRecord, MemberRecord, UNKNOWN_USER_NAME};
7
7
8
8
use crate::engine::SyncEngine;
9
9
use crate::error::{Error, Result};
10
10
use crate::mapping::chat_record;
11
11
use crate::remote::Remote;
12
12
13
const ALONE_TITLE: &str = "(only you)";
14
13
15
/// Bots Graph leaves out of 1:1 chat members, such as the Echo test call bot, by the id in the chat id.
14
16
#[derive(Default)]
15
17
pub(crate) struct BotPartners {
@@ -83,7 +85,49 @@ impl<R: Remote> SyncEngine<R> {
83
85
}
84
86
85
87
pub(crate) fn partner_chat_record(&self, chat: &Chat, my_user_id: &str) -> ChatRecord {
86
self.bot_partners().chat_record(chat, my_user_id)
88
let mut record = self.bot_partners().chat_record(chat, my_user_id);
89
self.name_departed_partners(std::slice::from_ref(chat), std::slice::from_mut(&mut record), my_user_id);
90
record
91
}
92
93
/// Graph drops a departed person from the members; Teams keeps their name from the chat history.
94
pub(crate) fn name_departed_partners(
95
&self,
96
chats: &[Chat],
97
records: &mut [ChatRecord],
98
my_user_id: &str,
99
) {
100
let partner_ids: HashMap<&str, &str> = chats
101
.iter()
102
.filter_map(|chat| Some((chat.id.as_str(), chat.memberless_partner_id(my_user_id)?)))
103
.collect();
104
let departed: Vec<(usize, String)> = records
105
.iter()
106
.enumerate()
107
.filter(|(_, record)| record.title == ALONE_TITLE)
108
.filter_map(|(index, record)| {
109
Some((index, (*partner_ids.get(record.id.as_str())?).to_owned()))
110
})
111
.collect();
112
if departed.is_empty() {
113
return;
114
}
115
let partner_ids: Vec<String> = departed.iter().map(|(_, partner_id)| partner_id.clone()).collect();
116
let names = match self.store.display_names(&partner_ids) {
117
Ok(names) => names,
118
Err(error) => {
119
self.report(format!("cannot name departed chat partners: {error}"));
120
return;
121
}
122
};
123
for (index, partner_id) in departed {
124
let name = names
125
.get(&partner_id)
126
.map_or(UNKNOWN_USER_NAME, String::as_str)
127
.to_owned();
128
records[index].member_summary = name.clone();
129
records[index].title = name;
130
}
87
131
}
88
132
89
133
pub(crate) async fn fetch_bot_icons(&self, bot_mris: &[String]) -> Result<Vec<String>> {
@@ -3,13 +3,15 @@ use std::collections::HashMap;3
use chatsvc::BotProfile;3
use chatsvc::BotProfile;4
use chrono::Utc;4
use chrono::Utc;5
use graph::Chat;5
use graph::Chat;6
use store::{AvatarRecord, BOT_MRI_PREFIX, ChatRecord, MemberRecord};6
use store::{AvatarRecord, BOT_MRI_PREFIX, ChatRecord, MemberRecord, UNKNOWN_USER_NAME};7
7
8
use crate::engine::SyncEngine;8
use crate::engine::SyncEngine;9
use crate::error::{Error, Result};9
use crate::error::{Error, Result};10
use crate::mapping::chat_record;10
use crate::mapping::chat_record;11
use crate::remote::Remote;11
use crate::remote::Remote;12
12
13
const ALONE_TITLE: &str = "(only you)";14
13
/// Bots Graph leaves out of 1:1 chat members, such as the Echo test call bot, by the id in the chat id.15
/// Bots Graph leaves out of 1:1 chat members, such as the Echo test call bot, by the id in the chat id.14
#[derive(Default)]16
#[derive(Default)]15
pub(crate) struct BotPartners {17
pub(crate) struct BotPartners {@@ -83,7 +85,49 @@ impl<R: Remote> SyncEngine<R> {83
}85
}84
86
85
pub(crate) fn partner_chat_record(&self, chat: &Chat, my_user_id: &str) -> ChatRecord {87
pub(crate) fn partner_chat_record(&self, chat: &Chat, my_user_id: &str) -> ChatRecord {86
self.bot_partners().chat_record(chat, my_user_id)88
let mut record = self.bot_partners().chat_record(chat, my_user_id);89
self.name_departed_partners(std::slice::from_ref(chat), std::slice::from_mut(&mut record), my_user_id);90
record91
}92
93
/// Graph drops a departed person from the members; Teams keeps their name from the chat history.94
pub(crate) fn name_departed_partners(95
&self,96
chats: &[Chat],97
records: &mut [ChatRecord],98
my_user_id: &str,99
) {100
let partner_ids: HashMap<&str, &str> = chats101
.iter()102
.filter_map(|chat| Some((chat.id.as_str(), chat.memberless_partner_id(my_user_id)?)))103
.collect();104
let departed: Vec<(usize, String)> = records105
.iter()106
.enumerate()107
.filter(|(_, record)| record.title == ALONE_TITLE)108
.filter_map(|(index, record)| {109
Some((index, (*partner_ids.get(record.id.as_str())?).to_owned()))110
})111
.collect();112
if departed.is_empty() {113
return;114
}115
let partner_ids: Vec<String> = departed.iter().map(|(_, partner_id)| partner_id.clone()).collect();116
let names = match self.store.display_names(&partner_ids) {117
Ok(names) => names,118
Err(error) => {119
self.report(format!("cannot name departed chat partners: {error}"));120
return;121
}122
};123
for (index, partner_id) in departed {124
let name = names125
.get(&partner_id)126
.map_or(UNKNOWN_USER_NAME, String::as_str)127
.to_owned();128
records[index].member_summary = name.clone();129
records[index].title = name;130
}87
}131
}88
132
89
pub(crate) async fn fetch_bot_icons(&self, bot_mris: &[String]) -> Result<Vec<String>> {133
pub(crate) async fn fetch_bot_icons(&self, bot_mris: &[String]) -> Result<Vec<String>> {@@ -317,7 +317,7 @@ impl<R: Remote> SyncEngine<R> {
317
317
my_user_id: &str,
318
318
) -> Result<(Vec<ChatRecord>, Vec<String>)> {
319
319
let now = Utc::now();
320
self.with_locally_hidden(|locally| {
320
let (mut records, hidden) = self.with_locally_hidden(|locally| {
321
321
split_hidden(
322
322
page,
323
323
my_user_id,
@@ -326,7 +326,9 @@ impl<R: Remote> SyncEngine<R> {
326
326
locally,
327
327
now,
328
328
)
329
})
329
})?;
330
self.name_departed_partners(&page.items, &mut records, my_user_id);
331
Ok((records, hidden))
330
332
}
331
333
332
334
fn any_chat_changed(&self, records: &[ChatRecord]) -> Result<bool> {
@@ -317,7 +317,7 @@ impl<R: Remote> SyncEngine<R> {317
my_user_id: &str,317
my_user_id: &str,318
) -> Result<(Vec<ChatRecord>, Vec<String>)> {318
) -> Result<(Vec<ChatRecord>, Vec<String>)> {319
let now = Utc::now();319
let now = Utc::now();320
self.with_locally_hidden(|locally| {320
let (mut records, hidden) = self.with_locally_hidden(|locally| {321
split_hidden(321
split_hidden(322
page,322
page,323
my_user_id,323
my_user_id,@@ -326,7 +326,9 @@ impl<R: Remote> SyncEngine<R> {326
locally,326
locally,327
now,327
now,328
)328
)329
})329
})?;330
self.name_departed_partners(&page.items, &mut records, my_user_id);331
Ok((records, hidden))330
}332
}331
333
332
fn any_chat_changed(&self, records: &[ChatRecord]) -> Result<bool> {334
fn any_chat_changed(&self, records: &[ChatRecord]) -> Result<bool> {@@ -22,7 +22,7 @@ impl Store {
22
22
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)
23
23
ON CONFLICT (id) DO UPDATE SET
24
24
kind = excluded.kind,
25
title = CASE WHEN excluded.title = '(only you)' AND chats.title NOT IN ('', '(only you)')
25
title = CASE WHEN excluded.title IN ('(only you)', 'Unknown User') AND chats.title NOT IN ('', '(only you)', 'Unknown User')
26
26
THEN chats.title ELSE excluded.title END,
27
27
member_summary = excluded.member_summary,
28
28
last_message_at = excluded.last_message_at,
@@ -22,7 +22,7 @@ impl Store {22
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)22
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)23
ON CONFLICT (id) DO UPDATE SET23
ON CONFLICT (id) DO UPDATE SET24
kind = excluded.kind,24
kind = excluded.kind,25
title = CASE WHEN excluded.title = '(only you)' AND chats.title NOT IN ('', '(only you)')25
title = CASE WHEN excluded.title IN ('(only you)', 'Unknown User') AND chats.title NOT IN ('', '(only you)', 'Unknown User')26
THEN chats.title ELSE excluded.title END,26
THEN chats.title ELSE excluded.title END,27
member_summary = excluded.member_summary,27
member_summary = excluded.member_summary,28
last_message_at = excluded.last_message_at,28
last_message_at = excluded.last_message_at,