Skip to content

Commit

Permalink
implement unstable MSC2666 support for querying mutual rooms
Browse files Browse the repository at this point in the history
matrix-org/matrix-spec-proposals#2666

Signed-off-by: strawberry <strawberry@puppygock.gay>
  • Loading branch information
girlbossceo committed Apr 9, 2024
1 parent 0dc3ace commit 85814e9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,16 @@ features = [
"federation-api",
"push-gateway-api-c",
"state-res",
"unstable-msc2448",
"unstable-msc3575",
"unstable-exhaustive-types",
"ring-compat",
"unstable-unspecified",
"unstable-msc2448",
"unstable-msc2666",
"unstable-msc2867",
"unstable-msc2870",
"unstable-msc3026",
"unstable-msc3061",
"unstable-msc3575",
"unstable-msc4121",
"unstable-extensible-events",
]
Expand Down
2 changes: 2 additions & 0 deletions src/api/client_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod thirdparty;
mod threads;
mod to_device;
mod typing;
mod unstable;
mod unversioned;
mod user_directory;
mod voip;
Expand Down Expand Up @@ -64,6 +65,7 @@ pub use thirdparty::*;
pub use threads::*;
pub use to_device::*;
pub use typing::*;
pub use unstable::*;
pub use unversioned::*;
pub use user_directory::*;
pub use voip::*;
Expand Down
45 changes: 45 additions & 0 deletions src/api/client_server/unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use ruma::{
api::client::{error::ErrorKind, membership::mutual_rooms},
OwnedRoomId,
};

use crate::{services, Error, Result, Ruma};

/// # `GET /_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms`
///
/// Gets all the rooms the sender shares with the specified user.
///
/// TODO: Implement pagination, currently this just returns everything
///
/// An implementation of [MSC2666](https://github.com/matrix-org/matrix-spec-proposals/pull/2666)
pub async fn get_mutual_rooms_route(
body: Ruma<mutual_rooms::unstable::Request>,
) -> Result<mutual_rooms::unstable::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");

if sender_user == &body.user_id {
return Err(Error::BadRequest(
ErrorKind::Unknown,
"You cannot request rooms in common with yourself.",
));
}

if !services().users.exists(&body.user_id)? {
return Ok(mutual_rooms::unstable::Response {
joined: vec![],
next_batch_token: None,
});
}

let mutual_rooms: Vec<OwnedRoomId> = services()
.rooms
.user
.get_shared_rooms(vec![sender_user.clone(), body.user_id.clone()])?
.filter_map(Result::ok)
.collect();

Ok(mutual_rooms::unstable::Response {
joined: mutual_rooms,
next_batch_token: None,
})
}
3 changes: 2 additions & 1 deletion src/api/client_server/unversioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ pub async fn get_supported_versions_route(
unstable_features: BTreeMap::from_iter([
("org.matrix.e2e_cross_signing".to_owned(), true),
("org.matrix.msc2285.stable".to_owned(), true),
("uk.half-shot.msc2666.query_mutual_rooms".to_owned(), true),
("org.matrix.msc2836".to_owned(), true),
("org.matrix.msc2946".to_owned(), true),
("org.matrix.msc3827".to_owned(), true),
("org.matrix.msc3026.busy_presence".to_owned(), true),
("org.matrix.msc3827".to_owned(), true),
]),
};

Expand Down
1 change: 1 addition & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub fn routes() -> Router {
.ruma_route(server_server::get_keys_route)
.ruma_route(server_server::claim_keys_route)
.ruma_route(server_server::get_hierarchy_route)
.ruma_route(client_server::get_mutual_rooms_route)
.ruma_route(client_server::well_known_support)
.route("/_conduwuit/server_version", get(client_server::conduwuit_server_version))
.route("/_matrix/client/r0/rooms/:room_id/initialSync", get(initial_sync))
Expand Down

0 comments on commit 85814e9

Please sign in to comment.