feat (msc3266): Room Summary API

This commit is contained in:
Ralf Zerres
2025-05-20 16:25:33 +00:00
parent ff7b2af80d
commit 4710a974c4
7 changed files with 739 additions and 391 deletions

870
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -172,6 +172,8 @@ features = [
"ring-compat",
"state-res",
"unstable-msc2448",
#[cfg(feature = "unstable-msc3266")]
"unstable-msc3266",
"unstable-msc4186",
]
git = "https://github.com/ruma/ruma.git"
@@ -189,10 +191,12 @@ nix = { version = "0.30", features = ["resource"] }
backend_rocksdb = ["rocksdb"]
backend_sqlite = ["sqlite"]
conduit_bin = ["axum"]
default = ["backend_rocksdb", "backend_sqlite", "conduit_bin", "systemd"]
default = ["backend_rocksdb", "backend_sqlite", "conduit_bin", "systemd", "unstable-msc3266"]
jemalloc = ["tikv-jemallocator"]
sqlite = ["parking_lot", "rusqlite", "tokio/signal"]
systemd = ["sd-notify"]
#unstable-features = ["unstable-msc3266"]
unstable-msc3266 = []
[[bin]]
name = "conduit"

View File

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

View File

@@ -0,0 +1,58 @@
//! `MSC3266` ([MSC]) room-summary
//!
//! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3266
use std::str::FromStr;
use crate::{service::rooms::summary::SummaryRequest, services, Error, Result, Ruma};
use ruma::{
api::client::{error::ErrorKind, room::get_summary},
UInt,
};
/// # `GET /_matrix/client/v1/room_summary/{roomIdOrAlias}`
///
/// Retrieves a summary for a room.
///
/// The API returns a summary of the given room identified via its id, or room alias.
///
/// * is either already a member
/// * or has the necessary permissions to join.u
///
/// (For example, the user may be a member of a room mentioned in an allow condition
/// in the join rules of a restricted room.)
///
/// Servers MAY allow unauthenticated access to this API if at least one of
/// the following conditions holds true:
///
/// * The room has a [join rule](#mroomjoin_rules) of `public`, `knock` or
/// `knock_restricted`.
/// * The room has a `world_readable` [history visibility](#room-history-visibility).
///
/// Servers should consider rate limiting federation requests more heavily,
/// if the client is unauthenticated.
///
/// Improvements yet to be defined:
/// Clients should note that requests for rooms where the user's membership
/// is `invite` or `knock` might yield outdated, partial or even no data
/// 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)
let via = body
.via
//services()
// .rooms
// .summary
// .get_room_summary(
// &body.room_id_or_alias,
// &body.via,
// )
.await
}

View File

@@ -456,6 +456,8 @@ 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")]
.ruma_route(client_server::get_room_summary_route)
.ruma_route(client_server::get_hierarchy_route)
.ruma_route(client_server::well_known_client)
.route(
@@ -505,6 +507,8 @@ 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")]
.ruma_route(client_server::get_room_summary_route)
.ruma_route(server_server::get_hierarchy_route)
.ruma_route(server_server::well_known_server)
} else {

View File

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

View File

@@ -0,0 +1,186 @@
//! `MSC3266` ([MSC]) room-summary
//!
//! [MSC]: https://github.com/matrix-org/matrix-spec-proposals/pull/3266
use std::{
collections::VecDeque,
fmt::{Display, Formatter},
str::FromStr,
};
use lru_cache::LruCache;
use ruma::{
api::{
client::{
self,
error::ErrorKind,
room::get_summary,
},
},
directory::PublicRoomJoinRule,
events::{
room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
join_rules::{JoinRule, RoomJoinRulesEventContent},
topic::RoomTopicEventContent,
},
StateEventType,
},
serde::Raw,
OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, RoomId, ServerName,
UInt, UserId,
};
use tokio::sync::Mutex;
use tracing::{debug, error, info, warn};
use crate::{services, Error, Result};
#[derive(Debug, PartialEq)]
pub struct SummaryRequest {
/// The Alias or ID of the room to be summarized.
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`.
pub via: Vec<OwnedServerName>,
}
impl Display for SummaryRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}_{}",
self.room_id_or_alias,
self.via
.iter()
.map(|b| b.to_string())
.collect::<Vec<_>>()
.join(","),
)
}
}
/// 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> {
}
}
/// With the given identifier, checks if a room is accessible
fn is_accessible(
current_room: &OwnedRoomId,
join_rule: &JoinRule,
identifier: &Identifier<'_>,
allowed_room_ids: &Vec<OwnedRoomId>,
) -> bool {
// Note: unwrap_or_default for bool means false
match identifier {
Identifier::ServerName(server_name) => {
let room_id: &RoomId = current_room;
// Checks if ACLs allow for the server to participate
if services()
.rooms
.event_handler
.acl_check(server_name, room_id)
.is_err()
{
return false;
}
}
Identifier::UserId(user_id) => {
if services()
.rooms
.state_cache
.is_joined(user_id, current_room)
.unwrap_or_default()
|| services()
.rooms
.state_cache
.is_invited(user_id, current_room)
.unwrap_or_default()
{
return true;
}
}
} // Takes care of join rules
match join_rule {
PublicRoomJoinRule::Public
| PublicRoomJoinRule::Knock => true,
// Custom join rule
_ => false,
}
}
#[cfg(test)]
mod tests {
use ruma::{
OwnedRoomOrAliasId, RoomOrAliasId};
use crate::IdParseError;
use super::*;
#[test]
fn valid_room_id_or_alias_id_with_a_room_alias_id() {
assert_eq!(
<&RoomOrAliasId>::try_from("#conduit:example.com")
.expect("Failed to create RoomAliasId.")
.as_str(),
"#conduit:example.com"
);
}
#[test]
fn valid_room_id_or_alias_id_with_a_room_id() {
assert_eq!(
<&RoomOrAliasId>::try_from("!29fhd83h92h0:example.com")
.expect("Failed to create RoomId.")
.as_str(),
"!29fhd83h92h0:example.com"
);
}
#[test]
fn missing_sigil_for_room_id_or_alias_id() {
assert_eq!(
<&RoomOrAliasId>::try_from("ruma:example.com").unwrap_err(),
IdParseError::MissingLeadingSigil
);
}
#[test]
fn valid_room_summary_for_room_id() {
assert_eq!(
SummaryRequest {
room_id_or_alias: vec!["!29fhd83h92h0:example.com"],
via: vec!("matrix.org", "my_homeserver.io"),
}
SummaryRequest.try_from(),
"!29fhd83h92h0:example.com_matrix.org,my_homeserver.org"
);
}
fn valid_room_summary_for_room_alias() {
assert_eq!(
SummaryRequest {
room_id_or_alias: vec!["#Admin:example.com"],
via: vec!("matrix.org", "my_homeserver.io"),
}
.to_string(),
"#Admin:example.com_matrix.org,my_homeserver.org"
);
}
}