use std::collections::BTreeMap;
use std::sync::Arc;
use jmap_server::{
CallContext, ClosureHandler, Dispatcher, HandlerFuture, HandlerOutput, JmapHandler,
NoRestoreHooks, ObjectHistoryBackend, history_get, history_query, history_query_changes,
history_set,
};
use jmap_tasks_types::{Task, TaskList};
use jmap_types::{Id, JmapError};
use serde_json::Value;
use crate::{
TasksBackend, handle_task_get, handle_task_list_get, handle_task_list_set, handle_task_query,
handle_task_query_changes, handle_task_set, register_tasks_handlers,
};
pub async fn handle_task_list_get_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_get::<TaskList, B, _, _>(backend, caller, args, using, |a| {
handle_task_list_get(backend, caller, a, using)
})
.await
}
pub async fn handle_task_list_set_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_set::<TaskList, B, _, _>(backend, caller, args, using, &NoRestoreHooks, |a| {
handle_task_list_set(backend, caller, a, using)
})
.await
}
pub async fn handle_task_get_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_get::<Task, B, _, _>(backend, caller, args, using, |a| {
handle_task_get(backend, caller, a, using)
})
.await
}
pub async fn handle_task_query_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_query::<Task, B, _, _>(backend, caller, args, using, |a| {
handle_task_query(backend, caller, a, using)
})
.await
}
pub async fn handle_task_query_changes_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_query_changes::<Task, B, _, _>(backend, caller, args, using, |a| {
handle_task_query_changes(backend, caller, a, using)
})
.await
}
pub async fn handle_task_set_with_history<B: TasksBackend + ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
created_ids: &BTreeMap<Id, Id>,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_set::<Task, B, _, _>(backend, caller, args, using, &NoRestoreHooks, |a| {
handle_task_set(backend, caller, created_ids, a, using)
})
.await
}
pub fn register_tasks_handlers_with_history<B>(
dispatcher: &mut Dispatcher<B::CallerCtx>,
backend: Arc<B>,
) where
B: TasksBackend + ObjectHistoryBackend + 'static,
{
register_tasks_handlers(dispatcher, Arc::clone(&backend));
macro_rules! reg {
($method:expr, |$b:ident, $a:ident, $ctx:ident, $using:ident $(, $field:ident)*| $body:expr) => {{
let h: Arc<dyn JmapHandler<B::CallerCtx>> = Arc::new(ClosureHandler::new(
Arc::clone(&backend),
move |$b: Arc<B>, cx: CallContext<B::CallerCtx>| {
let CallContext {
args: $a,
caller: $ctx,
using: $using,
$($field,)*
..
} = cx;
Box::pin(async move { $body }) as HandlerFuture
},
));
dispatcher.register_history_aware($method, h);
}};
}
reg!("TaskList/get", |b, a, ctx, using| {
handle_task_list_get_with_history(&*b, &ctx, a, &using).await
});
reg!("TaskList/set", |b, a, ctx, using| {
handle_task_list_set_with_history(&*b, &ctx, a, &using).await
});
reg!("Task/get", |b, a, ctx, using| {
handle_task_get_with_history(&*b, &ctx, a, &using).await
});
reg!("Task/query", |b, a, ctx, using| {
handle_task_query_with_history(&*b, &ctx, a, &using).await
});
reg!("Task/queryChanges", |b, a, ctx, using| {
handle_task_query_changes_with_history(&*b, &ctx, a, &using).await
});
reg!("Task/set", |b, a, ctx, using, created_ids| {
handle_task_set_with_history(&*b, &ctx, &created_ids, a, &using).await
});
}