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

feat(ui): RoomListService::room is no longer async! #3551

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ impl RoomListService {
})))
}

async fn room(&self, room_id: String) -> Result<Arc<RoomListItem>, RoomListError> {
fn room(&self, room_id: String) -> Result<Arc<RoomListItem>, RoomListError> {
let room_id = <&RoomId>::try_from(room_id.as_str()).map_err(RoomListError::from)?;

Ok(Arc::new(RoomListItem {
inner: Arc::new(self.inner.room(room_id).await?),
inner: Arc::new(self.inner.room(room_id)?),
utd_hook: self.utd_hook.clone(),
}))
}
Expand Down Expand Up @@ -172,7 +172,7 @@ pub struct RoomList {
inner: Arc<matrix_sdk_ui::room_list_service::RoomList>,
}

#[uniffi::export(async_runtime = "tokio")]
#[uniffi::export]
impl RoomList {
fn loading_state(
&self,
Expand Down Expand Up @@ -233,8 +233,8 @@ impl RoomList {
}
}

async fn room(&self, room_id: String) -> Result<Arc<RoomListItem>, RoomListError> {
self.room_list_service.room(room_id).await
fn room(&self, room_id: String) -> Result<Arc<RoomListItem>, RoomListError> {
self.room_list_service.room(room_id)
}
}

Expand Down
18 changes: 10 additions & 8 deletions crates/matrix-sdk-ui/src/room_list_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ mod room;
mod room_list;
mod state;

use std::{future::ready, num::NonZeroUsize, sync::Arc, time::Duration};
use std::{
future::ready,
num::NonZeroUsize,
sync::{Arc, RwLock},
time::Duration,
};

use async_stream::stream;
use eyeball::{SharedObservable, Subscriber};
Expand All @@ -90,10 +95,7 @@ use ruma::{
};
pub use state::*;
use thiserror::Error;
use tokio::{
sync::{Mutex, RwLock},
time::timeout,
};
use tokio::{sync::Mutex, time::timeout};

use crate::timeline;

Expand Down Expand Up @@ -424,9 +426,9 @@ impl RoomListService {
}

/// Get a [`Room`] if it exists.
pub async fn room(&self, room_id: &RoomId) -> Result<Room, Error> {
pub fn room(&self, room_id: &RoomId) -> Result<Room, Error> {
{
let rooms = self.rooms.read().await;
let rooms = self.rooms.read().unwrap();

if let Some(room) = rooms.iter().rfind(|room| room.id() == room_id) {
return Ok(room.clone());
Expand All @@ -435,7 +437,7 @@ impl RoomListService {

let room = Room::new(&self.client, room_id, &self.sliding_sync)?;

self.rooms.write().await.push(room.clone());
self.rooms.write().unwrap().push(room.clone());
Hywan marked this conversation as resolved.
Show resolved Hide resolved

Ok(room)
}
Expand Down
16 changes: 8 additions & 8 deletions crates/matrix-sdk-ui/tests/integration/room_list_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,15 +2072,15 @@ async fn test_room() -> Result<(), Error> {
},
};

let room0 = room_list.room(room_id_0).await?;
let room0 = room_list.room(room_id_0)?;

// Room has received a name from sliding sync.
assert_eq!(room0.cached_display_name(), Some("Room #0".to_owned()));

// Room has received an avatar from sliding sync.
assert_eq!(room0.avatar_url(), Some(mxc_uri!("mxc://homeserver/media").to_owned()));

let room1 = room_list.room(room_id_1).await?;
let room1 = room_list.room(room_id_1)?;

// Room has not received a name from sliding sync, then it's calculated.
assert_eq!(room1.cached_display_name(), Some("Empty Room".to_owned()));
Expand Down Expand Up @@ -2144,7 +2144,7 @@ async fn test_room_not_found() -> Result<(), Error> {
let room_id = room_id!("!foo:bar.org");

assert_matches!(
room_list.room(room_id).await,
room_list.room(room_id),
Err(Error::RoomNotFound(error_room_id)) => {
assert_eq!(error_room_id, room_id.to_owned());
}
Expand Down Expand Up @@ -2208,7 +2208,7 @@ async fn test_room_subscription() -> Result<(), Error> {
},
};

let room1 = room_list.room(room_id_1).await.unwrap();
let room1 = room_list.room(room_id_1).unwrap();

// Subscribe.

Expand Down Expand Up @@ -2253,7 +2253,7 @@ async fn test_room_subscription() -> Result<(), Error> {
// Unsubscribe.

room1.unsubscribe();
room_list.room(room_id_2).await?.unsubscribe(); // unsubscribe from a room that has no subscription.
room_list.room(room_id_2)?.unsubscribe(); // unsubscribe from a room that has no subscription.

sync_then_assert_request_and_fake_response! {
[server, room_list, sync]
Expand Down Expand Up @@ -2316,7 +2316,7 @@ async fn test_room_unread_notifications() -> Result<(), Error> {
},
};

let room = room_list.room(room_id).await.unwrap();
let room = room_list.room(room_id).unwrap();

assert_matches!(
room.unread_notification_counts(),
Expand Down Expand Up @@ -2391,7 +2391,7 @@ async fn test_room_timeline() -> Result<(), Error> {
},
};

let room = room_list.room(room_id).await?;
let room = room_list.room(room_id)?;
room.init_timeline_with_builder(room.default_room_timeline_builder().await.unwrap()).await?;
let timeline = room.timeline().unwrap();

Expand Down Expand Up @@ -2473,7 +2473,7 @@ async fn test_room_latest_event() -> Result<(), Error> {
},
};

let room = room_list.room(room_id).await?;
let room = room_list.room(room_id)?;
room.init_timeline_with_builder(room.default_room_timeline_builder().await.unwrap()).await?;

// The latest event does not exist.
Expand Down
2 changes: 1 addition & 1 deletion labs/multiverse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl App {
all_rooms.into_iter().filter(|room_id| !previous_ui_rooms.contains_key(room_id))
{
// Retrieve the room list service's Room.
let Ok(ui_room) = sync_service.room_list_service().room(&room_id).await else {
let Ok(ui_room) = sync_service.room_list_service().room(&room_id) else {
error!("error when retrieving room after an update");
continue;
};
Expand Down
Loading