refactor(matrix): extract room_label helper, dedup 4 call sites

This commit is contained in:
damocles 2026-06-15 12:05:04 +02:00 committed by mara
commit 1b1c6e03b9
2 changed files with 13 additions and 13 deletions

View file

@ -134,6 +134,14 @@ fn extract_body_sync(event: &matrix_sdk::ruma::events::AnySyncTimelineEvent) ->
}
}
/// Human-facing label for a room: its canonical alias when set,
/// otherwise the raw room id. Used wherever a room is named in
/// agent-facing text (wake bodies, loose-ends, the unread guard).
pub fn room_label(room: &matrix_sdk::Room) -> String {
room.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string())
}
/// Refuse to post into a room the agent hasn't caught up on. Returns
/// `Some(error)` with a helpful hint when the room still has unread
/// notifications (the agent must `read_room` then `mark_read` the
@ -149,9 +157,7 @@ fn unread_guard(room: &matrix_sdk::Room) -> Option<DaemonResponse> {
if count == 0 {
return None;
}
let label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
let label = room_label(room);
Some(DaemonResponse::error(format!(
"refusing to send: {count} unread message(s) in {label}. \
use read_room to view them, then mark_read the latest event before \
@ -357,9 +363,7 @@ pub async fn refresh_invite_loose_ends(client: &Client) {
let items: Vec<String> = invites
.iter()
.map(|room| {
let label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
let label = room_label(room);
format!(
"[matrix] pending invite: {label} — use list_invites to see, resolve_invite to accept or reject"
)
@ -547,9 +551,7 @@ pub async fn collect_unread(client: &Client) -> Vec<crate::protocol::RoomUnread>
if count == 0 {
continue;
}
let label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
let label = room_label(&room);
let (last_body, last_sender) = if count == 1 {
fetch_last_message(client, &room).await
} else {

View file

@ -77,10 +77,8 @@ pub fn install_message_handler(client: &Client, hyperhive_socket: PathBuf) {
MessageType::Emote(e) => format!("* {}", e.body),
_ => format!("[{}]", event.content.msgtype()),
};
let room_label = room
.canonical_alias()
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
wake::format_wake_body(event.sender.as_str(), &room_label, &text)
let label = handlers::room_label(&room);
wake::format_wake_body(event.sender.as_str(), &label, &text)
} else {
wake::format_unread_summary(&unread)
};