Skip to content

Commit

Permalink
feat: Add method to find public channel by name (refs #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
nesium committed Jan 26, 2024
1 parent 193c55f commit 95bd4fa
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
12 changes: 12 additions & 0 deletions bindings/prose-sdk-js/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ impl Client {
.collect_into_js_array::<ChannelsArray>())
}

/// Returns the `BareJid` of the public room with `name` if one exists.
#[wasm_bindgen(js_name = "findPublicChannelByName")]
pub async fn find_public_channel_by_name(&self, name: &str) -> Result<Option<BareJid>> {
Ok(self
.client
.rooms
.find_public_channel_by_name(name)
.await
.map_err(WasmError::from)?
.map(|room_id| room_id.into_inner().into()))
}

#[wasm_bindgen(js_name = "loadAccountInfo")]
pub async fn load_account_info(&self) -> Result<AccountInfo> {
Ok(self
Expand Down
6 changes: 3 additions & 3 deletions bindings/prose-sdk-js/src/types/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ impl Channel {
impl From<PublicRoomInfo> for Channel {
fn from(value: PublicRoomInfo) -> Self {
Channel {
jid: value.jid.clone().into_inner().into(),
jid: value.id.clone().into_inner().into(),
name: value
.name
.or(value.jid.node_str().map(|n| n.to_string()))
.unwrap_or(value.jid.to_string()),
.or(value.id.node_str().map(|n| n.to_string()))
.unwrap_or(value.id.to_string()),
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/prose-core-client/src/app/services/rooms_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ impl RoomsService {
.await?)
}

pub async fn find_public_channel_by_name(&self, name: &str) -> Result<Option<RoomId>> {
let rooms = self
.room_management_service
.load_public_rooms(&self.ctx.muc_service()?)
.await?;

let needle = name.to_lowercase();
Ok(rooms
.into_iter()
.find(|r| r.name.as_ref().map(|name| name.to_lowercase()).as_ref() == Some(&needle))
.map(|room| room.id))
}

pub async fn start_conversation(&self, participants: &[UserId]) -> Result<RoomId> {
if participants.is_empty() {
bail!("You need at least one participant to start a conversation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use crate::domain::shared::models::RoomId;

#[derive(Debug, Clone, PartialEq)]
pub struct PublicRoomInfo {
pub jid: RoomId,
pub id: RoomId,
pub name: Option<String>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl RoomManagementService for XMPPClient {
.await?
.into_iter()
.map(|room| PublicRoomInfo {
jid: room.jid.into_bare().into(),
id: room.jid.into_bare().into(),
name: room.name,
})
.collect();
Expand Down
6 changes: 3 additions & 3 deletions crates/prose-core-client/tests/rooms_domain_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ async fn test_throws_conflict_error_if_room_exists() -> Result<()> {
.return_once(|_| {
Box::pin(async {
Ok(vec![PublicRoomInfo {
jid: room_id!("room@conference.prose.org"),
id: room_id!("room@conference.prose.org"),
name: Some("new channel".to_string()),
}])
})
Expand Down Expand Up @@ -732,7 +732,7 @@ async fn test_creates_public_room_if_it_does_not_exist() -> Result<()> {
.return_once(|_| {
Box::pin(async {
Ok(vec![PublicRoomInfo {
jid: room_id!("room@conference.prose.org"),
id: room_id!("room@conference.prose.org"),
name: Some("Old Channel".to_string()),
}])
})
Expand Down Expand Up @@ -1081,7 +1081,7 @@ async fn test_converts_private_to_public_channel_name_conflict() -> Result<()> {
.return_once(|_| {
Box::pin(async {
Ok(vec![PublicRoomInfo {
jid: room_id!("room@conference.prose.org"),
id: room_id!("room@conference.prose.org"),
name: Some("new channel".to_string()),
}])
})
Expand Down
41 changes: 41 additions & 0 deletions crates/prose-core-client/tests/rooms_service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use prose_core_client::domain::shared::models::RoomId;
use prose_core_client::dtos::PublicRoomInfo;
use prose_core_client::room_id;
use prose_core_client::services::RoomsService;
use prose_core_client::test::MockAppDependencies;

#[tokio::test]
async fn test_find_public_channel_by_name() -> anyhow::Result<()> {
let mut deps = MockAppDependencies::default();

deps.room_management_service
.expect_load_public_rooms()
.returning(|_| {
Box::pin(async {
Ok(vec![
PublicRoomInfo {
id: room_id!("dev-core@muc.prose.org"),
name: Some("Dev-Core".to_string()),
},
PublicRoomInfo {
id: room_id!("dev-web@muc.prose.org"),
name: Some("dev-web".to_string()),
},
])
})
});

let service = RoomsService::from(&deps.into_deps());

assert_eq!(
service.find_public_channel_by_name("dev-core").await?,
Some(room_id!("dev-core@muc.prose.org"))
);
assert_eq!(
service.find_public_channel_by_name("Dev-Web").await?,
Some(room_id!("dev-web@muc.prose.org"))
);
assert_eq!(service.find_public_channel_by_name("dev-pod").await?, None);

Ok(())
}
4 changes: 2 additions & 2 deletions examples/prose-core-client-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl From<muc::Room> for JidWithName {
impl From<PublicRoomInfo> for JidWithName {
fn from(value: PublicRoomInfo) -> Self {
Self {
jid: value.jid.into_inner(),
jid: value.id.into_inner(),
name: value.name.as_deref().unwrap_or("<untitled>").to_string(),
}
}
Expand Down Expand Up @@ -882,7 +882,7 @@ async fn main() -> Result<()> {
}
Selection::JoinPublicRoom => {
let room = select_public_channel(&client).await?;
client.rooms.join_room(&room.jid, None).await?;
client.rooms.join_room(&room.id, None).await?;
}
Selection::JoinRoomByJid => {
let jid = prompt_bare_jid(None);
Expand Down

0 comments on commit 95bd4fa

Please sign in to comment.