jmap-chat-server 0.6.0

JMAP Chat extension method handlers — backend-agnostic, plugs into jmap-server Dispatcher
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! JMAP Chat extension method handlers.
//!
//! # Usage
//!
//! Implement [`ChatBackend`] for your storage layer, then call
//! [`register_chat_handlers`] to wire all method names into a
//! [`jmap_server::Dispatcher`]:
//!
//! ```rust,no_run
//! # use std::sync::Arc;
//! # use jmap_chat_server::{ChatBackend, register_chat_handlers};
//! # use jmap_server::Dispatcher;
//! # fn example<B: ChatBackend<CallerCtx = ()> + 'static>(backend: B) {
//! let mut dispatcher: Dispatcher<()> = Dispatcher::new();
//! register_chat_handlers(&mut dispatcher, Arc::new(backend));
//! # }
//! ```
//!
//! # `memory` feature (reference implementation)
//!
//! Enable the `memory` feature to expose the `memory::MemoryBackend`
//! reference implementation of [`ChatBackend`]. This is the same
//! backend used by this crate's own integration tests, intended for
//! downstream contributors to study and for smoke tests / examples
//! that do not want to stand up a real database. **Not production.**
//! API stability is opt-in via this feature and may break across minor
//! versions while the crate is pre-1.0.
//!
//! # Permission enforcement: backend canonical
//!
//! All Space/set permission gates live in the backend's
//! `apply_space_patch` implementation (see `memory::MemoryBackend`
//! under the `memory` feature for the reference impl). The handler
//! [`handle_space_set`] does NOT permission-check.
//!
//! The pure helper [`required_permissions_for_op`]
//! maps each [`SpacePatchOp`] variant to its required
//! [`ChatPermission`](jmap_chat_types::ChatPermission)s per draft-atwood-jmap-chat-00 §Space/set. Backends consume this
//! helper inside `apply_space_patch` after resolving the caller's
//! effective permissions from the Space `members` entry whose `id` is
//! the `accountId` (the owner, per draft §Terminology). The gates apply
//! whether or not [`jmap_server::JmapAccounts::principal_id`] resolves a
//! caller identity.
//! Handlers MUST NOT consume the helper for gating — backends are the
//! single source of truth. This mirrors the workspace-wide rule
//! documented in workspace AGENTS.md "Caller identity (foundation
//! seam)":
//!
//! - Handlers do NO permission checking.
//! - Defense-in-depth handler pre-checks are allowed but the backend
//!   MUST re-verify atomically with the mutation.

#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
// bd:JMAP-uqhn.2 — narrowing and sign-changing `as` casts are banned;
// use `try_from`. Widening `usize as u64` stays legal (these lints do
// not fire on it): it is lossless on every target Rust supports.
#![warn(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss
)]

use std::sync::Arc;

use jmap_server::{
    CallContext, ClosureHandler, ConditionalBackend, Dispatcher, HandlerFuture, JmapHandler,
    RegistrationError, SetSupport,
};

mod backend;
mod ban;
mod chat;
mod chat_ban;
mod contact;
mod did_auth;
mod emoji;
mod ephemeral;
mod expiry;
mod helpers;
mod history;
mod invite;
/// In-memory reference implementation of [`ChatBackend`].
///
/// Gated behind `feature = "memory"`. Not production. See [`memory`] for
/// the full module documentation.
#[cfg(feature = "memory")]
pub mod memory;
pub mod message;
mod outbound;
mod permissions;
mod position;
mod presence;
mod push;
mod space;
#[cfg(feature = "testkit")]
pub mod testkit;

pub use backend::{
    AttachmentBlob, ChatAccessPolicy, ChatBackend, ChatEvents, ChatLimits, ChatMessageLifecycle,
    ChatModeration, ChatResolvers, ChatRestrictions, ChatSettings, ChatSpaces, ChatStore,
    EmojiSetOp, InviteJoinRefusal, OpResult, SlowModeError, SpacePolicy,
};
pub use backend::{
    DelegateChatAccessPolicy, DelegateChatEvents, DelegateChatMessageLifecycle,
    DelegateChatModeration, DelegateChatResolvers, DelegateChatRestrictions, DelegateChatSettings,
    DelegateChatSpaces, DelegateChatStore, DelegateSpacePolicy,
};
pub use ban::{handle_ban_changes, handle_ban_get, handle_ban_set};
pub use chat::{
    handle_chat_changes, handle_chat_get, handle_chat_query, handle_chat_query_changes,
    handle_chat_set, handle_chat_typing,
};
pub use chat_ban::{handle_chat_ban_changes, handle_chat_ban_get, handle_chat_ban_set};
pub use contact::{
    handle_contact_changes, handle_contact_get, handle_contact_query, handle_contact_query_changes,
    handle_contact_set,
};
pub use did_auth::{DidAuthRejection, DidDocument, verify_did_binding};
pub use emoji::{
    handle_emoji_changes, handle_emoji_get, handle_emoji_query, handle_emoji_query_changes,
    handle_emoji_set,
};
pub use ephemeral::{
    CompanionStreams, EphemeralSubscription, IdScope, project_presence, should_deliver_typing,
};
pub use expiry::sweep_expired;
pub use history::register_chat_handlers_with_history;
pub use invite::{handle_invite_changes, handle_invite_get, handle_invite_set};
pub use jmap_chat_types::{
    ChatContactProperty, ChatProperty, MessageProperty, ReadPositionProperty, SpaceProperty,
};
pub use jmap_chat_types::{SpaceMetadataPatch, SpacePatchOp};
pub use outbound::{GroupUpdateAction, OutboundEvent, ReactionAction};
pub use permissions::{
    RequiredPermissions, required_permissions_for_op, resolve_channel_permissions,
};
pub use position::{handle_position_changes, handle_position_get, handle_position_set};
pub use presence::{handle_presence_changes, handle_presence_get, handle_presence_set};
pub use push::{
    BuiltPush, OwnerFlags, PushItem, build_chat_message_push, chat_state_change_for,
    fit_chat_message_push, owner_unread_count, should_push, strip_unsupported_chat_push,
    validate_chat_push,
};
pub use space::{
    handle_space_changes, handle_space_get, handle_space_join, handle_space_query,
    handle_space_query_changes, handle_space_set,
};

// ---------------------------------------------------------------------------
// register_chat_handlers — the main entry point for consumers
// ---------------------------------------------------------------------------

/// Register all JMAP Chat method handlers with `dispatcher`.
///
/// `backend` is wrapped in [`Arc`] so it is cloned cheaply into each handler.
/// You may pass any `Arc<B>` — the function clones it internally into each
/// registered handler closure. Sharing the same `Arc<B>` across this call
/// and other application-level uses of the backend is a memory
/// optimization, not a correctness requirement; separate `Arc<B>` instances
/// pointing at the same underlying backend would also work.
///
/// After this call, the dispatcher handles:
/// `Chat/*`, `Message/*`, `Space/*`, `SpaceBan/*`, `ChatBan/*`, `ChatContact/*`,
/// `ReadPosition/*`, `CustomEmoji/*`, `SpaceInvite/*`, and `PresenceStatus/*`.
///
/// The dispatcher's `CallerCtx` is taken from `B::CallerCtx`; every registered
/// closure forwards it as `&ctx` into the wrapped `handle_*` function. Backends
/// that use `type CallerCtx = ()` therefore see `&()` inside every handler.
///
/// # Re-registration semantics
///
/// This function calls [`Dispatcher::register`] once per
/// draft-atwood-jmap-chat-00 method name. `Dispatcher::register`
/// **silently overwrites** any pre-existing handler under the same
/// method name (the underlying primitive is `HashMap::insert`). Three
/// consequences callers MUST be aware of:
///
/// - **Double-call**: invoking this function twice on the same
///   dispatcher loses the first set's handlers. The second call wins.
/// - **Custom overrides go LAST**: to replace a single handler (e.g.
///   provide a custom `Chat/get`), call this function FIRST, then
///   `dispatcher.register("Chat/get", my_override)`. The inverse
///   order silently undoes the custom handler.
/// - **No collision diagnostic**: there is no error or log when a
///   handler is overwritten. The contract is "last register wins" and
///   the caller is responsible for ordering.
///
/// [`Dispatcher::register`]: jmap_server::Dispatcher::register
pub fn register_chat_handlers<B>(dispatcher: &mut Dispatcher<B::CallerCtx>, backend: Arc<B>)
where
    B: ChatBackend + 'static,
{
    // Helper: register one method with a closure that takes
    // (Arc<B>, call_id, args, ctx).
    //
    // `$ci` is the call_id string (echoed back to the client). Most handlers
    // ignore it and use `_ci` as the identifier.
    //
    // `$ctx` is the per-request caller context (`B::CallerCtx`) forwarded
    // by the dispatcher. Closures pass `&ctx` to the inner `handle_*` fn.
    // Each optional trailing ident binds the `CallContext` field of that name:
    // `created_ids`, the request-wide creation id map (RFC 8620 §5.3) that
    // `/set` handlers use to resolve `#creationId` references, or `using`,
    // the request's capability list (draft-ietf-jmap-metadata-05 §3).
    macro_rules! reg {
        ($method:expr, $backend:expr, |$b:ident, $ci:ident, $a:ident, $ctx:ident $(, $field:ident)*| $body:expr) => {{
            let backend_arc: Arc<B> = Arc::clone(&$backend);
            let h: Arc<dyn JmapHandler<B::CallerCtx>> = Arc::new(ClosureHandler::new(
                backend_arc,
                move |$b: Arc<B>, cx: CallContext<B::CallerCtx>| {
                    let CallContext {
                        call_id: $ci,
                        args: $a,
                        caller: $ctx,
                        $($field,)*
                        ..
                    } = cx;
                    Box::pin(async move { $body }) as HandlerFuture
                },
            ));
            dispatcher.register($method, h);
        }};
    }

    // Chat
    reg!("Chat/get", backend, |b, _ci, a, ctx, using| {
        handle_chat_get(&*b, &ctx, a, &using).await
    });
    reg!("Chat/changes", backend, |b, _ci, a, ctx, using| {
        handle_chat_changes(&*b, &ctx, a, &using).await
    });
    reg!("Chat/query", backend, |b, _ci, a, ctx, using| {
        handle_chat_query(&*b, &ctx, a, &using).await
    });
    reg!("Chat/queryChanges", backend, |b, _ci, a, ctx, using| {
        handle_chat_query_changes(&*b, &ctx, a, &using).await
    });
    reg!("Chat/set", backend, |b, _ci, a, ctx, created_ids, using| {
        handle_chat_set(&*b, &ctx, &created_ids, a, &using).await
    });
    reg!("Chat/typing", backend, |b, _ci, a, ctx| {
        handle_chat_typing(&*b, &ctx, a).await
    });

    // Message
    reg!("Message/get", backend, |b, _ci, a, ctx, using| {
        message::handle_message_get(&*b, &ctx, a, &using).await
    });
    reg!("Message/changes", backend, |b, _ci, a, ctx, using| {
        message::handle_message_changes(&*b, &ctx, a, &using).await
    });
    reg!("Message/query", backend, |b, _ci, a, ctx, using| {
        message::handle_message_query(&*b, &ctx, a, &using).await
    });
    reg!("Message/queryChanges", backend, |b, _ci, a, ctx, using| {
        message::handle_message_query_changes(&*b, &ctx, a, &using).await
    });
    reg!(
        "Message/set",
        backend,
        |b, _ci, a, ctx, created_ids, using| {
            message::handle_message_set(&*b, &ctx, &created_ids, a, &using).await
        }
    );

    // Space
    reg!("Space/get", backend, |b, _ci, a, ctx, using| {
        handle_space_get(&*b, &ctx, a, &using).await
    });
    reg!("Space/changes", backend, |b, _ci, a, ctx, using| {
        handle_space_changes(&*b, &ctx, a, &using).await
    });
    reg!("Space/query", backend, |b, _ci, a, ctx, using| {
        handle_space_query(&*b, &ctx, a, &using).await
    });
    reg!("Space/queryChanges", backend, |b, _ci, a, ctx, using| {
        handle_space_query_changes(&*b, &ctx, a, &using).await
    });
    reg!(
        "Space/set",
        backend,
        |b, _ci, a, ctx, created_ids, using| {
            handle_space_set(&*b, &ctx, &created_ids, a, &using).await
        }
    );
    reg!("Space/join", backend, |b, _ci, a, ctx| {
        handle_space_join(&*b, &ctx, a).await
    });

    // ChatContact
    reg!("ChatContact/get", backend, |b, _ci, a, ctx| {
        handle_contact_get(&*b, &ctx, a).await
    });
    reg!("ChatContact/changes", backend, |b, _ci, a, ctx| {
        handle_contact_changes(&*b, &ctx, a).await
    });
    reg!("ChatContact/query", backend, |b, _ci, a, ctx| {
        handle_contact_query(&*b, &ctx, a).await
    });
    reg!("ChatContact/queryChanges", backend, |b, _ci, a, ctx| {
        handle_contact_query_changes(&*b, &ctx, a).await
    });
    reg!("ChatContact/set", backend, |b, _ci, a, ctx| {
        handle_contact_set(&*b, &ctx, a).await
    });

    // ReadPosition
    reg!("ReadPosition/get", backend, |b, _ci, a, ctx| {
        handle_position_get(&*b, &ctx, a).await
    });
    reg!("ReadPosition/changes", backend, |b, _ci, a, ctx| {
        handle_position_changes(&*b, &ctx, a).await
    });
    reg!(
        "ReadPosition/set",
        backend,
        |b, _ci, a, ctx, created_ids| handle_position_set(&*b, &ctx, &created_ids, a).await
    );

    // SpaceInvite
    reg!("SpaceInvite/get", backend, |b, _ci, a, ctx| {
        handle_invite_get(&*b, &ctx, a).await
    });
    reg!("SpaceInvite/changes", backend, |b, _ci, a, ctx| {
        handle_invite_changes(&*b, &ctx, a).await
    });
    reg!("SpaceInvite/set", backend, |b, _ci, a, ctx, created_ids| {
        handle_invite_set(&*b, &ctx, &created_ids, a).await
    });

    // SpaceBan
    reg!("SpaceBan/get", backend, |b, _ci, a, ctx| {
        handle_ban_get(&*b, &ctx, a).await
    });
    reg!("SpaceBan/changes", backend, |b, _ci, a, ctx| {
        handle_ban_changes(&*b, &ctx, a).await
    });
    reg!("SpaceBan/set", backend, |b, _ci, a, ctx, created_ids| {
        handle_ban_set(&*b, &ctx, &created_ids, a).await
    });

    // ChatBan
    reg!("ChatBan/get", backend, |b, _ci, a, ctx| {
        handle_chat_ban_get(&*b, &ctx, a).await
    });
    reg!("ChatBan/changes", backend, |b, _ci, a, ctx| {
        handle_chat_ban_changes(&*b, &ctx, a).await
    });
    reg!("ChatBan/set", backend, |b, _ci, a, ctx, created_ids| {
        handle_chat_ban_set(&*b, &ctx, &created_ids, a).await
    });

    // CustomEmoji
    reg!("CustomEmoji/get", backend, |b, _ci, a, ctx| {
        handle_emoji_get(&*b, &ctx, a).await
    });
    reg!("CustomEmoji/changes", backend, |b, _ci, a, ctx| {
        handle_emoji_changes(&*b, &ctx, a).await
    });
    reg!("CustomEmoji/query", backend, |b, _ci, a, ctx| {
        handle_emoji_query(&*b, &ctx, a).await
    });
    reg!("CustomEmoji/queryChanges", backend, |b, _ci, a, ctx| {
        handle_emoji_query_changes(&*b, &ctx, a).await
    });
    reg!("CustomEmoji/set", backend, |b, _ci, a, ctx, created_ids| {
        handle_emoji_set(&*b, &ctx, &created_ids, a).await
    });

    // PresenceStatus
    reg!("PresenceStatus/get", backend, |b, _ci, a, ctx| {
        handle_presence_get(&*b, &ctx, a).await
    });
    reg!("PresenceStatus/changes", backend, |b, _ci, a, ctx| {
        handle_presence_changes(&*b, &ctx, a).await
    });
    reg!("PresenceStatus/set", backend, |b, _ci, a, ctx| {
        handle_presence_set(&*b, &ctx, a).await
    });
}

/// Opt this crate's registered Foo/set methods in to
/// draft-ietf-jmap-conditional-02 (`ifUnchangedBy` and `atomic`;
/// docs/design/DECISIONS.md decisions 1 and 17).
///
/// Call after [`register_chat_handlers`]. Attaches [`SetSupport`] to each
/// of `Chat/set`, `Message/set`, `Space/set`, `ChatContact/set`,
/// `ReadPosition/set`, `SpaceInvite/set`, `SpaceBan/set`, `ChatBan/set`,
/// `CustomEmoji/set` and `PresenceStatus/set` that is registered, and skips
/// the rest; no handler is re-registered. For `PushSubscription/set`, call
/// [`jmap_server::register_push_subscription_conditional`] too.
///
/// The dispatcher reads preconditions through each type's `/get` and runs
/// each call in the scope `backend` opens
/// ([`ConditionalBackend::begin_set`]). Under `atomic` that scope must hold
/// back outbound federation and lifecycle effects until commit and check
/// cross-object constraints (CustomEmoji names, the push subscription cap)
/// against the final state; the memory backend does. Advertise
/// `urn:ietf:params:jmap:conditional` only once
/// [`Dispatcher::conditional_ready`] is true for the whole dispatcher.
pub fn register_chat_conditional<B>(dispatcher: &mut Dispatcher<B::CallerCtx>, backend: Arc<B>)
where
    B: ChatBackend + ConditionalBackend + 'static,
{
    fn attach<C: Clone + Send + 'static>(
        dispatcher: &mut Dispatcher<C>,
        method: &str,
        support: SetSupport<C>,
    ) {
        match dispatcher.attach_set_support(method, support) {
            Ok(()) | Err(RegistrationError::NotRegistered(_)) => {}
            Err(e) => unreachable!("{method} pairs with its own data type: {e}"),
        }
    }
    macro_rules! attach {
        ($($t:ident),*) => {$(
            attach(
                dispatcher,
                concat!(stringify!($t), "/set"),
                SetSupport::new::<jmap_chat_types::$t, B>(Arc::clone(&backend)),
            );
        )*};
    }
    attach!(
        Chat,
        Message,
        Space,
        ChatContact,
        ReadPosition,
        SpaceInvite,
        SpaceBan,
        ChatBan,
        CustomEmoji,
        PresenceStatus
    );
}