Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

room preview: use the room summary MSC3266 endpoint #3339

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ futures-util = { version = "0.3.26", default-features = false, features = [
http = "0.2.6"
imbl = "2.0.0"
itertools = "0.12.0"
ruma = { git = "https://github.com/ruma/ruma", rev = "4c00bd010dbdca6005bd599b52e90a0b7015d056", features = [
ruma = { git = "https://github.com/ruma/ruma", rev = "21b644ac6ae1c7d4a4f7e98a6481a3318f2deeaa", features = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the original repository since your PR has been merged.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed to use the original repo, which I've done in a commit this morning,… so there's nothing to change, right? 😁

"client-api-c",
"compat-upload-signatures",
"compat-user-id",
"compat-arbitrary-length-ids",
"compat-tag-info",
"unstable-msc3401",
"unstable-msc3266",
] }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "4c00bd010dbdca6005bd599b52e90a0b7015d056" }
ruma-common = { git = "https://github.com/ruma/ruma", rev = "21b644ac6ae1c7d4a4f7e98a6481a3318f2deeaa" }
once_cell = "1.16.0"
rand = "0.8.5"
serde = "1.0.151"
Expand Down
15 changes: 7 additions & 8 deletions bindings/matrix-sdk-ffi/src/room_preview.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use matrix_sdk::{room_preview::RoomPreview as SdkRoomPreview, RoomState};
use ruma::{
events::room::{history_visibility::HistoryVisibility, join_rules::JoinRule},
OwnedRoomId,
};
use ruma::{space::SpaceRoomJoinRule, OwnedRoomId};

/// The preview of a room, be it invited/joined/left, or not.
#[derive(uniffi::Record)]
Expand Down Expand Up @@ -43,12 +40,14 @@ impl RoomPreview {
avatar_url: preview.avatar_url.map(|url| url.to_string()),
num_joined_members: preview.num_joined_members,
room_type: preview.room_type.map(|room_type| room_type.to_string()),
is_history_world_readable: preview.history_visibility
== HistoryVisibility::WorldReadable,
is_history_world_readable: preview.is_world_readable,
is_joined: preview.state.map_or(false, |state| state == RoomState::Joined),
is_invited: preview.state.map_or(false, |state| state == RoomState::Invited),
is_public: preview.join_rule == JoinRule::Public,
can_knock: matches!(preview.join_rule, JoinRule::KnockRestricted(_) | JoinRule::Knock),
is_public: preview.join_rule == SpaceRoomJoinRule::Public,
can_knock: matches!(
preview.join_rule,
SpaceRoomJoinRule::KnockRestricted | SpaceRoomJoinRule::Knock
),
}
}
}
14 changes: 8 additions & 6 deletions crates/matrix-sdk/src/http_client/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use eyeball::SharedObservable;
use http::header::CONTENT_LENGTH;
use reqwest::Certificate;
use ruma::api::{
client::error::{ErrorBody as ClientApiErrorBody, ErrorKind as ClientApiErrorKind},
client::error::{ErrorBody as ClientApiErrorBody, ErrorKind as ClientApiErrorKind, RetryAfter},
error::FromHttpResponseError,
IncomingResponse, OutgoingRequest,
};
Expand Down Expand Up @@ -68,13 +68,15 @@ impl HttpClient {
let status_code = match api_error {
RumaApiError::ClientApi(e) => match e.body {
ClientApiErrorBody::Standard {
kind: ClientApiErrorKind::LimitExceeded { retry_after_ms },
kind: ClientApiErrorKind::LimitExceeded { retry_after },
..
} => {
return RetryError::Transient {
err,
retry_after: retry_after_ms,
};
let retry_after =
retry_after.and_then(|retry_after| match retry_after {
RetryAfter::Delay(d) => Some(d),
RetryAfter::DateTime(_) => None,
});
return RetryError::Transient { err, retry_after };
}
_ => Some(e.status_code),
},
Expand Down
90 changes: 82 additions & 8 deletions crates/matrix-sdk/src/room_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use ruma::{
api::client::{membership::joined_members, state::get_state_events},
events::room::{history_visibility::HistoryVisibility, join_rules::JoinRule},
room::RoomType,
space::SpaceRoomJoinRule,
OwnedMxcUri, OwnedRoomAliasId, RoomId,
};
use tokio::try_join;
Expand Down Expand Up @@ -52,10 +53,11 @@ pub struct RoomPreview {
pub room_type: Option<RoomType>,

/// What's the join rule for this room?
pub join_rule: JoinRule,
pub join_rule: SpaceRoomJoinRule,

/// What's the history visibility for this room?
pub history_visibility: HistoryVisibility,
/// Is the room world-readable (i.e. is its history_visibility set to
/// world_readable)?
pub is_world_readable: bool,

/// Has the current user been invited/joined/left this room?
///
Expand All @@ -79,8 +81,20 @@ impl RoomPreview {
topic: room_info.topic().map(ToOwned::to_owned),
avatar_url: room_info.avatar_url().map(ToOwned::to_owned),
room_type: room_info.room_type().cloned(),
join_rule: room_info.join_rule().clone(),
history_visibility: room_info.history_visibility().clone(),
join_rule: match room_info.join_rule() {
JoinRule::Invite => SpaceRoomJoinRule::Invite,
JoinRule::Knock => SpaceRoomJoinRule::Knock,
JoinRule::Private => SpaceRoomJoinRule::Private,
JoinRule::Restricted(_) => SpaceRoomJoinRule::Restricted,
JoinRule::KnockRestricted(_) => SpaceRoomJoinRule::KnockRestricted,
JoinRule::Public => SpaceRoomJoinRule::Public,
_ => {
// The JoinRule enum is non-exhaustive. Let's do a white lie and pretend it's
// private (a cautious choice).
SpaceRoomJoinRule::Private
}
},
is_world_readable: *room_info.history_visibility() == HistoryVisibility::WorldReadable,

num_joined_members,
state,
Expand All @@ -95,14 +109,72 @@ impl RoomPreview {

#[instrument(skip(client))]
pub(crate) async fn from_unknown(client: &Client, room_id: &RoomId) -> crate::Result<Self> {
// TODO: (optimization) Use the room summary endpoint, if available, as
// described in https://github.com/deepbluev7/matrix-doc/blob/room-summaries/proposals/3266-room-summary.md
// Use the room summary endpoint, if available, as described in
// https://github.com/deepbluev7/matrix-doc/blob/room-summaries/proposals/3266-room-summary.md
match Self::from_room_summary(client, room_id).await {
Ok(res) => return Ok(res),
Err(err) => {
warn!("error when previewing room from the room summary endpoint: {err}");
}
}

// TODO: (optimization) Use the room search directory, if available:
// - if the room directory visibility is public,
// - then use a public room filter set to this room id

// Resort to using the room state endpoint, as well as the joined members one.
Self::from_state_events(client, room_id).await
}

/// Get a [`RoomPreview`] using MSC3266, if available on the remote server.
///
/// Will fail with a 404 if the API is not available.
///
/// This method is exposed for testing purposes; clients should prefer
/// `Client::get_room_preview` in general over this.
pub async fn from_room_summary(client: &Client, room_id: &RoomId) -> crate::Result<Self> {
let request = ruma::api::client::room::get_summary::msc3266::Request::new(
room_id.to_owned().into(),
Vec::new(),
);

let response = client.send(request, None).await?;

// The server returns a `Left` room state for rooms the user has not joined. Be
// more precise than that, and set it to `None` if we haven't joined
// that room.
let state = if client.get_room(room_id).is_none() {
None
} else {
response.membership.map(|membership| RoomState::from(&membership))
};

Ok(RoomPreview {
canonical_alias: response.canonical_alias,
name: response.name,
topic: response.topic,
avatar_url: response.avatar_url,
num_joined_members: response.num_joined_members.into(),
room_type: response.room_type,
join_rule: response.join_rule,
is_world_readable: response.world_readable,
state,
})
}

/// Get a [`RoomPreview`] using the room state endpoint.
///
/// This is always available on a remote server, but will only work if one
/// of these two conditions is true:
///
/// - the user has joined the room at some point (i.e. they're still joined
/// or they've joined
/// it and left it later).
/// - the room has an history visibility set to world-readable.
///
/// This method is exposed for testing purposes; clients should prefer
/// `Client::get_room_preview` in general over this.
pub async fn from_state_events(client: &Client, room_id: &RoomId) -> crate::Result<Self> {
let state_request = get_state_events::v3::Request::new(room_id.to_owned());
let joined_members_request = joined_members::v3::Request::new(room_id.to_owned());

Expand All @@ -128,6 +200,8 @@ impl RoomPreview {
room_info.handle_state_event(&ev.into());
}

Ok(Self::from_room_info(room_info, num_joined_members, None))
let state = client.get_room(room_id).map(|room| room.state());

Ok(Self::from_room_info(room_info, num_joined_members, state))
}
}
Loading
Loading