Skip to main content

im_core/core/
client.rs

1use std::sync::{Arc, OnceLock};
2
3#[derive(Clone)]
4pub struct ImClient {
5    core: Arc<super::ImCoreInner>,
6    identity: crate::identity::IdentitySummary,
7    runtime: Arc<crate::internal::identity_runtime::ClientIdentityRuntime>,
8    conversation_store:
9        Arc<OnceLock<Arc<crate::internal::runtime_store::conversation_store::ConversationStore>>>,
10    message_store: Arc<OnceLock<Arc<crate::internal::runtime_store::message_store::MessageStore>>>,
11}
12
13impl ImClient {
14    pub(crate) fn new(
15        core: Arc<super::ImCoreInner>,
16        runtime: crate::internal::identity_runtime::ClientIdentityRuntime,
17    ) -> Self {
18        let identity = runtime.summary.clone();
19        Self {
20            core,
21            identity,
22            runtime: Arc::new(runtime),
23            conversation_store: Arc::new(OnceLock::new()),
24            message_store: Arc::new(OnceLock::new()),
25        }
26    }
27
28    pub fn current_identity(&self) -> &crate::identity::IdentitySummary {
29        &self.identity
30    }
31
32    pub fn did(&self) -> &crate::ids::Did {
33        &self.identity.did
34    }
35
36    pub fn handle(&self) -> Option<&crate::ids::Handle> {
37        self.identity.handle.as_ref()
38    }
39
40    pub fn did_domain(&self) -> &str {
41        self.core.sdk_config().did_domain.as_str()
42    }
43
44    pub fn auth(&self) -> crate::auth::AuthService<'_> {
45        crate::auth::AuthService::new(self)
46    }
47
48    pub fn identity(&self) -> crate::identity::IdentityService<'_> {
49        crate::identity::IdentityService::new(self)
50    }
51
52    pub fn directory(&self) -> crate::directory::DirectoryService<'_> {
53        crate::directory::DirectoryService::new(self)
54    }
55
56    pub fn content(&self) -> crate::content::ContentService<'_> {
57        crate::content::ContentService::new(self)
58    }
59
60    pub fn site(&self) -> crate::site::SiteService<'_> {
61        crate::site::SiteService::new(self)
62    }
63
64    pub fn messages(&self) -> crate::messages::MessageService<'_> {
65        crate::messages::MessageService::new(self)
66    }
67
68    pub fn attachments(&self) -> crate::attachments::AttachmentService<'_> {
69        crate::attachments::AttachmentService::new(self)
70    }
71
72    pub fn groups(&self) -> crate::groups::GroupService<'_> {
73        crate::groups::GroupService::new(self)
74    }
75
76    pub fn realtime(&self) -> crate::realtime::RealtimeService<'_> {
77        crate::realtime::RealtimeService::new(self)
78    }
79
80    pub fn email(&self) -> crate::email::EmailService<'_> {
81        crate::email::EmailService::new(self)
82    }
83
84    pub fn secure(&self) -> crate::secure::SecureService<'_> {
85        crate::secure::SecureService::new(self)
86    }
87
88    pub(crate) fn runtime(&self) -> &crate::internal::identity_runtime::ClientIdentityRuntime {
89        &self.runtime
90    }
91
92    pub(crate) fn core_inner(&self) -> &super::ImCoreInner {
93        &self.core
94    }
95
96    pub(crate) fn conversation_store(
97        &self,
98    ) -> Arc<crate::internal::runtime_store::conversation_store::ConversationStore> {
99        self.conversation_store
100            .get_or_init(|| {
101                crate::internal::runtime_store::conversation_store::ConversationStore::new_for_client(
102                    self,
103                )
104            })
105            .clone()
106    }
107
108    pub(crate) fn emit_committed_conversation_projection(&self, reason: &str) {
109        let Some(store) = self.conversation_store.get() else {
110            return;
111        };
112        store.on_committed_local_projection(self, reason);
113    }
114
115    pub(crate) fn emit_committed_local_message_projection(&self, reason: &str) {
116        self.emit_committed_conversation_projection(reason);
117        self.emit_committed_message_projection(reason);
118    }
119
120    pub(crate) fn message_store(
121        &self,
122    ) -> Arc<crate::internal::runtime_store::message_store::MessageStore> {
123        self.message_store
124            .get_or_init(|| {
125                crate::internal::runtime_store::message_store::MessageStore::new_for_client(self)
126            })
127            .clone()
128    }
129
130    pub(crate) fn emit_committed_message_projection(&self, reason: &str) {
131        let Some(store) = self.message_store.get() else {
132            return;
133        };
134        store.on_committed_local_projection(self, reason);
135    }
136
137    pub(crate) fn emit_committed_message_sync_invalidation_if_initialized(
138        &self,
139        invalidation: &crate::internal::local_state::sync_state::SyncDeltaInvalidation,
140    ) {
141        let Some(store) = self.message_store.get() else {
142            return;
143        };
144        store.on_committed_sync_invalidation(self, invalidation);
145    }
146}