diff --git a/hive-matrix-mcp/src/handlers.rs b/hive-matrix-mcp/src/handlers.rs index e8095458..f9a9f458 100644 --- a/hive-matrix-mcp/src/handlers.rs +++ b/hive-matrix-mcp/src/handlers.rs @@ -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 { 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 = 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 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 { diff --git a/hive-matrix-mcp/src/timeline.rs b/hive-matrix-mcp/src/timeline.rs index 763582c0..41553827 100644 --- a/hive-matrix-mcp/src/timeline.rs +++ b/hive-matrix-mcp/src/timeline.rs @@ -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) };