#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![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;
#[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;
#[cfg(feature = "websocket")]
pub mod websocket;
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,
};
pub fn register_chat_handlers<B>(dispatcher: &mut Dispatcher<B::CallerCtx>, backend: Arc<B>)
where
B: ChatBackend + 'static,
{
register_chat_message_handlers(dispatcher, &backend);
register_space_contact_handlers(dispatcher, &backend);
register_space_object_handlers(dispatcher, &backend);
}
macro_rules! reg {
($d:ident, $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
},
));
$d.register($method, h);
}};
}
fn register_chat_message_handlers<B>(d: &mut Dispatcher<B::CallerCtx>, backend: &Arc<B>)
where
B: ChatBackend + 'static,
{
reg!(d, "Chat/get", backend, |b, _ci, a, ctx, using| {
handle_chat_get(&*b, &ctx, a, &using).await
});
reg!(d, "Chat/changes", backend, |b, _ci, a, ctx, using| {
handle_chat_changes(&*b, &ctx, a, &using).await
});
reg!(d, "Chat/query", backend, |b, _ci, a, ctx, using| {
handle_chat_query(&*b, &ctx, a, &using).await
});
reg!(d, "Chat/queryChanges", backend, |b, _ci, a, ctx, using| {
handle_chat_query_changes(&*b, &ctx, a, &using).await
});
reg!(
d,
"Chat/set",
backend,
|b, _ci, a, ctx, created_ids, using| {
handle_chat_set(&*b, &ctx, &created_ids, a, &using).await
}
);
reg!(d, "Chat/typing", backend, |b, _ci, a, ctx| {
handle_chat_typing(&*b, &ctx, a).await
});
reg!(d, "Message/get", backend, |b, _ci, a, ctx, using| {
message::handle_message_get(&*b, &ctx, a, &using).await
});
reg!(d, "Message/changes", backend, |b, _ci, a, ctx, using| {
message::handle_message_changes(&*b, &ctx, a, &using).await
});
reg!(d, "Message/query", backend, |b, _ci, a, ctx, using| {
message::handle_message_query(&*b, &ctx, a, &using).await
});
reg!(
d,
"Message/queryChanges",
backend,
|b, _ci, a, ctx, using| {
message::handle_message_query_changes(&*b, &ctx, a, &using).await
}
);
reg!(
d,
"Message/set",
backend,
|b, _ci, a, ctx, created_ids, using| {
message::handle_message_set(&*b, &ctx, &created_ids, a, &using).await
}
);
}
fn register_space_contact_handlers<B>(d: &mut Dispatcher<B::CallerCtx>, backend: &Arc<B>)
where
B: ChatBackend + 'static,
{
reg!(d, "Space/get", backend, |b, _ci, a, ctx, using| {
handle_space_get(&*b, &ctx, a, &using).await
});
reg!(d, "Space/changes", backend, |b, _ci, a, ctx, using| {
handle_space_changes(&*b, &ctx, a, &using).await
});
reg!(d, "Space/query", backend, |b, _ci, a, ctx, using| {
handle_space_query(&*b, &ctx, a, &using).await
});
reg!(d, "Space/queryChanges", backend, |b, _ci, a, ctx, using| {
handle_space_query_changes(&*b, &ctx, a, &using).await
});
reg!(
d,
"Space/set",
backend,
|b, _ci, a, ctx, created_ids, using| {
handle_space_set(&*b, &ctx, &created_ids, a, &using).await
}
);
reg!(d, "Space/join", backend, |b, _ci, a, ctx| {
handle_space_join(&*b, &ctx, a).await
});
reg!(d, "ChatContact/get", backend, |b, _ci, a, ctx| {
handle_contact_get(&*b, &ctx, a).await
});
reg!(d, "ChatContact/changes", backend, |b, _ci, a, ctx| {
handle_contact_changes(&*b, &ctx, a).await
});
reg!(d, "ChatContact/query", backend, |b, _ci, a, ctx| {
handle_contact_query(&*b, &ctx, a).await
});
reg!(d, "ChatContact/queryChanges", backend, |b, _ci, a, ctx| {
handle_contact_query_changes(&*b, &ctx, a).await
});
reg!(d, "ChatContact/set", backend, |b, _ci, a, ctx| {
handle_contact_set(&*b, &ctx, a).await
});
reg!(d, "ReadPosition/get", backend, |b, _ci, a, ctx| {
handle_position_get(&*b, &ctx, a).await
});
reg!(d, "ReadPosition/changes", backend, |b, _ci, a, ctx| {
handle_position_changes(&*b, &ctx, a).await
});
reg!(
d,
"ReadPosition/set",
backend,
|b, _ci, a, ctx, created_ids| handle_position_set(&*b, &ctx, &created_ids, a).await
);
}
fn register_space_object_handlers<B>(d: &mut Dispatcher<B::CallerCtx>, backend: &Arc<B>)
where
B: ChatBackend + 'static,
{
reg!(d, "SpaceInvite/get", backend, |b, _ci, a, ctx| {
handle_invite_get(&*b, &ctx, a).await
});
reg!(d, "SpaceInvite/changes", backend, |b, _ci, a, ctx| {
handle_invite_changes(&*b, &ctx, a).await
});
reg!(
d,
"SpaceInvite/set",
backend,
|b, _ci, a, ctx, created_ids| handle_invite_set(&*b, &ctx, &created_ids, a).await
);
reg!(d, "SpaceBan/get", backend, |b, _ci, a, ctx| {
handle_ban_get(&*b, &ctx, a).await
});
reg!(d, "SpaceBan/changes", backend, |b, _ci, a, ctx| {
handle_ban_changes(&*b, &ctx, a).await
});
reg!(d, "SpaceBan/set", backend, |b, _ci, a, ctx, created_ids| {
handle_ban_set(&*b, &ctx, &created_ids, a).await
});
reg!(d, "ChatBan/get", backend, |b, _ci, a, ctx| {
handle_chat_ban_get(&*b, &ctx, a).await
});
reg!(d, "ChatBan/changes", backend, |b, _ci, a, ctx| {
handle_chat_ban_changes(&*b, &ctx, a).await
});
reg!(d, "ChatBan/set", backend, |b, _ci, a, ctx, created_ids| {
handle_chat_ban_set(&*b, &ctx, &created_ids, a).await
});
reg!(d, "CustomEmoji/get", backend, |b, _ci, a, ctx| {
handle_emoji_get(&*b, &ctx, a).await
});
reg!(d, "CustomEmoji/changes", backend, |b, _ci, a, ctx| {
handle_emoji_changes(&*b, &ctx, a).await
});
reg!(d, "CustomEmoji/query", backend, |b, _ci, a, ctx| {
handle_emoji_query(&*b, &ctx, a).await
});
reg!(d, "CustomEmoji/queryChanges", backend, |b, _ci, a, ctx| {
handle_emoji_query_changes(&*b, &ctx, a).await
});
reg!(
d,
"CustomEmoji/set",
backend,
|b, _ci, a, ctx, created_ids| handle_emoji_set(&*b, &ctx, &created_ids, a).await
);
reg!(d, "PresenceStatus/get", backend, |b, _ci, a, ctx| {
handle_presence_get(&*b, &ctx, a).await
});
reg!(d, "PresenceStatus/changes", backend, |b, _ci, a, ctx| {
handle_presence_changes(&*b, &ctx, a).await
});
reg!(d, "PresenceStatus/set", backend, |b, _ci, a, ctx| {
handle_presence_set(&*b, &ctx, a).await
});
}
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
);
}