libchat/core/conversations/src/conversation.rs

71 lines
2.1 KiB
Rust
Raw Normal View History

2026-04-23 18:04:45 -07:00
pub mod group_v1;
mod privatev1;
2026-04-23 18:04:45 -07:00
use crate::{
2026-04-24 14:21:15 -07:00
DeliveryService, service_traits::KeyPackageProvider,
2026-04-23 18:04:45 -07:00
ctx::ClientCtx,
2026-04-24 14:21:15 -07:00
types::{AccountId, AddressedEncryptedPayload, ContentData},
2026-04-23 18:04:45 -07:00
};
use chat_proto::logoschat::encryption::EncryptedPayload;
2025-12-22 09:40:46 -08:00
use std::fmt::Debug;
use std::sync::Arc;
2026-04-23 18:04:45 -07:00
use storage::{ChatStore, ConversationKind, ConversationStore, RatchetStore};
2025-12-22 09:40:46 -08:00
pub use crate::errors::ChatError;
2026-04-23 18:19:41 -07:00
pub use group_v1::{GroupV1Convo, IdentityProvider};
pub use privatev1::PrivateV1Convo;
2025-12-22 09:40:46 -08:00
pub type ConversationId<'a> = &'a str;
pub type ConversationIdOwned = Arc<str>;
pub trait Id: Debug {
2026-02-10 18:34:33 +01:00
fn id(&self) -> ConversationId<'_>;
2025-12-22 09:40:46 -08:00
}
pub trait Convo: Id + Debug {
fn send_message(&mut self, content: &[u8])
-> Result<Vec<AddressedEncryptedPayload>, ChatError>;
/// Decrypts and processes an incoming encrypted frame.
///
/// Returns `Ok(Some(ContentData))` if the frame contains user content,
/// `Ok(None)` for protocol frames (e.g., placeholders), or an error if
/// decryption or frame parsing fails.
fn handle_frame(
&mut self,
enc_payload: EncryptedPayload,
) -> Result<Option<ContentData>, ChatError>;
fn remote_id(&self) -> String;
2025-12-22 09:40:46 -08:00
/// Returns the conversation type identifier for storage.
fn convo_type(&self) -> ConversationKind;
2025-12-22 09:40:46 -08:00
}
2026-04-24 14:21:15 -07:00
pub trait GroupConvo<DS: DeliveryService, RS: KeyPackageProvider, CS: ChatStore>: Convo {
2026-04-23 18:04:45 -07:00
fn add_member(
&mut self,
ctx: &mut ClientCtx<DS, RS, CS>,
2026-04-24 14:21:15 -07:00
members: &[&AccountId],
2026-04-23 18:04:45 -07:00
) -> Result<(), ChatError>;
// Default implementation which dispatches envelopes to the DeliveryService
fn send_content(
&mut self,
ctx: &mut ClientCtx<DS, RS, CS>,
content: &[u8],
) -> Result<(), ChatError> {
let payloads = self.send_message(content)?;
for payload in payloads {
ctx.ds()
.publish(payload.into_envelope(self.id().into()))
.map_err(|e| ChatError::Delivery(e.to_string()))?;
}
Ok(())
}
}
pub enum Conversation<S: ConversationStore + RatchetStore> {
Private(PrivateV1Convo<S>),
}