Skip to content

Commit

Permalink
refactor(room): rename RequestToJoin and related code to JoinRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Dec 10, 2024
1 parent 848142a commit 5068a0e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 61 deletions.
22 changes: 11 additions & 11 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,11 @@ impl Room {
/// The current requests to join the room will be emitted immediately
/// when subscribing, along with a [`TaskHandle`] to cancel the
/// subscription.
pub async fn subscribe_to_requests_to_join(
pub async fn subscribe_to_join_requests(
self: Arc<Self>,
listener: Box<dyn RequestsToJoinListener>,
) -> Result<Arc<TaskHandle>, ClientError> {
let stream = self.inner.subscribe_to_requests_to_join().await?;
let stream = self.inner.subscribe_to_join_requests().await?;

let handle = Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
pin_mut!(stream);
Expand All @@ -906,8 +906,8 @@ impl Room {
}
}

impl From<matrix_sdk::room::request_to_join::RequestToJoinRoom> for RequestToJoin {
fn from(request: matrix_sdk::room::request_to_join::RequestToJoinRoom) -> Self {
impl From<matrix_sdk::room::request_to_join::JoinRequest> for JoinRequest {
fn from(request: matrix_sdk::room::request_to_join::JoinRequest) -> Self {
Self {
event_id: request.event_id.to_string(),
user_id: request.member_info.user_id.to_string(),
Expand All @@ -917,20 +917,20 @@ impl From<matrix_sdk::room::request_to_join::RequestToJoinRoom> for RequestToJoi
reason: request.member_info.reason.clone(),
timestamp: request.timestamp.map(|ts| ts.into()),
is_seen: request.is_seen,
actions: Arc::new(RequestToJoinActions { inner: request }),
actions: Arc::new(JoinRequestActions { inner: request }),
}
}
}

/// A listener for receiving new requests to a join a room.
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait RequestsToJoinListener: Send + Sync {
fn call(&self, requests_to_join: Vec<RequestToJoin>);
fn call(&self, join_requests: Vec<JoinRequest>);
}

/// An FFI representation of a request to join a room.
#[derive(Debug, Clone, uniffi::Record)]
pub struct RequestToJoin {
pub struct JoinRequest {
/// The event id of the event that contains the `knock` membership change.
pub event_id: String,
/// The user id of the user who's requesting to join the room.
Expand All @@ -949,17 +949,17 @@ pub struct RequestToJoin {
/// filtered by the client.
pub is_seen: bool,
/// A set of actions to perform for this request to join.
pub actions: Arc<RequestToJoinActions>,
pub actions: Arc<JoinRequestActions>,
}

/// A set of actions to perform for a request to join.
#[derive(Debug, Clone, uniffi::Object)]
pub struct RequestToJoinActions {
inner: matrix_sdk::room::request_to_join::RequestToJoinRoom,
pub struct JoinRequestActions {
inner: matrix_sdk::room::request_to_join::JoinRequest,
}

#[matrix_sdk_ffi_macros::export]
impl RequestToJoinActions {
impl JoinRequestActions {
/// Accepts the request to join by inviting the user to the room.
pub async fn accept(&self) -> Result<(), ClientError> {
self.inner.accept().await.map_err(Into::into)
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub struct Room {
pub latest_encrypted_events: Arc<SyncRwLock<RingBuffer<Raw<AnySyncTimelineEvent>>>>,

/// The event ids for seen request to join room events.
pub seen_requests_to_join_ids: SharedObservable<Option<HashSet<OwnedEventId>>>,
pub seen_join_request_ids: SharedObservable<Option<HashSet<OwnedEventId>>>,
}

/// The room summary containing member counts and members that should be used to
Expand Down Expand Up @@ -275,7 +275,7 @@ impl Room {
Self::MAX_ENCRYPTED_EVENTS,
))),
room_info_notable_update_sender,
seen_requests_to_join_ids: SharedObservable::new(None),
seen_join_request_ids: SharedObservable::new(None),
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-base/src/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ impl StateStore for MemoryStore {
.get(room_id)
.cloned()
.map(StateStoreDataValue::ComposerDraft),
StateStoreDataKey::SeenRequestsToJoin(room_id) => self
StateStoreDataKey::SeenJoinRequests(room_id) => self
.seen_requests_to_join
.read()
.unwrap()
.get(room_id)
.cloned()
.map(StateStoreDataValue::SeenRequestsToJoin),
.map(StateStoreDataValue::SeenJoinRequests),
})
}

Expand Down Expand Up @@ -247,7 +247,7 @@ impl StateStore for MemoryStore {
.expect("Session data not containing server capabilities"),
);
}
StateStoreDataKey::SeenRequestsToJoin(room_id) => {
StateStoreDataKey::SeenJoinRequests(room_id) => {
self.seen_requests_to_join.write().unwrap().insert(
room_id.to_owned(),
value
Expand Down Expand Up @@ -281,7 +281,7 @@ impl StateStore for MemoryStore {
StateStoreDataKey::ComposerDraft(room_id) => {
self.composer_drafts.write().unwrap().remove(room_id);
}
StateStoreDataKey::SeenRequestsToJoin(room_id) => {
StateStoreDataKey::SeenJoinRequests(room_id) => {
self.seen_requests_to_join.write().unwrap().remove(room_id);
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-base/src/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ pub enum StateStoreDataValue {
ComposerDraft(ComposerDraft),

/// A list of requests to join marked as seen in a room.
SeenRequestsToJoin(HashSet<OwnedEventId>),
SeenJoinRequests(HashSet<OwnedEventId>),
}

/// Current draft of the composer for the room.
Expand Down Expand Up @@ -1094,7 +1094,7 @@ impl StateStoreDataValue {

/// Get this value if it is the data for the ignored join requests.
pub fn into_seen_join_requests(self) -> Option<HashSet<OwnedEventId>> {
as_variant!(self, Self::SeenRequestsToJoin)
as_variant!(self, Self::SeenJoinRequests)
}
}

Expand Down Expand Up @@ -1127,7 +1127,7 @@ pub enum StateStoreDataKey<'a> {
ComposerDraft(&'a RoomId),

/// A list of requests to join in a room marked as seen.
SeenRequestsToJoin(&'a RoomId),
SeenJoinRequests(&'a RoomId),
}

impl StateStoreDataKey<'_> {
Expand Down Expand Up @@ -1155,7 +1155,7 @@ impl StateStoreDataKey<'_> {
pub const COMPOSER_DRAFT: &'static str = "composer_draft";

/// Key prefix to use for the
/// [`SeenRequestsToJoin`][Self::SeenRequestsToJoin] variant.
/// [`SeenJoinRequests`][Self::SeenJoinRequests] variant.
pub const SEEN_REQUESTS_TO_JOIN: &'static str = "seen_requests_to_join";
}

Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-indexeddb/src/state_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl IndexeddbStateStore {
StateStoreDataKey::ComposerDraft(room_id) => {
self.encode_key(keys::KV, (StateStoreDataKey::COMPOSER_DRAFT, room_id))
}
StateStoreDataKey::SeenRequestsToJoin(room_id) => {
StateStoreDataKey::SeenJoinRequests(room_id) => {
self.encode_key(keys::KV, (StateStoreDataKey::SEEN_REQUESTS_TO_JOIN, room_id))
}
}
Expand Down Expand Up @@ -540,10 +540,10 @@ impl_state_store!({
.map(|f| self.deserialize_value::<ComposerDraft>(&f))
.transpose()?
.map(StateStoreDataValue::ComposerDraft),
StateStoreDataKey::SeenRequestsToJoin(_) => value
StateStoreDataKey::SeenJoinRequests(_) => value
.map(|f| self.deserialize_value::<HashSet<OwnedEventId>>(&f))
.transpose()?
.map(StateStoreDataValue::SeenRequestsToJoin),
.map(StateStoreDataValue::SeenJoinRequests),
};

Ok(value)
Expand Down Expand Up @@ -581,7 +581,7 @@ impl_state_store!({
StateStoreDataKey::ComposerDraft(_) => self.serialize_value(
&value.into_composer_draft().expect("Session data not a composer draft"),
),
StateStoreDataKey::SeenRequestsToJoin(_) => self.serialize_value(
StateStoreDataKey::SeenJoinRequests(_) => self.serialize_value(
&value
.into_seen_join_requests()
.expect("Session data not a set of ignored requests to join"),
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-sqlite/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl SqliteStateStore {
StateStoreDataKey::ComposerDraft(room_id) => {
Cow::Owned(format!("{}:{room_id}", StateStoreDataKey::COMPOSER_DRAFT))
}
StateStoreDataKey::SeenRequestsToJoin(room_id) => {
StateStoreDataKey::SeenJoinRequests(room_id) => {
Cow::Owned(format!("{}:{room_id}", StateStoreDataKey::SEEN_REQUESTS_TO_JOIN))
}
};
Expand Down Expand Up @@ -998,8 +998,8 @@ impl StateStore for SqliteStateStore {
StateStoreDataKey::ComposerDraft(_) => {
StateStoreDataValue::ComposerDraft(self.deserialize_value(&data)?)
}
StateStoreDataKey::SeenRequestsToJoin(_) => {
StateStoreDataValue::SeenRequestsToJoin(self.deserialize_value(&data)?)
StateStoreDataKey::SeenJoinRequests(_) => {
StateStoreDataValue::SeenJoinRequests(self.deserialize_value(&data)?)
}
})
})
Expand Down Expand Up @@ -1035,7 +1035,7 @@ impl StateStore for SqliteStateStore {
StateStoreDataKey::ComposerDraft(_) => self.serialize_value(
&value.into_composer_draft().expect("Session data not a composer draft"),
)?,
StateStoreDataKey::SeenRequestsToJoin(_) => self.serialize_value(
StateStoreDataKey::SeenJoinRequests(_) => self.serialize_value(
&value
.into_seen_join_requests()
.expect("Session data not a set of ignored requests to join"),
Expand Down
52 changes: 25 additions & 27 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ use crate::{
notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode},
room::{
power_levels::{RoomPowerLevelChanges, RoomPowerLevelsExt},
request_to_join::RequestToJoinRoom,
request_to_join::JoinRequest,
},
sync::RoomUpdate,
utils::{IntoRawMessageLikeEventContent, IntoRawStateEventContent},
Expand Down Expand Up @@ -3210,20 +3210,18 @@ impl Room {

/// Helper to requests to join this `Room`. It returns both a list with the
/// initial items and any new request to join received.
pub async fn subscribe_to_requests_to_join(
&self,
) -> Result<impl Stream<Item = Vec<RequestToJoinRoom>>> {
pub async fn subscribe_to_join_requests(&self) -> Result<impl Stream<Item = Vec<JoinRequest>>> {
let this = Arc::new(self.clone());

let requests_observable =
this.client.observe_room_events::<SyncRoomMemberEvent, (Client, Room)>(this.room_id());

let (current_seen_ids, mut seen_request_ids_stream) =
this.subscribe_to_seen_requests_to_join_ids().await?;
this.subscribe_to_seen_join_request_ids().await?;

let combined_stream = stream! {
// Emit current requests to join
match this.clone().get_current_requests_to_join(&current_seen_ids).await {
match this.clone().get_current_join_requests(&current_seen_ids).await {
Ok(initial_requests) => yield initial_requests,
Err(e) => warn!("Failed to get initial requests to join: {e:?}")
}
Expand Down Expand Up @@ -3260,7 +3258,7 @@ impl Room {
MembershipChange::KnockAccepted |
MembershipChange::KnockDenied |
MembershipChange::KnockRetracted => {
match this.clone().get_current_requests_to_join(&seen_ids).await {
match this.clone().get_current_join_requests(&seen_ids).await {
Ok(requests) => yield requests,
Err(e) => {
warn!("Failed to get updated requests to join on membership change: {e:?}")
Expand All @@ -3272,7 +3270,7 @@ impl Room {
} else {
// If we can't calculate the membership change, assume we need to
// emit updated values
match this.clone().get_current_requests_to_join(&seen_ids).await {
match this.clone().get_current_join_requests(&seen_ids).await {
Ok(requests) => yield requests,
Err(e) => {
warn!("Failed to get updated requests to join on new member event: {e:?}")
Expand All @@ -3282,7 +3280,7 @@ impl Room {
} else if has_new_seen_ids {
// If seen requests have changed, we need to recalculate all the
// requests to join
match this.clone().get_current_requests_to_join(&seen_ids).await {
match this.clone().get_current_join_requests(&seen_ids).await {
Ok(requests) => yield requests,
Err(e) => {
warn!("Failed to get updated requests to join on seen ids changed: {e:?}")
Expand All @@ -3295,18 +3293,18 @@ impl Room {
Ok(combined_stream)
}

async fn get_current_requests_to_join(
async fn get_current_join_requests(
self: Arc<Self>,
seen_request_ids: &HashSet<OwnedEventId>,
) -> Result<Vec<RequestToJoinRoom>> {
) -> Result<Vec<JoinRequest>> {
Ok(self
.members(RoomMemberships::KNOCK)
.await?
.into_iter()
.filter_map(|member| {
if let Some(event_id) = member.event().event_id() {
let event_id = event_id.to_owned();
Some(RequestToJoinRoom::new(
Some(JoinRequest::new(
self.clone(),
&event_id,
member.event().timestamp(),
Expand All @@ -3322,54 +3320,54 @@ impl Room {

/// Mark a list of requests to join the room as seen, given their state
/// event ids.
pub async fn mark_requests_to_join_as_seen(&self, event_ids: &[OwnedEventId]) -> Result<()> {
let mut current_seen_events = self.get_seen_requests_to_join_ids().await?;
pub async fn mark_join_requests_as_seen(&self, event_ids: &[OwnedEventId]) -> Result<()> {
let mut current_seen_events = self.get_seen_join_requests().await?;

for event_id in event_ids {
current_seen_events.insert(event_id.to_owned());
}

self.seen_requests_to_join_ids.set(Some(current_seen_events.clone()));
self.seen_join_request_ids.set(Some(current_seen_events.clone()));

self.client
.store()
.set_kv_data(
StateStoreDataKey::SeenRequestsToJoin(self.room_id()),
StateStoreDataValue::SeenRequestsToJoin(current_seen_events),
StateStoreDataKey::SeenJoinRequests(self.room_id()),
StateStoreDataValue::SeenJoinRequests(current_seen_events),
)
.await
.map_err(Into::into)
}

/// Get the list of seen requests to join event ids in this room.
pub async fn get_seen_requests_to_join_ids(&self) -> Result<HashSet<OwnedEventId>> {
let current_requests_to_join_ids = self.seen_requests_to_join_ids.get();
let current_requests_to_join_ids: HashSet<OwnedEventId> =
if let Some(requests) = current_requests_to_join_ids.as_ref() {
pub async fn get_seen_join_requests(&self) -> Result<HashSet<OwnedEventId>> {
let current_join_request_ids = self.seen_join_request_ids.get();
let current_join_request_ids: HashSet<OwnedEventId> =
if let Some(requests) = current_join_request_ids.as_ref() {
requests.clone()
} else {
let requests = self
.client
.store()
.get_kv_data(StateStoreDataKey::SeenRequestsToJoin(self.room_id()))
.get_kv_data(StateStoreDataKey::SeenJoinRequests(self.room_id()))
.await?
.and_then(|v| v.into_seen_join_requests())
.unwrap_or_default();

self.seen_requests_to_join_ids.set(Some(requests.clone()));
self.seen_join_request_ids.set(Some(requests.clone()));
requests
};
Ok(current_requests_to_join_ids)
Ok(current_join_request_ids)
}

/// Subscribes to the set of requests to join that have been marked as
/// 'seen'.
pub async fn subscribe_to_seen_requests_to_join_ids(
pub async fn subscribe_to_seen_join_request_ids(
&self,
) -> Result<(HashSet<OwnedEventId>, impl Stream<Item = HashSet<OwnedEventId>>)> {
let current = self.get_seen_requests_to_join_ids().await?;
let current = self.get_seen_join_requests().await?;
let subscriber =
self.seen_requests_to_join_ids.subscribe().map(|values| values.unwrap_or_default());
self.seen_join_request_ids.subscribe().map(|values| values.unwrap_or_default());
Ok((current, subscriber))
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk/src/room/request_to_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{room::RoomMember, Error, Room};

/// A request to join a room with `knock` join rule.
#[derive(Debug, Clone)]
pub struct RequestToJoinRoom {
pub struct JoinRequest {
room: Arc<Room>,
/// The event id of the event containing knock membership change.
pub event_id: OwnedEventId,
Expand All @@ -19,7 +19,7 @@ pub struct RequestToJoinRoom {
pub is_seen: bool,
}

impl RequestToJoinRoom {
impl JoinRequest {
pub(crate) fn new(
room: Arc<Room>,
event_id: &EventId,
Expand All @@ -38,7 +38,7 @@ impl RequestToJoinRoom {
/// Marks the request to join as 'seen' so the client can ignore it in the
/// future.
pub async fn mark_as_seen(&mut self) -> Result<(), Error> {
self.room.mark_requests_to_join_as_seen(&[self.event_id.to_owned()]).await?;
self.room.mark_join_requests_as_seen(&[self.event_id.to_owned()]).await?;
Ok(())
}

Expand Down
Loading

0 comments on commit 5068a0e

Please sign in to comment.