feat: auto-accept matrix room invites in hive-matrix-daemon
This commit is contained in:
parent
9209094397
commit
c5b35fb5fa
2 changed files with 58 additions and 1 deletions
|
|
@ -61,7 +61,8 @@ async fn main() -> Result<()> {
|
|||
.await
|
||||
.context("build matrix client")?;
|
||||
|
||||
timeline::install_message_handler(&matrix_client, hyperhive_socket);
|
||||
timeline::install_message_handler(&matrix_client, hyperhive_socket.clone());
|
||||
timeline::install_invite_handler(&matrix_client, hyperhive_socket);
|
||||
|
||||
// Spawn the unix socket server before sync starts so the MCP
|
||||
// bridge can connect as soon as the first claude turn fires. The
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ use std::sync::Arc;
|
|||
|
||||
use matrix_sdk::{
|
||||
Client, Room, RoomState,
|
||||
ruma::events::room::member::{MembershipState, StrippedRoomMemberEvent},
|
||||
ruma::events::room::message::{MessageType, OriginalSyncRoomMessageEvent},
|
||||
};
|
||||
|
||||
|
|
@ -74,3 +75,58 @@ pub fn install_message_handler(client: &Client, hyperhive_socket: PathBuf) {
|
|||
});
|
||||
tracing::info!("matrix message handler installed");
|
||||
}
|
||||
|
||||
/// Install a handler that auto-accepts room invites as soon as they
|
||||
/// arrive during sync. When the daemon receives an `m.room.member`
|
||||
/// state event with `membership: invite` for the agent's own user ID,
|
||||
/// it calls `join_room_by_id` immediately and fires a hyperhive wake
|
||||
/// so the agent learns about the new room on its next turn.
|
||||
///
|
||||
/// This closes the gap where agents were provisioned and invited to the
|
||||
/// hive Space by hive-c0re but never received the invite notification
|
||||
/// because the handler wasn't registered — the invite sat in the
|
||||
/// `invited_rooms()` list forever until the agent called `join_room`
|
||||
/// manually.
|
||||
pub fn install_invite_handler(client: &Client, hyperhive_socket: PathBuf) {
|
||||
let socket = Arc::new(hyperhive_socket);
|
||||
let own_user = client.user_id().map(std::borrow::ToOwned::to_owned);
|
||||
client.add_event_handler({
|
||||
move |event: StrippedRoomMemberEvent, room: Room, client: Client| {
|
||||
let socket = socket.clone();
|
||||
let own_user = own_user.clone();
|
||||
async move {
|
||||
// Only handle invite events for our own user.
|
||||
if event.content.membership != MembershipState::Invite {
|
||||
return;
|
||||
}
|
||||
let is_ours = own_user
|
||||
.as_ref()
|
||||
.is_some_and(|u| u.as_str() == event.state_key.as_str());
|
||||
if !is_ours {
|
||||
return;
|
||||
}
|
||||
let room_id = room.room_id().to_owned();
|
||||
match client.join_room_by_id(&room_id).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(%room_id, "matrix: auto-accepted invite");
|
||||
let label = room.name().unwrap_or_else(|| room_id.to_string());
|
||||
let body = format!(
|
||||
"[matrix] invited to {label} — auto-accepted; \
|
||||
use read_room to view messages"
|
||||
);
|
||||
if let Err(e) = wake::send_wake(&socket, &body).await {
|
||||
tracing::warn!(
|
||||
error = %e,
|
||||
"matrix: failed to deliver invite-wake to hyperhive"
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, %room_id, "matrix: auto-accept invite failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
tracing::info!("matrix invite handler installed");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue