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

sdk: Room Directory Search #3169

Merged
merged 37 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
18c155b
feat: room_directory_sync basic sync loop
Velin92 Feb 22, 2024
8ac6845
feat: paginated public room search
Velin92 Feb 26, 2024
a79c528
improved the tests
Velin92 Feb 27, 2024
26b0b32
feat(bindings): ffi layer started implementation
Velin92 Feb 27, 2024
d9231be
feat(bindings): listener code
Velin92 Feb 27, 2024
caa9a7d
tests: code improvement for the filter integration test
Velin92 Feb 27, 2024
37d9557
test: fixed a test by a adding a small delay
Velin92 Feb 27, 2024
9c33540
tests: improved tests and added a unit test
Velin92 Feb 28, 2024
690ed46
tests: unit tests have been completed
Velin92 Feb 28, 2024
3e35f16
docs: updated documentation
Velin92 Feb 28, 2024
70466aa
docs: more documentation for types and the mod
Velin92 Feb 28, 2024
4dd7c30
tests: improved the tests by adding the limit
Velin92 Feb 28, 2024
4b1eefc
Apply suggestions from code review
Velin92 Feb 29, 2024
2163ab0
tests: test improvements and added a new test
Velin92 Feb 29, 2024
2e3ced1
docs: more documentation
Velin92 Feb 29, 2024
8890bf3
feat(bindings): improved and fixed ffi code
Velin92 Feb 29, 2024
b2b9b5f
feat(bindings): reverted some code from ffi
Velin92 Feb 29, 2024
77ba301
Merge branch 'main' into mauroromito/directory_search
Velin92 Feb 29, 2024
2abe3ab
improved docs
Velin92 Mar 1, 2024
ad1623d
docs: fixed docs
Velin92 Mar 1, 2024
2f7b2f0
fix: fmt
Velin92 Mar 1, 2024
9fbc2ab
mocking library not supported on wasm
Velin92 Mar 1, 2024
eb0ddbc
Merge branch 'main' into mauroromito/directory_search
Velin92 Mar 1, 2024
e922a58
tests: fix fmt
Velin92 Mar 1, 2024
4db6964
Merge branch 'mauroromito/directory_search' of github.com:matrix-org/…
Velin92 Mar 1, 2024
3f627f4
Merge branch 'main' into mauroromito/directory_search
Velin92 Mar 5, 2024
9ef78a4
Merge branch 'main' into mauroromito/directory_search
Velin92 Mar 11, 2024
a52a232
pr suggestions
Velin92 Mar 13, 2024
2f9b994
docs: removed useless comment
Velin92 Mar 13, 2024
73b0174
improved code spacing
Velin92 Mar 13, 2024
5e69293
Apply suggestions from code review
Velin92 Mar 14, 2024
4b85a81
docs: warning about NSFW content
Velin92 Mar 14, 2024
2291055
docs: updated docs
Velin92 Mar 14, 2024
655ac07
Merge branch 'main' into mauroromito/directory_search
Velin92 Mar 14, 2024
a308d34
Merge branch 'main' into mauroromito/directory_search
Velin92 Mar 15, 2024
0a02a41
doc(sdk): Fix mark up.
Hywan Mar 20, 2024
96c7b3f
doc(sdk): Add a warning.
Hywan Mar 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use crate::{
encryption::Encryption,
notification::NotificationClientBuilder,
notification_settings::NotificationSettings,
room_directory_search::RoomDirectorySearch,
sync_service::{SyncService, SyncServiceBuilder},
task_handle::TaskHandle,
ClientError,
Expand Down Expand Up @@ -740,6 +741,12 @@ impl Client {
}
})))
}

pub fn room_directory_search(&self) -> Arc<RoomDirectorySearch> {
Arc::new(RoomDirectorySearch::new(
matrix_sdk::room_directory_search::RoomDirectorySearch::new((*self.inner).clone()),
))
}
}

#[uniffi::export(callback_interface)]
Expand Down
1 change: 1 addition & 0 deletions bindings/matrix-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod notification;
mod notification_settings;
mod platform;
mod room;
mod room_directory_search;
mod room_info;
mod room_list;
mod room_member;
Expand Down
175 changes: 175 additions & 0 deletions bindings/matrix-sdk-ffi/src/room_directory_search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2024 Mauro Romito
// Copyright 2024 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{fmt::Debug, sync::Arc};

use eyeball_im::VectorDiff;
use futures_util::StreamExt;
use matrix_sdk::room_directory_search::RoomDirectorySearch as SdkRoomDirectorySearch;
use tokio::sync::RwLock;

use super::RUNTIME;
use crate::{error::ClientError, task_handle::TaskHandle};

#[derive(uniffi::Enum)]
pub enum PublicRoomJoinRule {
Public,
Knock,
}

impl TryFrom<ruma::directory::PublicRoomJoinRule> for PublicRoomJoinRule {
type Error = String;

fn try_from(value: ruma::directory::PublicRoomJoinRule) -> Result<Self, Self::Error> {
match value {
ruma::directory::PublicRoomJoinRule::Public => Ok(Self::Public),
ruma::directory::PublicRoomJoinRule::Knock => Ok(Self::Knock),
rule => Err(format!("unsupported join rule: {rule:?}")),
}
}
}

#[derive(uniffi::Record)]
pub struct RoomDescription {
pub room_id: String,
pub name: Option<String>,
pub topic: Option<String>,
pub alias: Option<String>,
pub avatar_url: Option<String>,
pub join_rule: Option<PublicRoomJoinRule>,
pub is_world_readable: bool,
pub joined_members: u64,
}

impl From<matrix_sdk::room_directory_search::RoomDescription> for RoomDescription {
fn from(value: matrix_sdk::room_directory_search::RoomDescription) -> Self {
Self {
room_id: value.room_id.to_string(),
name: value.name,
topic: value.topic,
alias: value.alias.map(|alias| alias.to_string()),
avatar_url: value.avatar_url.map(|url| url.to_string()),
join_rule: value.join_rule.try_into().ok(),
is_world_readable: value.is_world_readable,
joined_members: value.joined_members,
}
}
}

#[derive(uniffi::Object)]
pub struct RoomDirectorySearch {
pub(crate) inner: RwLock<SdkRoomDirectorySearch>,
}

impl RoomDirectorySearch {
pub fn new(inner: SdkRoomDirectorySearch) -> Self {
Self { inner: RwLock::new(inner) }
}
}

#[uniffi::export(async_runtime = "tokio")]
impl RoomDirectorySearch {
pub async fn next_page(&self) -> Result<(), ClientError> {
let mut inner = self.inner.write().await;
inner.next_page().await?;
Ok(())
}

pub async fn search(&self, filter: Option<String>, batch_size: u32) -> Result<(), ClientError> {
let mut inner = self.inner.write().await;
inner.search(filter, batch_size).await?;
Ok(())
}

pub async fn loaded_pages(&self) -> Result<u32, ClientError> {
let inner = self.inner.read().await;
Ok(inner.loaded_pages() as u32)
}

pub async fn is_at_last_page(&self) -> Result<bool, ClientError> {
let inner = self.inner.read().await;
Ok(inner.is_at_last_page())
}

pub async fn results(
&self,
listener: Box<dyn RoomDirectorySearchEntriesListener>,
) -> TaskHandle {
let (initial_values, mut stream) = self.inner.read().await.results();

TaskHandle::new(RUNTIME.spawn(async move {
listener.on_update(vec![RoomDirectorySearchEntryUpdate::Reset {
values: initial_values.into_iter().map(Into::into).collect(),
}]);

while let Some(diffs) = stream.next().await {
listener.on_update(diffs.into_iter().map(|diff| diff.into()).collect());
}
}))
}
}

#[derive(uniffi::Record)]
pub struct RoomDirectorySearchEntriesResult {
pub entries_stream: Arc<TaskHandle>,
}

#[derive(uniffi::Enum)]
pub enum RoomDirectorySearchEntryUpdate {
Append { values: Vec<RoomDescription> },
Clear,
PushFront { value: RoomDescription },
PushBack { value: RoomDescription },
PopFront,
PopBack,
Insert { index: u32, value: RoomDescription },
Set { index: u32, value: RoomDescription },
Remove { index: u32 },
Truncate { length: u32 },
Reset { values: Vec<RoomDescription> },
}

impl From<VectorDiff<matrix_sdk::room_directory_search::RoomDescription>>
for RoomDirectorySearchEntryUpdate
{
fn from(diff: VectorDiff<matrix_sdk::room_directory_search::RoomDescription>) -> Self {
match diff {
VectorDiff::Append { values } => {
Self::Append { values: values.into_iter().map(|v| v.into()).collect() }
}
VectorDiff::Clear => Self::Clear,
VectorDiff::PushFront { value } => Self::PushFront { value: value.into() },
VectorDiff::PushBack { value } => Self::PushBack { value: value.into() },
VectorDiff::PopFront => Self::PopFront,
VectorDiff::PopBack => Self::PopBack,
VectorDiff::Insert { index, value } => {
Self::Insert { index: index as u32, value: value.into() }
}
VectorDiff::Set { index, value } => {
Self::Set { index: index as u32, value: value.into() }
}
VectorDiff::Remove { index } => Self::Remove { index: index as u32 },
VectorDiff::Truncate { length } => Self::Truncate { length: length as u32 },
VectorDiff::Reset { values } => {
Self::Reset { values: values.into_iter().map(|v| v.into()).collect() }
}
}
}
}

#[uniffi::export(callback_interface)]
pub trait RoomDirectorySearchEntriesListener: Send + Sync + Debug {
fn on_update(&self, room_entries_update: Vec<RoomDirectorySearchEntryUpdate>);
}
1 change: 1 addition & 0 deletions crates/matrix-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod notification_settings;
#[cfg(feature = "experimental-oidc")]
pub mod oidc;
pub mod room;
pub mod room_directory_search;
pub mod utils;
pub mod futures {
//! Named futures returned from methods on types in [the crate root][crate].
Expand Down