From 3cdcf581db09e5b9d8410be938df4c033a79b7d0 Mon Sep 17 00:00:00 2001 From: mb Date: Tue, 19 Dec 2023 18:23:49 +0100 Subject: [PATCH] feat: Set has_draft in SidebarItem --- bindings/prose-sdk-js/src/client.rs | 3 ++- crates/prose-core-client/src/app/services/room.rs | 7 +++++-- .../src/app/services/sidebar_service.rs | 15 +++++++++++---- .../src/infra/messaging/drafts_repository.rs | 2 +- examples/prose-core-client-cli/src/main.rs | 6 ++++-- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/bindings/prose-sdk-js/src/client.rs b/bindings/prose-sdk-js/src/client.rs index 4feda622..6092bea1 100644 --- a/bindings/prose-sdk-js/src/client.rs +++ b/bindings/prose-sdk-js/src/client.rs @@ -155,10 +155,11 @@ impl Client { } #[wasm_bindgen(js_name = "sidebarItems")] - pub fn sidebar_items(&self) -> SidebarItemsArray { + pub async fn sidebar_items(&self) -> SidebarItemsArray { self.client .sidebar .sidebar_items() + .await .into_iter() .map(|item| { JsValue::from(SidebarItem { diff --git a/crates/prose-core-client/src/app/services/room.rs b/crates/prose-core-client/src/app/services/room.rs index 860d50d0..6fe9d967 100644 --- a/crates/prose-core-client/src/app/services/room.rs +++ b/crates/prose-core-client/src/app/services/room.rs @@ -23,7 +23,7 @@ use crate::domain::messaging::models::{Emoji, Message, MessageId, MessageLike}; use crate::domain::rooms::models::{RoomAffiliation, RoomInternals, RoomSpec}; use crate::domain::shared::models::{ParticipantId, ParticipantInfo, RoomId}; use crate::dtos::{Message as MessageDTO, MessageSender, UserBasicInfo, UserId}; -use crate::ClientRoomEventType; +use crate::{ClientEvent, ClientRoomEventType}; pub struct Room { inner: Arc, @@ -210,7 +210,10 @@ impl Room { } pub async fn save_draft(&self, text: Option<&str>) -> Result<()> { - self.drafts_repo.set(&self.data.room_id, text).await + self.drafts_repo.set(&self.data.room_id, text).await?; + self.client_event_dispatcher + .dispatch_event(ClientEvent::SidebarChanged); + Ok(()) } pub async fn load_draft(&self) -> Result> { diff --git a/crates/prose-core-client/src/app/services/sidebar_service.rs b/crates/prose-core-client/src/app/services/sidebar_service.rs index ee45cdee..41018a45 100644 --- a/crates/prose-core-client/src/app/services/sidebar_service.rs +++ b/crates/prose-core-client/src/app/services/sidebar_service.rs @@ -9,8 +9,8 @@ use tracing::error; use prose_proc_macros::InjectDependencies; use crate::app::deps::{ - DynConnectedRoomsReadOnlyRepository, DynRoomFactory, DynSidebarDomainService, - DynSidebarReadOnlyRepository, + DynConnectedRoomsReadOnlyRepository, DynDraftsRepository, DynRoomFactory, + DynSidebarDomainService, DynSidebarReadOnlyRepository, }; use crate::domain::shared::models::RoomId; use crate::dtos::SidebarItem as SidebarItemDTO; @@ -20,6 +20,8 @@ pub struct SidebarService { #[inject] connected_rooms_repo: DynConnectedRoomsReadOnlyRepository, #[inject] + drafts_repo: DynDraftsRepository, + #[inject] room_factory: DynRoomFactory, #[inject] sidebar_domain_service: DynSidebarDomainService, @@ -28,7 +30,7 @@ pub struct SidebarService { } impl SidebarService { - pub fn sidebar_items(&self) -> Vec { + pub async fn sidebar_items(&self) -> Vec { let items = self.sidebar_repo.get_all(); let mut item_dtos = vec![]; @@ -45,7 +47,12 @@ impl SidebarService { name: item.name, room: self.room_factory.build(room), is_favorite: item.is_favorite, - has_draft: false, // TODO + has_draft: self + .drafts_repo + .get(&item.jid) + .await + .unwrap_or_default() + .is_some(), unread_count: 0, // TODO mentions_count: 0, // TODO error: item.error, diff --git a/crates/prose-core-client/src/infra/messaging/drafts_repository.rs b/crates/prose-core-client/src/infra/messaging/drafts_repository.rs index 2d7455bb..8764327f 100644 --- a/crates/prose-core-client/src/infra/messaging/drafts_repository.rs +++ b/crates/prose-core-client/src/infra/messaging/drafts_repository.rs @@ -38,7 +38,7 @@ impl DomainDraftsRepository for DraftsRepository { .await?; let collection = tx.readable_collection(DraftsRecord::collection())?; let record = collection.get::<_, DraftsRecord>(room_id).await?; - Ok(record.map(|r| r.text)) + Ok(record.and_then(|r| (!r.text.is_empty()).then_some(r.text))) } async fn set(&self, room_id: &BareJid, draft: Option<&str>) -> anyhow::Result<()> { diff --git a/examples/prose-core-client-cli/src/main.rs b/examples/prose-core-client-cli/src/main.rs index 840aa131..4ffb76a4 100644 --- a/examples/prose-core-client-cli/src/main.rs +++ b/examples/prose-core-client-cli/src/main.rs @@ -336,6 +336,7 @@ async fn select_room( let mut rooms = client .sidebar .sidebar_items() + .await .into_iter() .filter_map(|room| { if !filter(&room) { @@ -372,7 +373,7 @@ async fn select_public_channel(client: &Client) -> Result { } async fn select_sidebar_item(client: &Client) -> Result> { - let items = client.sidebar.sidebar_items(); + let items = client.sidebar.sidebar_items().await; if items.is_empty() { return Ok(None); } @@ -694,6 +695,7 @@ async fn list_connected_rooms(client: &Client) -> Result<()> { let mut rooms = client .sidebar .sidebar_items() + .await .into_iter() .map(|item| item.room) .collect::>(); @@ -918,7 +920,7 @@ async fn main() -> Result<()> { } } Selection::ListSidebarItems => { - let items = client.sidebar.sidebar_items().into_iter().fold( + let items = client.sidebar.sidebar_items().await.into_iter().fold( HashMap::new(), |mut map, item| { let category = match item.room {