Skip to main content

jmap_chat_server/backend/
delegate.rs

1//! `Delegate*` companions of the chat hook traits: implement one to forward a whole hook
2//! trait to another backend (docs/design/chat-backend-split.md §3.2).
3//!
4//! A delegated hook trait runs with the target as `self`. When a default of one hook trait
5//! calls another (the [`ChatModeration`] defaults write through [`ChatStore`]; the
6//! [`ChatStore`] defaults report through [`ChatEvents`]), a delegated trait reaches the
7//! target's implementation, not the wrapper's. A wrapper that needs its own behaviour under a
8//! delegated trait implements that trait itself.
9#![allow(clippy::too_many_arguments)]
10
11use super::*;
12
13/// Forward every [`ChatStore`] method to [`chat_store`](Self::chat_store)'s target.
14///
15/// Implement this instead of [`ChatStore`]; a type implements one or the other, never both.
16pub trait DelegateChatStore: JmapBackend {
17    /// The backend that answers the [`ChatStore`] methods.
18    type Target: ChatStore<CallerCtx = Self::CallerCtx, Error = Self::Error>;
19    /// The target, usually the wrapped backend.
20    fn chat_store(&self) -> &Self::Target;
21}
22
23impl<T: DelegateChatStore + ChatEvents> ChatStore for T {
24    fn create_object<O, F>(
25        &self,
26        caller: &Self::CallerCtx,
27        account_id: &jmap_types::Id,
28        create_id: &str,
29        build: F,
30    ) -> impl std::future::Future<Output = Result<(jmap_types::Id, O), BackendSetError<Self::Error>>>
31    + Send
32    where
33        O: SetObject + Send + Sync,
34        F: FnOnce(&jmap_types::Id) -> O + Send,
35    {
36        self.chat_store()
37            .create_object::<O, F>(caller, account_id, create_id, build)
38    }
39
40    fn create_message<F>(
41        &self,
42        caller: &Self::CallerCtx,
43        account_id: &jmap_types::Id,
44        create_id: &str,
45        build: F,
46    ) -> impl std::future::Future<
47        Output = Result<(jmap_types::Id, jmap_chat_types::Message), BackendSetError<Self::Error>>,
48    > + Send
49    where
50        F: FnOnce(&jmap_types::Id) -> jmap_chat_types::Message + Send,
51    {
52        self.chat_store()
53            .create_message::<F>(caller, account_id, create_id, build)
54    }
55
56    fn update_object<O: SetObject + Send + Sync>(
57        &self,
58        caller: &Self::CallerCtx,
59        account_id: &jmap_types::Id,
60        id: &jmap_types::Id,
61        patch: O::Patch,
62    ) -> impl std::future::Future<Output = Result<Option<O>, BackendSetError<Self::Error>>> + Send
63    {
64        self.chat_store()
65            .update_object::<O>(caller, account_id, id, patch)
66    }
67
68    fn destroy_object<O: SetObject + Send + Sync>(
69        &self,
70        caller: &Self::CallerCtx,
71        account_id: &jmap_types::Id,
72        id: &jmap_types::Id,
73    ) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send {
74        self.chat_store()
75            .destroy_object::<O>(caller, account_id, id)
76    }
77
78    fn supports_type<O: JmapObject>(
79        &self,
80        caller: &Self::CallerCtx,
81        account_id: &jmap_types::Id,
82    ) -> bool {
83        self.chat_store().supports_type::<O>(caller, account_id)
84    }
85}
86
87/// Forward every [`ChatSpaces`] method to [`chat_spaces`](Self::chat_spaces)'s target.
88///
89/// Implement this instead of [`ChatSpaces`]; a type implements one or the other, never both.
90pub trait DelegateChatSpaces: JmapBackend {
91    /// The backend that answers the [`ChatSpaces`] methods.
92    type Target: ChatSpaces<CallerCtx = Self::CallerCtx, Error = Self::Error>;
93    /// The target, usually the wrapped backend.
94    fn chat_spaces(&self) -> &Self::Target;
95}
96
97impl<T: DelegateChatSpaces> ChatSpaces for T {
98    fn generate_invite_code(
99        &self,
100        caller: &Self::CallerCtx,
101        account_id: &jmap_types::Id,
102    ) -> impl std::future::Future<Output = Result<String, Self::Error>> + Send {
103        self.chat_spaces().generate_invite_code(caller, account_id)
104    }
105
106    fn join_space_with_invite(
107        &self,
108        caller: &Self::CallerCtx,
109        account_id: &jmap_types::Id,
110        invite_id: &jmap_types::Id,
111        joined_at: &jmap_types::UTCDate,
112    ) -> impl std::future::Future<Output = Result<Result<(), InviteJoinRefusal>, Self::Error>> + Send
113    {
114        self.chat_spaces()
115            .join_space_with_invite(caller, account_id, invite_id, joined_at)
116    }
117
118    fn apply_space_patch(
119        &self,
120        caller: &Self::CallerCtx,
121        account_id: &jmap_types::Id,
122        space_id: &jmap_types::Id,
123        ops: Vec<SpacePatchOp>,
124    ) -> impl std::future::Future<Output = Result<Vec<OpResult>, BackendSetError<Self::Error>>> + Send
125    {
126        self.chat_spaces()
127            .apply_space_patch(caller, account_id, space_id, ops)
128    }
129
130    fn apply_space_metadata_patch(
131        &self,
132        caller: &Self::CallerCtx,
133        account_id: &jmap_types::Id,
134        space_id: &jmap_types::Id,
135        patch: jmap_chat_types::SpaceMetadataPatch,
136    ) -> impl std::future::Future<
137        Output = Result<Option<jmap_chat_types::Space>, BackendSetError<Self::Error>>,
138    > + Send {
139        self.chat_spaces()
140            .apply_space_metadata_patch(caller, account_id, space_id, patch)
141    }
142
143    fn space_extension_properties(
144        &self,
145        caller: &Self::CallerCtx,
146        account_id: &jmap_types::Id,
147    ) -> &'static [&'static str] {
148        self.chat_spaces()
149            .space_extension_properties(caller, account_id)
150    }
151
152    fn protect_last_admin(&self, caller: &Self::CallerCtx, account_id: &jmap_types::Id) -> bool {
153        self.chat_spaces().protect_last_admin(caller, account_id)
154    }
155
156    fn everyone_permissions(
157        &self,
158        caller: &Self::CallerCtx,
159        account_id: &jmap_types::Id,
160        space: &jmap_chat_types::Space,
161    ) -> impl std::future::Future<
162        Output = Result<std::collections::HashSet<jmap_chat_types::ChatPermission>, Self::Error>,
163    > + Send {
164        self.chat_spaces()
165            .everyone_permissions(caller, account_id, space)
166    }
167}
168
169/// Forward every [`SpacePolicy`] method to [`space_policy`](Self::space_policy)'s target.
170///
171/// Implement this instead of [`SpacePolicy`]; a type implements one or the other, never both.
172pub trait DelegateSpacePolicy: JmapBackend {
173    /// The backend that answers the [`SpacePolicy`] methods.
174    type Target: SpacePolicy<CallerCtx = Self::CallerCtx, Error = Self::Error>;
175    /// The target, usually the wrapped backend.
176    fn space_policy(&self) -> &Self::Target;
177}
178
179impl<T: DelegateSpacePolicy> SpacePolicy for T {
180    fn may_set_custom_emoji(
181        &self,
182        _caller: &Self::CallerCtx,
183        _account_id: &jmap_types::Id,
184        _target_space_id: Option<&jmap_types::Id>,
185        _op: EmojiSetOp,
186    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
187        self.space_policy()
188            .may_set_custom_emoji(_caller, _account_id, _target_space_id, _op)
189    }
190
191    fn may_set_space_ban(
192        &self,
193        _caller: &Self::CallerCtx,
194        _account_id: &jmap_types::Id,
195        _space_id: &jmap_types::Id,
196    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
197        self.space_policy()
198            .may_set_space_ban(_caller, _account_id, _space_id)
199    }
200
201    fn may_set_space_invite(
202        &self,
203        _caller: &Self::CallerCtx,
204        _account_id: &jmap_types::Id,
205        _space_id: &jmap_types::Id,
206        _created_by: Option<&jmap_types::Id>,
207    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
208        self.space_policy()
209            .may_set_space_invite(_caller, _account_id, _space_id, _created_by)
210    }
211
212    fn may_get_space_invite(
213        &self,
214        _caller: &Self::CallerCtx,
215        _account_id: &jmap_types::Id,
216        _invite: &jmap_chat_types::SpaceInvite,
217    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
218        self.space_policy()
219            .may_get_space_invite(_caller, _account_id, _invite)
220    }
221
222    fn may_get_space_ban(
223        &self,
224        _caller: &Self::CallerCtx,
225        _account_id: &jmap_types::Id,
226        _ban: &jmap_chat_types::SpaceBan,
227    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
228        self.space_policy()
229            .may_get_space_ban(_caller, _account_id, _ban)
230    }
231}
232
233/// Forward every [`ChatAccessPolicy`] method to [`chat_access_policy`](Self::chat_access_policy)'s target.
234///
235/// Implement this instead of [`ChatAccessPolicy`]; a type implements one or the other, never both.
236pub trait DelegateChatAccessPolicy: JmapBackend {
237    /// The backend that answers the [`ChatAccessPolicy`] methods.
238    type Target: ChatAccessPolicy<CallerCtx = Self::CallerCtx, Error = Self::Error>;
239    /// The target, usually the wrapped backend.
240    fn chat_access_policy(&self) -> &Self::Target;
241}
242
243impl<T: DelegateChatAccessPolicy> ChatAccessPolicy for T {
244    fn is_contact_blocked(
245        &self,
246        caller: &Self::CallerCtx,
247        account_id: &jmap_types::Id,
248        contact_id: &jmap_types::Id,
249    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
250        self.chat_access_policy()
251            .is_contact_blocked(caller, account_id, contact_id)
252    }
253
254    fn may_broadcast_mention(
255        &self,
256        _caller: &Self::CallerCtx,
257        _account_id: &jmap_types::Id,
258        _space_id: &jmap_types::Id,
259        _chat_id: &jmap_types::Id,
260    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
261        self.chat_access_policy()
262            .may_broadcast_mention(_caller, _account_id, _space_id, _chat_id)
263    }
264
265    fn may_set_pinned_messages(
266        &self,
267        _caller: &Self::CallerCtx,
268        _account_id: &jmap_types::Id,
269        _chat_id: &jmap_types::Id,
270    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
271        self.chat_access_policy()
272            .may_set_pinned_messages(_caller, _account_id, _chat_id)
273    }
274
275    fn may_send_message(
276        &self,
277        _caller: &Self::CallerCtx,
278        _account_id: &jmap_types::Id,
279        _chat_id: &jmap_types::Id,
280    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
281        self.chat_access_policy()
282            .may_send_message(_caller, _account_id, _chat_id)
283    }
284
285    fn may_view_chat(
286        &self,
287        _caller: &Self::CallerCtx,
288        _account_id: &jmap_types::Id,
289        _chat_id: &jmap_types::Id,
290    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
291        self.chat_access_policy()
292            .may_view_chat(_caller, _account_id, _chat_id)
293    }
294
295    fn validate_pinned_message_ids(
296        &self,
297        caller: &Self::CallerCtx,
298        account_id: &jmap_types::Id,
299        chat_id: &jmap_types::Id,
300        added: &[jmap_types::Id],
301    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
302        self.chat_access_policy()
303            .validate_pinned_message_ids(caller, account_id, chat_id, added)
304    }
305
306    fn slow_mode_check(
307        &self,
308        _caller: &Self::CallerCtx,
309        _account_id: &jmap_types::Id,
310        _chat_id: &jmap_types::Id,
311    ) -> impl std::future::Future<Output = Result<(), SlowModeError>> + Send {
312        self.chat_access_policy()
313            .slow_mode_check(_caller, _account_id, _chat_id)
314    }
315}
316
317/// Forward every [`ChatRestrictions`] method to [`chat_restrictions`](Self::chat_restrictions)'s target.
318///
319/// Implement this instead of [`ChatRestrictions`]; a type implements one or the other, never both.
320pub trait DelegateChatRestrictions: JmapBackend {
321    /// The backend that answers the [`ChatRestrictions`] methods.
322    type Target: ChatRestrictions<CallerCtx = Self::CallerCtx, Error = Self::Error>;
323    /// The target, usually the wrapped backend.
324    fn chat_restrictions(&self) -> &Self::Target;
325}
326
327impl<T: DelegateChatRestrictions> ChatRestrictions for T {
328    fn may_create_message(
329        &self,
330        _caller: &Self::CallerCtx,
331        _account_id: &jmap_types::Id,
332        _chat: &jmap_chat_types::Chat,
333    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
334        self.chat_restrictions()
335            .may_create_message(_caller, _account_id, _chat)
336    }
337
338    fn may_update_message(
339        &self,
340        _caller: &Self::CallerCtx,
341        _account_id: &jmap_types::Id,
342        _chat: &jmap_chat_types::Chat,
343        _message: &jmap_chat_types::Message,
344        _patch_keys: &[&str],
345    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
346        self.chat_restrictions().may_update_message(
347            _caller,
348            _account_id,
349            _chat,
350            _message,
351            _patch_keys,
352        )
353    }
354
355    fn may_update_chat(
356        &self,
357        _caller: &Self::CallerCtx,
358        _account_id: &jmap_types::Id,
359        _chat: &jmap_chat_types::Chat,
360        _patch_keys: &[&str],
361    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
362        self.chat_restrictions()
363            .may_update_chat(_caller, _account_id, _chat, _patch_keys)
364    }
365}
366
367/// Forward every [`ChatModeration`] method to [`chat_moderation`](Self::chat_moderation)'s target.
368///
369/// Implement this instead of [`ChatModeration`]; a type implements one or the other, never both.
370pub trait DelegateChatModeration: JmapBackend {
371    /// The backend that answers the [`ChatModeration`] methods.
372    type Target: ChatModeration<CallerCtx = Self::CallerCtx, Error = Self::Error>;
373    /// The target, usually the wrapped backend.
374    fn chat_moderation(&self) -> &Self::Target;
375}
376
377impl<T: DelegateChatModeration + ChatStore> ChatModeration for T {
378    fn create_chat_ban<F>(
379        &self,
380        caller: &Self::CallerCtx,
381        account_id: &jmap_types::Id,
382        create_id: &str,
383        build: F,
384    ) -> impl std::future::Future<
385        Output = Result<(jmap_types::Id, jmap_chat_types::ChatBan), BackendSetError<Self::Error>>,
386    > + Send
387    where
388        F: FnOnce(&jmap_types::Id) -> jmap_chat_types::ChatBan + Send,
389    {
390        self.chat_moderation()
391            .create_chat_ban::<F>(caller, account_id, create_id, build)
392    }
393
394    fn update_chat_ban(
395        &self,
396        caller: &Self::CallerCtx,
397        account_id: &jmap_types::Id,
398        id: &jmap_types::Id,
399        patch: jmap_types::PatchObject,
400    ) -> impl std::future::Future<
401        Output = Result<Option<jmap_chat_types::ChatBan>, BackendSetError<Self::Error>>,
402    > + Send {
403        self.chat_moderation()
404            .update_chat_ban(caller, account_id, id, patch)
405    }
406
407    fn destroy_chat_ban(
408        &self,
409        caller: &Self::CallerCtx,
410        account_id: &jmap_types::Id,
411        id: &jmap_types::Id,
412    ) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send {
413        self.chat_moderation()
414            .destroy_chat_ban(caller, account_id, id)
415    }
416
417    fn kick_chat_members(
418        &self,
419        _caller: &Self::CallerCtx,
420        _account_id: &jmap_types::Id,
421        _chat: &jmap_chat_types::Chat,
422        _user_ids: &[jmap_types::Id],
423    ) -> impl std::future::Future<Output = Result<Result<(), SetError>, Self::Error>> + Send {
424        self.chat_moderation()
425            .kick_chat_members(_caller, _account_id, _chat, _user_ids)
426    }
427}
428
429/// Forward every [`ChatMessageLifecycle`] method to [`chat_message_lifecycle`](Self::chat_message_lifecycle)'s target.
430///
431/// Implement this instead of [`ChatMessageLifecycle`]; a type implements one or the other, never both.
432pub trait DelegateChatMessageLifecycle: JmapBackend {
433    /// The backend that answers the [`ChatMessageLifecycle`] methods.
434    type Target: ChatMessageLifecycle<CallerCtx = Self::CallerCtx, Error = Self::Error>;
435    /// The target, usually the wrapped backend.
436    fn chat_message_lifecycle(&self) -> &Self::Target;
437}
438
439impl<T: DelegateChatMessageLifecycle> ChatMessageLifecycle for T {
440    fn read_and_burn_message(
441        &self,
442        caller: &Self::CallerCtx,
443        account_id: &jmap_types::Id,
444        message_id: &jmap_types::Id,
445        patch: jmap_types::PatchObject,
446    ) -> impl std::future::Future<Output = Result<(), BackendSetError<Self::Error>>> + Send {
447        self.chat_message_lifecycle()
448            .read_and_burn_message(caller, account_id, message_id, patch)
449    }
450
451    fn expire_message(
452        &self,
453        _caller: &Self::CallerCtx,
454        _account_id: &jmap_types::Id,
455        _message_id: &jmap_types::Id,
456    ) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send {
457        self.chat_message_lifecycle()
458            .expire_message(_caller, _account_id, _message_id)
459    }
460}
461
462/// Forward every [`ChatEvents`] method to [`chat_events`](Self::chat_events)'s target.
463///
464/// Implement this instead of [`ChatEvents`]; a type implements one or the other, never both.
465pub trait DelegateChatEvents: JmapBackend {
466    /// The backend that answers the [`ChatEvents`] methods.
467    type Target: ChatEvents<CallerCtx = Self::CallerCtx, Error = Self::Error>;
468    /// The target, usually the wrapped backend.
469    fn chat_events(&self) -> &Self::Target;
470}
471
472impl<T: DelegateChatEvents> ChatEvents for T {
473    fn on_chat_destroyed(
474        &self,
475        caller: &Self::CallerCtx,
476        account_id: &jmap_types::Id,
477        chat_id: &jmap_types::Id,
478    ) -> impl std::future::Future<Output = ()> + Send {
479        self.chat_events()
480            .on_chat_destroyed(caller, account_id, chat_id)
481    }
482
483    fn on_space_destroyed(
484        &self,
485        caller: &Self::CallerCtx,
486        account_id: &jmap_types::Id,
487        space_id: &jmap_types::Id,
488    ) -> impl std::future::Future<Output = ()> + Send {
489        self.chat_events()
490            .on_space_destroyed(caller, account_id, space_id)
491    }
492
493    fn on_space_ban_created(
494        &self,
495        caller: &Self::CallerCtx,
496        account_id: &jmap_types::Id,
497        ban: &jmap_chat_types::SpaceBan,
498    ) -> impl std::future::Future<Output = ()> + Send {
499        self.chat_events()
500            .on_space_ban_created(caller, account_id, ban)
501    }
502
503    fn on_outbound_event(
504        &self,
505        caller: &Self::CallerCtx,
506        account_id: &jmap_types::Id,
507        event: &crate::OutboundEvent,
508    ) -> impl std::future::Future<Output = ()> + Send {
509        self.chat_events()
510            .on_outbound_event(caller, account_id, event)
511    }
512}
513
514/// Forward every [`ChatSettings`] method to [`chat_settings`](Self::chat_settings)'s target.
515///
516/// Implement this instead of [`ChatSettings`]; a type implements one or the other, never both.
517pub trait DelegateChatSettings: JmapBackend {
518    /// The backend that answers the [`ChatSettings`] methods.
519    type Target: ChatSettings<CallerCtx = Self::CallerCtx, Error = Self::Error>;
520    /// The target, usually the wrapped backend.
521    fn chat_settings(&self) -> &Self::Target;
522}
523
524impl<T: DelegateChatSettings> ChatSettings for T {
525    fn limits(&self, _caller: &Self::CallerCtx, _account_id: &jmap_types::Id) -> ChatLimits {
526        self.chat_settings().limits(_caller, _account_id)
527    }
528
529    fn retains_edit_history(&self) -> bool {
530        self.chat_settings().retains_edit_history()
531    }
532
533    fn maintains_unread_count(&self) -> bool {
534        self.chat_settings().maintains_unread_count()
535    }
536
537    fn owner_user_id(
538        &self,
539        _caller: &Self::CallerCtx,
540        _account_id: &jmap_types::Id,
541    ) -> impl std::future::Future<Output = Result<Option<String>, Self::Error>> + Send {
542        self.chat_settings().owner_user_id(_caller, _account_id)
543    }
544
545    fn search_capability(
546        &self,
547        _caller: &Self::CallerCtx,
548        _account_id: &jmap_types::Id,
549    ) -> impl std::future::Future<
550        Output = Result<Option<jmap_search::SearchCapability>, Self::Error>,
551    > + Send {
552        self.chat_settings().search_capability(_caller, _account_id)
553    }
554}
555
556/// Forward every [`ChatResolvers`] method to [`chat_resolvers`](Self::chat_resolvers)'s target.
557///
558/// Implement this instead of [`ChatResolvers`]; a type implements one or the other, never both.
559pub trait DelegateChatResolvers: JmapBackend {
560    /// The backend that answers the [`ChatResolvers`] methods.
561    type Target: ChatResolvers<CallerCtx = Self::CallerCtx, Error = Self::Error>;
562    /// The target, usually the wrapped backend.
563    fn chat_resolvers(&self) -> &Self::Target;
564}
565
566impl<T: DelegateChatResolvers> ChatResolvers for T {
567    fn attachment_blob(
568        &self,
569        caller: &Self::CallerCtx,
570        account_id: &jmap_types::Id,
571        blob_id: &jmap_types::Id,
572    ) -> impl std::future::Future<
573        Output = Result<Result<Option<AttachmentBlob>, SetError>, Self::Error>,
574    > + Send {
575        self.chat_resolvers()
576            .attachment_blob(caller, account_id, blob_id)
577    }
578
579    fn filenode_attachment(
580        &self,
581        _caller: &Self::CallerCtx,
582        _account_id: &jmap_types::Id,
583        _chat: &jmap_chat_types::Chat,
584        _attachment: &jmap_chat_types::AttachmentCreate,
585    ) -> impl std::future::Future<
586        Output = Result<Result<Option<jmap_chat_types::Attachment>, SetError>, Self::Error>,
587    > + Send {
588        self.chat_resolvers()
589            .filenode_attachment(_caller, _account_id, _chat, _attachment)
590    }
591}