Skip to content

Commit

Permalink
feat: Set has_draft in SidebarItem
Browse files Browse the repository at this point in the history
  • Loading branch information
nesium committed Dec 19, 2023
1 parent d29486a commit 3cdcf58
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion bindings/prose-sdk-js/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions crates/prose-core-client/src/app/services/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Kind> {
inner: Arc<RoomInner>,
Expand Down Expand Up @@ -210,7 +210,10 @@ impl<Kind> Room<Kind> {
}

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<Option<String>> {
Expand Down
15 changes: 11 additions & 4 deletions crates/prose-core-client/src/app/services/sidebar_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -28,7 +30,7 @@ pub struct SidebarService {
}

impl SidebarService {
pub fn sidebar_items(&self) -> Vec<SidebarItemDTO> {
pub async fn sidebar_items(&self) -> Vec<SidebarItemDTO> {
let items = self.sidebar_repo.get_all();
let mut item_dtos = vec![];

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down
6 changes: 4 additions & 2 deletions examples/prose-core-client-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ async fn select_room(
let mut rooms = client
.sidebar
.sidebar_items()
.await
.into_iter()
.filter_map(|room| {
if !filter(&room) {
Expand Down Expand Up @@ -372,7 +373,7 @@ async fn select_public_channel(client: &Client) -> Result<PublicRoomInfo> {
}

async fn select_sidebar_item(client: &Client) -> Result<Option<SidebarItem>> {
let items = client.sidebar.sidebar_items();
let items = client.sidebar.sidebar_items().await;
if items.is_empty() {
return Ok(None);
}
Expand Down Expand Up @@ -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::<Vec<_>>();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3cdcf58

Please sign in to comment.