Compare commits

1 Commits

Author SHA1 Message Date
Ralf Zerres
714ca73fc5 feat (msc3266): Room Summary API 2025-05-20 16:25:33 +00:00
7 changed files with 475 additions and 521 deletions

858
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -172,7 +172,7 @@ features = [
"ring-compat",
"state-res",
"unstable-msc2448",
#[cfg(feature = "unstable-msc3266")]
#[cfg(feature = "msc3266")]
"unstable-msc3266",
"unstable-msc4186",
]
@@ -191,12 +191,11 @@ nix = { version = "0.30", features = ["resource"] }
backend_rocksdb = ["rocksdb"]
backend_sqlite = ["sqlite"]
conduit_bin = ["axum"]
default = ["backend_rocksdb", "backend_sqlite", "conduit_bin", "systemd", "unstable-msc3266"]
default = ["backend_rocksdb", "backend_sqlite", "conduit_bin", "proposal_msc3266", "systemd"]
jemalloc = ["tikv-jemallocator"]
sqlite = ["parking_lot", "rusqlite", "tokio/signal"]
systemd = ["sd-notify"]
#unstable-features = ["unstable-msc3266"]
unstable-msc3266 = []
proposal_msc3266 = ["msc3266"]
[[bin]]
name = "conduit"

View File

@@ -25,7 +25,7 @@ mod search;
mod session;
mod space;
mod state;
#[cfg(feature = "unstable-msc3266")]
#[cfg(feature = "msc3266")]
mod summary;
mod sync;
mod tag;
@@ -64,7 +64,7 @@ pub use room::*;
pub use search::*;
pub use session::*;
pub use space::*;
#[cfg(feature = "unstable-msc3266")]
#[cfg(feature = "msc3266")]
pub use summary::*;
pub use state::*;
pub use sync::*;

View File

@@ -4,9 +4,10 @@
use std::str::FromStr;
use crate::{service::rooms::summary::SummaryRequest, services, Error, Result, Ruma};
//use crate::{service::rooms::spaces::PagnationToken, services, Error, Result, Ruma};
use crate::{service::rooms::::PagnationToken, services, Error, Result, Ruma};
use ruma::{
api::client::{error::ErrorKind, room::get_summary},
api::client::{error::ErrorKind, summary::get_summary, space::get_hierarchy},
UInt,
};
@@ -38,21 +39,47 @@ use ruma::{
/// since the server may not have access to the current state of the room.
pub async fn get_room_summary_route(
body: Ruma<get_summary::msc3266::Request>,
) -> Result<get_summary::msc3266::Response> {
let room_id_or_alias = <OwnedRoomOrAliasId as AsRef<T>>(&body
.room_id_or_alias)
body: Ruma<get_summary::v1::Request>,
) -> Result<get_summary::v1::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let limit = body
.limit
.unwrap_or(UInt::from(10_u32))
.min(UInt::from(100_u32));
let via = body
.via
.unwrap_or(UInt::from(3_u32))
.min(UInt::from(10_u32));
//services()
// .rooms
// .summary
// .get_room_summary(
// &body.room_id_or_alias,
// &body.via,
// )
let key = body
.from
.as_ref()
.and_then(|s| PagnationToken::from_str(s).ok());
// Should prevent unexpected behaviour in (bad) clients
if let Some(token) = &key {
if token.suggested_only != body.suggested_only || token.max_depth != max_depth {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"suggested_only and max_depth cannot change on paginated requests",
));
}
}
services()
.rooms
.spaces
.get_client_hierarchy(
sender_user,
&body.room_id,
usize::try_from(limit)
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Limit is too great"))?,
key.map_or(vec![], |token| token.short_room_ids),
usize::try_from(max_depth).map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Max depth is too great")
})?,
body.suggested_only,
)
.await
}

View File

@@ -456,7 +456,7 @@ fn routes(config: &Config) -> Router {
.ruma_route(client_server::get_relating_events_with_rel_type_and_event_type_route)
.ruma_route(client_server::get_relating_events_with_rel_type_route)
.ruma_route(client_server::get_relating_events_route)
#[cfg(feature = "unstable-msc3266")]
#cfg[(feature = "msc3266")]
.ruma_route(client_server::get_room_summary_route)
.ruma_route(client_server::get_hierarchy_route)
.ruma_route(client_server::well_known_client)
@@ -507,7 +507,7 @@ fn routes(config: &Config) -> Router {
.ruma_route(server_server::get_keys_route)
.ruma_route(server_server::claim_keys_route)
.ruma_route(server_server::get_openid_userinfo_route)
#[cfg(feature = "unstable-msc3266")]
#cfg[(feature = "msc3266")]
.ruma_route(client_server::get_room_summary_route)
.ruma_route(server_server::get_hierarchy_route)
.ruma_route(server_server::well_known_server)

View File

@@ -11,7 +11,7 @@ pub mod pdu_metadata;
pub mod search;
pub mod short;
pub mod spaces;
#[cfg(feature = "unstable-msc3266")]
#cfg[(feature = "msc3266")]
pub mod summary;
pub mod state;
pub mod state_accessor;

View File

@@ -14,10 +14,9 @@ use ruma::{
client::{
self,
error::ErrorKind,
room::get_summary,
room::summary::get_summary
},
},
directory::PublicRoomJoinRule,
events::{
room::{
avatar::RoomAvatarEventContent,
@@ -40,10 +39,13 @@ use crate::{services, Error, Result};
#[derive(Debug, PartialEq)]
pub struct SummaryRequest {
/// The Alias or ID of the room to be summarized.
#[ruma_api(path)]
pub room_id_or_alias: Vec<OwnedRoomOrAliasId>,
/// A list of servers the homeserver should attempt to use to peek at the room.
///
/// Defaults to an empty `Vec`.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[ruma_api(query)]
pub via: Vec<OwnedServerName>,
}
@@ -52,38 +54,23 @@ impl Display for SummaryRequest {
write!(
f,
"{}_{}",
self.room_id_or_alias,
self.via
self.room_id_or_alias
.iter()
.map(|b| b.to_string())
.collect::<Vec<_>>()
.join(","),
self.via
)
}
}
/// Identifier used to check if rooms are accessible
///
/// None is used if you want to return the room, no matter if accessible or not
pub enum Identifier<'a> {
UserId(&'a UserId),
ServerName(&'a ServerName),
}
impl Service {
///Gets the response for the room summary request
pub async fn get_room_summary_route(
&self,
room_id_or_alias: &OwnedRoomOrAliasId,
via: &Vec<OwnedServerName>,
) -> Result<client::room::get_summary::msc3266::Response> {
}
}
//impl Service {
//}
/// With the given identifier, checks if a room is accessible
fn is_accessible(
current_room: &OwnedRoomId,
join_rule: &JoinRule,
join_rule: &RoomJoinRule,
identifier: &Identifier<'_>,
allowed_room_ids: &Vec<OwnedRoomId>,
) -> bool {
@@ -119,8 +106,38 @@ fn is_accessible(
}
} // Takes care of join rules
match join_rule {
PublicRoomJoinRule::Public
| PublicRoomJoinRule::Knock => true,
PublicRoomJoinRule::Restricted => {
SpaceRoomJoinRule::Restricted => {
for room in allowed_room_ids {
match identifier {
Identifier::UserId(user) => {
if services()
.rooms
.state_cache
.is_joined(user, room)
.unwrap_or_default()
{
return true;
}
}
Identifier::ServerName(server) => {
if services()
.rooms
.state_cache
.server_in_room(server, room)
.unwrap_or_default()
{
return true;
}
}
}
}
false
}
SpaceRoomJoinRule::Public
| SpaceRoomJoinRule::Knock
| SpaceRoomJoinRule::KnockRestricted => true,
SpaceRoomJoinRule::Invite | SpaceRoomJoinRule::Private => false,
// Custom join rule
_ => false,
}
@@ -141,6 +158,7 @@ mod tests {
.as_str(),
"#conduit:example.com"
);
}
#[test]
@@ -151,6 +169,7 @@ mod tests {
.as_str(),
"!29fhd83h92h0:example.com"
);
}
#[test]
@@ -171,7 +190,6 @@ mod tests {
SummaryRequest.try_from(),
"!29fhd83h92h0:example.com_matrix.org,my_homeserver.org"
);
}
fn valid_room_summary_for_room_alias() {
assert_eq!(