Skip to main content

jmap_chat_server/
lib.rs

1//! JMAP Chat extension method handlers.
2//!
3//! # Usage
4//!
5//! Implement [`ChatBackend`] for your storage layer, then call
6//! [`register_chat_handlers`] to wire all method names into a
7//! [`jmap_server::Dispatcher`]:
8//!
9//! ```rust,no_run
10//! # use std::sync::Arc;
11//! # use jmap_chat_server::{ChatBackend, register_chat_handlers};
12//! # use jmap_server::Dispatcher;
13//! # fn example<B: ChatBackend + 'static>(backend: B) {
14//! let mut dispatcher: Dispatcher<()> = Dispatcher::new();
15//! register_chat_handlers(&mut dispatcher, Arc::new(backend));
16//! # }
17//! ```
18
19#![forbid(unsafe_code)]
20
21use std::sync::Arc;
22
23use jmap_server::{Dispatcher, HandlerFuture, JmapHandler};
24
25pub mod backend;
26pub mod ban;
27pub mod chat;
28pub mod contact;
29pub mod emoji;
30mod helpers;
31pub mod invite;
32pub mod message;
33pub mod position;
34pub mod presence;
35pub mod space;
36
37pub use backend::{
38    AddedItem, BackendChangesError, BackendSetError, ChangesResult, ChatBackend, GetObject,
39    JmapBackend, JmapObject, QueryChangesResult, QueryObject, QueryResult, SetError, SetErrorType,
40    SetObject,
41};
42pub use ban::{handle_ban_changes, handle_ban_get, handle_ban_set};
43pub use chat::{
44    handle_chat_changes, handle_chat_get, handle_chat_query, handle_chat_query_changes,
45    handle_chat_set, handle_chat_typing,
46};
47pub use contact::{
48    handle_contact_changes, handle_contact_get, handle_contact_query, handle_contact_query_changes,
49    handle_contact_set,
50};
51pub use emoji::{
52    handle_emoji_changes, handle_emoji_get, handle_emoji_query, handle_emoji_query_changes,
53    handle_emoji_set,
54};
55pub use invite::{handle_invite_changes, handle_invite_get, handle_invite_set};
56pub use message::{
57    handle_message_changes, handle_message_get, handle_message_query, handle_message_query_changes,
58    handle_message_set,
59};
60pub use position::{handle_position_changes, handle_position_get, handle_position_set};
61pub use presence::{handle_presence_changes, handle_presence_get, handle_presence_set};
62pub use space::{
63    handle_space_changes, handle_space_get, handle_space_join, handle_space_query,
64    handle_space_query_changes, handle_space_set,
65};
66
67// ---------------------------------------------------------------------------
68// register_chat_handlers — the main entry point for consumers
69// ---------------------------------------------------------------------------
70
71/// Register all JMAP Chat method handlers with `dispatcher`.
72///
73/// `backend` is wrapped in [`Arc`] so it is cloned cheaply into each handler.
74/// Pass the same `Arc<B>` to both this function and any application-level code
75/// that needs to access the backend.
76///
77/// After this call, the dispatcher handles:
78/// `Chat/*`, `Message/*`, `Space/*`, `SpaceBan/*`, `ChatContact/*`,
79/// `ReadPosition/*`, `CustomEmoji/*`, `SpaceInvite/*`, and `PresenceStatus/*`.
80///
81/// **Caller context `C` is not forwarded to handlers.** Each handler closure
82/// receives only `(Arc<B>, call_id, args)`; the `caller: C` value from the
83/// dispatcher is discarded. To act on per-request context (e.g. for
84/// per-tenant auth or rate limiting), implement [`JmapHandler`] directly
85/// rather than using this function. The closure shape used here is stable for
86/// v0.1; adding `CallerCtx` forwarding would be a breaking change and is
87/// deferred to a future version.
88pub fn register_chat_handlers<B, C>(dispatcher: &mut Dispatcher<C>, backend: Arc<B>)
89where
90    B: ChatBackend + 'static,
91    C: Clone + Send + 'static,
92{
93    // Helper: register one method with a closure that takes (Arc<B>, call_id, args).
94    macro_rules! reg {
95        ($method:expr, $backend:expr, |$b:ident, $ci:ident, $a:ident| $body:expr) => {{
96            let backend_arc: Arc<B> = Arc::clone(&$backend);
97            let h: Arc<dyn JmapHandler<C>> = Arc::new(ClosureHandler {
98                backend: backend_arc,
99                call_fn: Box::new(move |$b: Arc<B>, $ci: String, $a: serde_json::Value| {
100                    Box::pin(async move { $body }) as HandlerFuture
101                }),
102            });
103            dispatcher.register($method, h);
104        }};
105    }
106
107    // Chat
108    reg!("Chat/get", backend, |b, _ci, a| handle_chat_get(&*b, a)
109        .await);
110    reg!("Chat/changes", backend, |b, _ci, a| handle_chat_changes(
111        &*b, a
112    )
113    .await);
114    reg!("Chat/query", backend, |b, _ci, a| handle_chat_query(&*b, a)
115        .await);
116    reg!("Chat/queryChanges", backend, |b, _ci, a| {
117        handle_chat_query_changes(&*b, a).await
118    });
119    reg!("Chat/set", backend, |b, _ci, a| handle_chat_set(&*b, a)
120        .await);
121    reg!("Chat/typing", backend, |b, _ci, a| handle_chat_typing(
122        &*b, a
123    )
124    .await);
125
126    // Message
127    reg!("Message/get", backend, |b, _ci, a| handle_message_get(
128        &*b, a
129    )
130    .await);
131    reg!("Message/changes", backend, |b, _ci, a| {
132        handle_message_changes(&*b, a).await
133    });
134    reg!("Message/query", backend, |b, _ci, a| handle_message_query(
135        &*b, a
136    )
137    .await);
138    reg!("Message/queryChanges", backend, |b, _ci, a| {
139        handle_message_query_changes(&*b, a).await
140    });
141    reg!("Message/set", backend, |b, _ci, a| handle_message_set(
142        &*b, a
143    )
144    .await);
145
146    // Space
147    reg!("Space/get", backend, |b, _ci, a| handle_space_get(&*b, a)
148        .await);
149    reg!("Space/changes", backend, |b, _ci, a| handle_space_changes(
150        &*b, a
151    )
152    .await);
153    reg!("Space/query", backend, |b, _ci, a| handle_space_query(
154        &*b, a
155    )
156    .await);
157    reg!("Space/queryChanges", backend, |b, _ci, a| {
158        handle_space_query_changes(&*b, a).await
159    });
160    reg!("Space/set", backend, |b, _ci, a| handle_space_set(&*b, a)
161        .await);
162    reg!("Space/join", backend, |b, _ci, a| handle_space_join(&*b, a)
163        .await);
164
165    // ChatContact
166    reg!("ChatContact/get", backend, |b, _ci, a| {
167        handle_contact_get(&*b, a).await
168    });
169    reg!("ChatContact/changes", backend, |b, _ci, a| {
170        handle_contact_changes(&*b, a).await
171    });
172    reg!("ChatContact/query", backend, |b, _ci, a| {
173        handle_contact_query(&*b, a).await
174    });
175    reg!("ChatContact/queryChanges", backend, |b, _ci, a| {
176        handle_contact_query_changes(&*b, a).await
177    });
178    reg!("ChatContact/set", backend, |b, _ci, a| {
179        handle_contact_set(&*b, a).await
180    });
181
182    // ReadPosition
183    reg!("ReadPosition/get", backend, |b, _ci, a| {
184        handle_position_get(&*b, a).await
185    });
186    reg!("ReadPosition/changes", backend, |b, _ci, a| {
187        handle_position_changes(&*b, a).await
188    });
189    reg!("ReadPosition/set", backend, |b, _ci, a| {
190        handle_position_set(&*b, a).await
191    });
192
193    // SpaceInvite
194    reg!("SpaceInvite/get", backend, |b, _ci, a| {
195        handle_invite_get(&*b, a).await
196    });
197    reg!("SpaceInvite/changes", backend, |b, _ci, a| {
198        handle_invite_changes(&*b, a).await
199    });
200    reg!("SpaceInvite/set", backend, |b, _ci, a| {
201        handle_invite_set(&*b, a).await
202    });
203
204    // SpaceBan
205    reg!("SpaceBan/get", backend, |b, _ci, a| {
206        handle_ban_get(&*b, a).await
207    });
208    reg!("SpaceBan/changes", backend, |b, _ci, a| {
209        handle_ban_changes(&*b, a).await
210    });
211    reg!("SpaceBan/set", backend, |b, _ci, a| {
212        handle_ban_set(&*b, a).await
213    });
214
215    // CustomEmoji
216    reg!("CustomEmoji/get", backend, |b, _ci, a| {
217        handle_emoji_get(&*b, a).await
218    });
219    reg!("CustomEmoji/changes", backend, |b, _ci, a| {
220        handle_emoji_changes(&*b, a).await
221    });
222    reg!("CustomEmoji/query", backend, |b, _ci, a| {
223        handle_emoji_query(&*b, a).await
224    });
225    reg!("CustomEmoji/queryChanges", backend, |b, _ci, a| {
226        handle_emoji_query_changes(&*b, a).await
227    });
228    reg!("CustomEmoji/set", backend, |b, _ci, a| {
229        handle_emoji_set(&*b, a).await
230    });
231
232    // PresenceStatus
233    reg!("PresenceStatus/get", backend, |b, _ci, a| {
234        handle_presence_get(&*b, a).await
235    });
236    reg!("PresenceStatus/changes", backend, |b, _ci, a| {
237        handle_presence_changes(&*b, a).await
238    });
239    reg!("PresenceStatus/set", backend, |b, _ci, a| {
240        handle_presence_set(&*b, a).await
241    });
242}
243
244pub use jmap_server::ClosureHandler;