feat(matrix mcp): add resolve_invite tool to accept or reject pending invites
This commit is contained in:
parent
c59a0de01e
commit
86f0de751f
6 changed files with 121 additions and 19 deletions
|
|
@ -25,7 +25,7 @@ use matrix_sdk::{
|
|||
};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::protocol::DaemonResponse;
|
||||
use crate::protocol::{DaemonResponse, InviteAction};
|
||||
|
||||
/// JSON-shape for `list_invites`: one row per pending room invite.
|
||||
#[derive(Debug, Serialize)]
|
||||
|
|
@ -309,7 +309,7 @@ pub async fn refresh_invite_loose_ends(client: &Client) {
|
|||
.canonical_alias()
|
||||
.map_or_else(|| room.room_id().to_string(), |a| a.to_string());
|
||||
format!(
|
||||
"[matrix] pending invite: {label} — use list_invites to see, join_room to accept"
|
||||
"[matrix] pending invite: {label} — use list_invites to see, resolve_invite to accept or reject"
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
|
@ -350,6 +350,41 @@ pub async fn join_room(client: &Client, room_ref: &str) -> DaemonResponse {
|
|||
}
|
||||
}
|
||||
|
||||
/// Accept or reject a pending invite to `room_ref` (id or alias).
|
||||
///
|
||||
/// `Accept` is exactly `join_room` (joining a room you were invited to
|
||||
/// accepts the invite). `Reject` resolves the invited room and leaves
|
||||
/// it, declining the invite. Either way the resolved invite drops out
|
||||
/// of `get_loose_ends` immediately.
|
||||
pub async fn resolve_invite(
|
||||
client: &Client,
|
||||
room_ref: &str,
|
||||
action: InviteAction,
|
||||
) -> DaemonResponse {
|
||||
match action {
|
||||
InviteAction::Accept => join_room(client, room_ref).await,
|
||||
InviteAction::Reject => {
|
||||
// `resolve_room` -> `get_room` returns the room in any
|
||||
// membership state the store knows, including `Invited`, so
|
||||
// this works for a pending invite that was never joined.
|
||||
let room = match resolve_room(client, room_ref).await {
|
||||
Ok(r) => r,
|
||||
Err(e) => return e,
|
||||
};
|
||||
match room.leave().await {
|
||||
Ok(()) => {
|
||||
refresh_invite_loose_ends(client).await;
|
||||
DaemonResponse::ok(&serde_json::json!({
|
||||
"rejected": true,
|
||||
"room_id": room.room_id().to_string(),
|
||||
}))
|
||||
}
|
||||
Err(e) => DaemonResponse::error(format!("reject invite {room_ref}: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Invite `user_id` to `room_ref`. The calling agent must be a member of
|
||||
/// the room with a power level high enough to invite; otherwise the
|
||||
/// homeserver rejects it and the error is surfaced verbatim.
|
||||
|
|
|
|||
Loading…
Reference in a new issue