hive-matrix-mcp: add a leave_room tool
This commit is contained in:
parent
56ed2e07da
commit
d88a0cc526
3 changed files with 68 additions and 4 deletions
|
|
@ -900,6 +900,22 @@ pub async fn join_room(client: &Client, room_ref: &str) -> DaemonResponse {
|
|||
}
|
||||
}
|
||||
|
||||
/// Leave the joined room `room_ref` (id or alias), so a retired account stops
|
||||
/// receiving its messages.
|
||||
pub async fn leave_room(client: &Client, room_ref: &str) -> DaemonResponse {
|
||||
let room = match resolve_room(client, room_ref).await {
|
||||
Ok(r) => r,
|
||||
Err(e) => return e,
|
||||
};
|
||||
match room.leave().await {
|
||||
Ok(()) => DaemonResponse::ok(&serde_json::json!({
|
||||
"left": true,
|
||||
"room_id": room.room_id().to_string(),
|
||||
})),
|
||||
Err(e) => DaemonResponse::error(format!("leave room {room_ref}: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Accept or reject a pending invite to `room_ref` (id or alias).
|
||||
///
|
||||
/// `Accept` is exactly `join_room` (joining a room you were invited to
|
||||
|
|
|
|||
|
|
@ -247,6 +247,24 @@ async fn rejecting_an_invite_leaves_the_room() {
|
|||
assert_eq!(got["room_id"], "!invited:localhost");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn leaving_a_joined_room_asks_the_server_and_an_unknown_room_is_refused() {
|
||||
let (server, client) = caught_up_room().await;
|
||||
server
|
||||
.mock_room_leave()
|
||||
.ok(room_id!("!room:localhost"))
|
||||
.expect(1)
|
||||
.mount()
|
||||
.await;
|
||||
|
||||
let got = ok(leave_room(&client, "!room:localhost").await);
|
||||
|
||||
assert_eq!(got["left"], true);
|
||||
assert_eq!(got["room_id"], "!room:localhost");
|
||||
let unknown = err(leave_room(&client, "!never:localhost").await);
|
||||
assert!(unknown.contains("not joined"), "{unknown}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn reading_a_room_returns_its_events_newest_first_with_sender_and_body() {
|
||||
let (_server, client) = room_with(vec![
|
||||
|
|
|
|||
|
|
@ -212,6 +212,17 @@ struct ListInvitesArgs {
|
|||
account: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
struct LeaveRoomArgs {
|
||||
/// Matrix room id (`!abc:server`) or canonical alias (`#name:server`) of a
|
||||
/// room the account has joined.
|
||||
room: String,
|
||||
/// Matrix account to act as (a `name` from `services.hyperhive.agent.matrixAccounts`).
|
||||
/// Omit to use the agent's primary account.
|
||||
#[serde(default)]
|
||||
account: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
struct JoinRoomArgs {
|
||||
/// Matrix room id (`!abc:server`) or canonical alias (`#name:server`).
|
||||
|
|
@ -414,6 +425,20 @@ impl MatrixMcp {
|
|||
render(handlers::join_room(&client, &args.room).await)
|
||||
}
|
||||
|
||||
#[tool(
|
||||
description = "Leave a matrix room you have joined, by id (!abc:server) or \
|
||||
alias (#name:server). Use it to drop a room a retired account still sits \
|
||||
in, so it stops getting that room's messages. To decline a pending \
|
||||
invite use `resolve_invite`. The room leaves `list_rooms`."
|
||||
)]
|
||||
async fn leave_room(&self, Parameters(args): Parameters<LeaveRoomArgs>) -> String {
|
||||
let client = match self.resolve(args.account.as_deref()) {
|
||||
Ok(c) => c,
|
||||
Err(e) => return format!("matrix error: {e}"),
|
||||
};
|
||||
render(handlers::leave_room(&client, &args.room).await)
|
||||
}
|
||||
|
||||
#[tool(
|
||||
description = "Accept or reject a pending matrix invite. `room` is a room id \
|
||||
(!abc:server) or alias (#name:server) you were invited to; `action` is \
|
||||
|
|
|
|||
Loading…
Reference in a new issue