Files
conduit/src/service/rooms/summary/mod.rs
2025-05-20 19:51:09 +00:00

187 lines
5.0 KiB
Rust

//! `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"
);
}
}