use std::collections::{BTreeMap, BTreeSet};
use jmap_object_history_types::{
DestroyedQueryArguments, HistoryEntry, HistoryGetArguments, HistoryObject,
JMAP_OBJECT_HISTORY_URI, ObjectHistoryAccountCapability,
};
use jmap_types::{PatchObject, QueryObject, SetObject, State, UTCDate};
use serde_json::{Map, Value, json};
use crate::backend::{
BackendChangesError, BackendQueryError, BackendSetError, JmapBackend, QueryChangesParams,
QueryChangesResult, QueryParams, QueryResult,
};
use crate::handlers::{
handle_get_with_metadata, handle_query_changes_with_i18n, handle_query_with_i18n,
server_fail_from_backend,
};
use crate::helpers::{
MetadataProjection, apply_patch_object, check_patch_paths, enforce_max_objects_in_get,
extract_account_id, filter_arg, json_type_name, not_found_json, optional_arg,
parse_max_changes, query_anchor_args, serialize_value,
};
use crate::i18n::I18nProjection;
use crate::{
Dispatcher, HandlerOutput, Id, JmapError, JmapHandler, SetError, SetErrorType,
backend_set_error_value,
};
pub const OBJECT_HISTORY_PROPERTY: &str = "objectHistory";
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct HistoryGetParams<'a> {
pub ids: Option<&'a [Id]>,
pub properties: Option<&'a [String]>,
pub arguments: &'a HistoryGetArguments,
}
impl<'a> HistoryGetParams<'a> {
pub fn new(arguments: &'a HistoryGetArguments) -> Self {
Self {
ids: None,
properties: None,
arguments,
}
}
#[must_use]
pub fn ids(mut self, ids: Option<&'a [Id]>) -> Self {
self.ids = ids;
self
}
#[must_use]
pub fn properties(mut self, properties: Option<&'a [String]>) -> Self {
self.properties = properties;
self
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct HistoryGetResult<O> {
pub entries: Vec<HistoryEntry<O>>,
pub not_found: Vec<Id>,
}
impl<O> HistoryGetResult<O> {
pub fn new(entries: Vec<HistoryEntry<O>>, not_found: Vec<Id>) -> Self {
Self { entries, not_found }
}
}
#[non_exhaustive]
pub struct DestroyedQueryParams<'a, O: QueryObject> {
pub query: QueryParams<'a, O>,
pub destroyed_after: Option<&'a UTCDate>,
pub destroyed_before: Option<&'a UTCDate>,
}
impl<'a, O: QueryObject> DestroyedQueryParams<'a, O> {
pub fn new(query: QueryParams<'a, O>) -> Self {
Self {
query,
destroyed_after: None,
destroyed_before: None,
}
}
#[must_use]
pub fn destroyed_after(mut self, t: Option<&'a UTCDate>) -> Self {
self.destroyed_after = t;
self
}
#[must_use]
pub fn destroyed_before(mut self, t: Option<&'a UTCDate>) -> Self {
self.destroyed_before = t;
self
}
}
#[non_exhaustive]
pub struct DestroyedQueryChangesParams<'a, O: QueryObject> {
pub query_changes: QueryChangesParams<'a, O>,
pub destroyed_after: Option<&'a UTCDate>,
pub destroyed_before: Option<&'a UTCDate>,
}
impl<'a, O: QueryObject> DestroyedQueryChangesParams<'a, O> {
pub fn new(query_changes: QueryChangesParams<'a, O>) -> Self {
Self {
query_changes,
destroyed_after: None,
destroyed_before: None,
}
}
#[must_use]
pub fn destroyed_after(mut self, t: Option<&'a UTCDate>) -> Self {
self.destroyed_after = t;
self
}
#[must_use]
pub fn destroyed_before(mut self, t: Option<&'a UTCDate>) -> Self {
self.destroyed_before = t;
self
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Restored<O> {
pub stored: O,
pub baseline: Map<String, Value>,
}
impl<O> Restored<O> {
pub fn new(stored: O, baseline: Map<String, Value>) -> Self {
Self { stored, baseline }
}
}
pub trait ObjectHistoryBackend: JmapBackend {
fn object_history_capability(
&self,
caller: &Self::CallerCtx,
account_id: &Id,
) -> Option<ObjectHistoryAccountCapability>;
fn get_object_history<O: HistoryObject>(
&self,
caller: &Self::CallerCtx,
account_id: &Id,
params: HistoryGetParams<'_>,
) -> impl std::future::Future<
Output = Result<HistoryGetResult<O>, BackendQueryError<Self::Error>>,
> + Send;
fn query_destroyed<O: HistoryObject + QueryObject>(
&self,
caller: &Self::CallerCtx,
account_id: &Id,
params: DestroyedQueryParams<'_, O>,
) -> impl std::future::Future<Output = Result<QueryResult, BackendQueryError<Self::Error>>> + Send;
fn query_destroyed_changes<O: HistoryObject + QueryObject>(
&self,
caller: &Self::CallerCtx,
account_id: &Id,
params: DestroyedQueryChangesParams<'_, O>,
) -> impl std::future::Future<
Output = Result<QueryChangesResult, BackendChangesError<Self::Error>>,
> + Send;
fn restore_object<O: HistoryObject + SetObject>(
&self,
caller: &Self::CallerCtx,
account_id: &Id,
id: &Id,
patch: Option<PatchObject>,
) -> impl std::future::Future<Output = Result<Restored<O>, BackendSetError<Self::Error>>> + Send;
}
fn uses_history(using: &[String]) -> bool {
using.iter().any(|u| u == JMAP_OBJECT_HISTORY_URI)
}
fn invalid(msg: impl Into<String>) -> JmapError {
JmapError::invalid_arguments(msg.into())
}
fn without_uri(name: &str) -> JmapError {
invalid(format!(
"{name} needs {JMAP_OBJECT_HISTORY_URI} in using (decision 18)"
))
}
fn take_flag(args: &mut Map<String, Value>, name: &str) -> Result<bool, JmapError> {
match args.remove(name) {
None | Some(Value::Null) => Ok(false),
Some(Value::Bool(b)) => Ok(b),
Some(v) => Err(invalid(format!(
"{name}: expected a boolean, got {}",
json_type_name(&v)
))),
}
}
fn parse_date(name: &str, v: Option<Value>) -> Result<Option<UTCDate>, JmapError> {
match v {
None | Some(Value::Null) => Ok(None),
Some(Value::String(s)) => UTCDate::new_validated(s)
.map(Some)
.map_err(|e| invalid(format!("{name}: {e}"))),
Some(v) => Err(invalid(format!(
"{name}: expected a UTCDate or null, got {}",
json_type_name(&v)
))),
}
}
fn parse_unsigned(name: &str, v: Option<Value>) -> Result<Option<u64>, JmapError> {
match v {
None | Some(Value::Null) => Ok(None),
Some(v) => v.as_u64().map(Some).ok_or_else(|| {
invalid(format!(
"{name}: expected an UnsignedInt, got {}",
json_type_name(&v)
))
}),
}
}
pub fn parse_history_get_arguments(
args: &mut Map<String, Value>,
using: &[String],
) -> Result<Option<HistoryGetArguments>, JmapError> {
let include_replaced = take_flag(args, "includeReplaced")?;
let include_destroyed = take_flag(args, "includeDestroyed")?;
let after = args.remove("historyAfter");
let before = args.remove("historyBefore");
let skip = args.remove("historySkip");
let limit = args.remove("historyLimit");
if !include_replaced && !include_destroyed {
return Ok(None);
}
if !uses_history(using) {
return Err(without_uri(if include_replaced {
"includeReplaced"
} else {
"includeDestroyed"
}));
}
let mut out = HistoryGetArguments::new();
out.include_replaced = include_replaced;
out.include_destroyed = include_destroyed;
out.history_after = parse_date("historyAfter", after)?;
out.history_before = parse_date("historyBefore", before)?;
out.history_skip = parse_unsigned("historySkip", skip)?.unwrap_or(0);
out.history_limit = parse_unsigned("historyLimit", limit)?;
Ok(Some(out))
}
pub fn parse_destroyed_query_arguments(
args: &mut Map<String, Value>,
using: &[String],
) -> Result<Option<DestroyedQueryArguments>, JmapError> {
let only_destroyed = take_flag(args, "onlyDestroyed")?;
let after = parse_date("destroyedAfter", args.remove("destroyedAfter"))?;
let before = parse_date("destroyedBefore", args.remove("destroyedBefore"))?;
let windowed = after.is_some() || before.is_some();
if (only_destroyed || windowed) && !uses_history(using) {
return Err(without_uri(if only_destroyed {
"onlyDestroyed"
} else {
"destroyedAfter/destroyedBefore"
}));
}
if !only_destroyed {
if windowed {
return Err(invalid(
"destroyedAfter and destroyedBefore need onlyDestroyed: true (§4)",
));
}
return Ok(None);
}
let mut out = DestroyedQueryArguments::new();
out.only_destroyed = true;
out.destroyed_after = after;
out.destroyed_before = before;
Ok(Some(out))
}
pub fn parse_restore_argument(
args: &mut Map<String, Value>,
using: &[String],
) -> Result<Map<String, Value>, JmapError> {
let restore = match args.remove("restore") {
None | Some(Value::Null) => Map::new(),
Some(Value::Object(m)) => m,
Some(v) => {
return Err(invalid(format!(
"restore: expected an object or null, got {}",
json_type_name(&v)
)));
}
};
if !restore.is_empty() && !uses_history(using) {
return Err(without_uri("restore"));
}
Ok(restore)
}
pub fn require_history_support<O: HistoryObject, B: ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
) -> Result<(), JmapError> {
match backend.object_history_capability(caller, account_id) {
Some(cap) if cap.supports::<O>() => Ok(()),
_ => Err(invalid(format!(
"{} has no history in this account (supportedTypes)",
O::TYPE_NAME
))),
}
}
pub fn strip_object_history_property(args: &mut Map<String, Value>) -> bool {
let Some(Value::Array(props)) = args.get_mut("properties") else {
return false;
};
let before = props.len();
props.retain(|p| p.as_str() != Some(OBJECT_HISTORY_PROPERTY));
props.len() != before
}
pub fn attach_object_history(
item: &mut Map<String, Value>,
object_history: Value,
properties: Option<&[String]>,
) {
if let Some(props) = properties {
item.retain(|k, _| k == "id" || props.iter().any(|p| p == k));
}
item.insert(OBJECT_HISTORY_PROPERTY.to_owned(), object_history);
}
pub async fn handle_get_with_history<O: HistoryObject, B: ObjectHistoryBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError> {
history_get::<O, B, _, _>(backend, caller, args, using, |args| {
handle_get_with_metadata::<O, B>(backend, caller, args, using)
})
.await
}
pub async fn history_get<O: HistoryObject, B: ObjectHistoryBackend, F, Fut>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
plain: F,
) -> Result<HandlerOutput, JmapError>
where
F: FnOnce(Value) -> Fut,
Fut: std::future::Future<Output = Result<HandlerOutput, JmapError>>,
{
let Value::Object(mut map) = args else {
return plain(args).await;
};
let Some(history) = parse_history_get_arguments(&mut map, using)? else {
strip_object_history_property(&mut map);
return plain(Value::Object(map)).await;
};
strip_object_history_property(&mut map);
let (account_id, mut args) = extract_account_id(Value::Object(map))?;
if !backend
.account_exists(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?
{
return Err(JmapError::account_not_found());
}
require_history_support::<O, B>(backend, caller, &account_id)?;
enforce_max_objects_in_get(&args, backend.max_objects_in_get(caller, &account_id))?;
let i18n = I18nProjection::from_args::<O>(
&mut args,
using,
backend.i18n_info(caller, &account_id).as_ref(),
)?;
let ids: Option<Vec<Id>> = optional_arg(&mut args, "ids", || {
JmapError::invalid_arguments("ids must be an Id array")
})?;
let properties: Option<Vec<String>> = optional_arg(&mut args, "properties", || {
JmapError::invalid_arguments("properties must be a string array")
})?;
let metadata = match backend.metadata_info::<O>(caller, &account_id) {
Some(info) => Some(MetadataProjection::new(
properties.as_deref(),
true,
using,
Some(&info),
)?),
None => None,
};
let roots: Option<Vec<String>> = properties.as_ref().map(|props| {
let mut roots: Vec<String> = Vec::with_capacity(props.len());
for p in props {
let root = match (metadata.is_some(), p.split_once('/')) {
(true, Some((r @ ("metadata" | "privateMetadata"), _))) => r.to_owned(),
_ => p.clone(),
};
if !roots.contains(&root) {
roots.push(root);
}
}
roots
});
let backend_properties = i18n.backend_properties(roots.as_deref());
let result = backend
.get_object_history::<O>(
caller,
&account_id,
HistoryGetParams::new(&history)
.ids(ids.as_deref())
.properties(backend_properties.as_deref()),
)
.await
.map_err(JmapError::from)?;
let state = backend
.get_state::<O>(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?;
let mut list = Vec::with_capacity(result.entries.len());
for entry in result.entries {
let version = entry.object_history.version;
let info = serialize_value(&entry.object_history)?;
let mut item = serialize_value(&entry.object)?;
if let Value::Object(map) = &mut item {
map.remove(OBJECT_HISTORY_PROPERTY);
i18n.apply(map);
if let Some(roots) = &roots {
map.retain(|k, _| k == "id" || roots.iter().any(|p| p == k));
}
}
if let Some(projection) = &metadata {
let source = item.clone();
projection.apply(&source, &mut item);
}
if let Value::Object(map) = &mut item {
attach_object_history(map, info, None);
}
list.push((version, item));
}
sort_entries_by_version(&mut list);
Ok(HandlerOutput::new(json!({
"accountId": account_id.as_ref(),
"state": state.as_ref(),
"list": list.into_iter().map(|(_, v)| v).collect::<Vec<_>>(),
"notFound": not_found_json(&result.not_found),
})))
}
fn sort_entries_by_version(list: &mut [(u64, Value)]) {
let mut first: BTreeMap<String, usize> = BTreeMap::new();
let keys: Vec<(usize, u64)> = list
.iter()
.enumerate()
.map(|(i, (version, item))| {
let id = item
.get("id")
.and_then(Value::as_str)
.unwrap_or("")
.to_owned();
(*first.entry(id).or_insert(i), *version)
})
.collect();
let mut order: Vec<usize> = (0..list.len()).collect();
order.sort_by_key(|&i| keys[i]);
let sorted: Vec<(u64, Value)> = order.iter().map(|&i| list[i].clone()).collect();
for (slot, v) in list.iter_mut().zip(sorted) {
*slot = v;
}
}
pub async fn handle_query_with_history<O, B>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError>
where
O: HistoryObject + QueryObject,
B: ObjectHistoryBackend,
{
history_query::<O, B, _, _>(backend, caller, args, using, |args| {
handle_query_with_i18n::<O, B>(backend, caller, args, using)
})
.await
}
pub async fn history_query<O, B, F, Fut>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
plain: F,
) -> Result<HandlerOutput, JmapError>
where
F: FnOnce(Value) -> Fut,
Fut: std::future::Future<Output = Result<HandlerOutput, JmapError>>,
O: HistoryObject + QueryObject,
B: ObjectHistoryBackend,
{
let Value::Object(mut map) = args else {
return plain(args).await;
};
let Some(destroyed) = parse_destroyed_query_arguments(&mut map, using)? else {
return plain(Value::Object(map)).await;
};
let (account_id, mut args) = extract_account_id(Value::Object(map))?;
if !backend
.account_exists(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?
{
return Err(JmapError::account_not_found());
}
require_history_support::<O, B>(backend, caller, &account_id)?;
let i18n = I18nProjection::from_args::<O>(
&mut args,
using,
backend.i18n_info(caller, &account_id).as_ref(),
)?;
let calculate_total = args
.get("calculateTotal")
.and_then(Value::as_bool)
.unwrap_or(false);
let limit = parse_unsigned("limit", args.remove("limit"))?;
let position: i64 = match args.get("position") {
None | Some(Value::Null) => 0,
Some(v) => v.as_i64().ok_or_else(|| {
invalid(format!(
"position: expected an integer, got {}",
json_type_name(v)
))
})?,
};
let (anchor, anchor_offset) = query_anchor_args(&args)?;
let filter: Option<O::Filter> = filter_arg(&mut args)?;
let sort: Option<Vec<O::Comparator>> = optional_arg(&mut args, "sort", || {
JmapError::invalid_arguments("sort must be an array")
})?;
let result = backend
.query_destroyed::<O>(
caller,
&account_id,
DestroyedQueryParams::new(
QueryParams::new()
.filter(filter.as_ref())
.sort(sort.as_deref())
.limit(limit)
.position(position)
.anchor(anchor.as_ref())
.anchor_offset(anchor_offset)
.language(i18n.language()),
)
.destroyed_after(destroyed.destroyed_after.as_ref())
.destroyed_before(destroyed.destroyed_before.as_ref()),
)
.await
.map_err(JmapError::from)?;
let mut resp = json!({
"accountId": account_id.as_ref(),
"queryState": result.query_state.as_ref(),
"canCalculateChanges": result.can_calculate_changes,
"position": result.position,
"ids": result.ids,
});
if calculate_total && let Some(t) = result.total {
resp["total"] = json!(t);
}
Ok(HandlerOutput::new(resp))
}
pub async fn handle_query_changes_with_history<O, B>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
) -> Result<HandlerOutput, JmapError>
where
O: HistoryObject + QueryObject,
B: ObjectHistoryBackend,
{
history_query_changes::<O, B, _, _>(backend, caller, args, using, |args| {
handle_query_changes_with_i18n::<O, B>(backend, caller, args, using)
})
.await
}
pub async fn history_query_changes<O, B, F, Fut>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
plain: F,
) -> Result<HandlerOutput, JmapError>
where
F: FnOnce(Value) -> Fut,
Fut: std::future::Future<Output = Result<HandlerOutput, JmapError>>,
O: HistoryObject + QueryObject,
B: ObjectHistoryBackend,
{
let Value::Object(mut map) = args else {
return plain(args).await;
};
let Some(destroyed) = parse_destroyed_query_arguments(&mut map, using)? else {
return plain(Value::Object(map)).await;
};
let (account_id, mut args) = extract_account_id(Value::Object(map))?;
if !backend
.account_exists(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?
{
return Err(JmapError::account_not_found());
}
require_history_support::<O, B>(backend, caller, &account_id)?;
let i18n = I18nProjection::from_args::<O>(
&mut args,
using,
backend.i18n_info(caller, &account_id).as_ref(),
)?;
let since: State = match args.get("sinceQueryState").and_then(Value::as_str) {
Some(s) => State::from(s),
None => return Err(invalid("sinceQueryState is required")),
};
let max_changes = parse_max_changes(args.get("maxChanges"))?;
let up_to_id: Option<Id> = match args.get("upToId") {
None | Some(Value::Null) => None,
Some(Value::String(s)) => Some(Id::from(s.as_str())),
Some(_) => return Err(invalid("upToId must be a string Id or null")),
};
let calculate_total = args
.get("calculateTotal")
.and_then(Value::as_bool)
.unwrap_or(false);
let filter: Option<O::Filter> = filter_arg(&mut args)?;
let sort: Option<Vec<O::Comparator>> = optional_arg(&mut args, "sort", || {
JmapError::invalid_arguments("sort must be an array")
})?;
let result = backend
.query_destroyed_changes::<O>(
caller,
&account_id,
DestroyedQueryChangesParams::new(
QueryChangesParams::new(&since)
.filter(filter.as_ref())
.sort(sort.as_deref())
.max_changes(max_changes)
.up_to_id(up_to_id.as_ref())
.language(i18n.language()),
)
.destroyed_after(destroyed.destroyed_after.as_ref())
.destroyed_before(destroyed.destroyed_before.as_ref()),
)
.await
.map_err(JmapError::from)?;
let added: Vec<Value> = result
.added
.iter()
.map(|a| json!({"id": a.id.as_ref(), "index": a.index}))
.collect();
let mut resp = json!({
"accountId": account_id.as_ref(),
"oldQueryState": result.old_query_state.as_ref(),
"newQueryState": result.new_query_state.as_ref(),
"removed": result.removed,
"added": added,
});
if calculate_total && let Some(t) = result.total {
resp["total"] = json!(t);
}
Ok(HandlerOutput::new(resp))
}
pub trait RestoreHooks: Sync {
fn check_patch(&self, id: &Id, patch: Option<&PatchObject>) -> Result<(), Box<SetError>> {
let _ = (id, patch);
Ok(())
}
fn same_type_references(&self, final_version: &Map<String, Value>) -> Vec<Id> {
let _ = final_version;
Vec::new()
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoRestoreHooks;
impl RestoreHooks for NoRestoreHooks {}
#[derive(Debug, Clone, Copy)]
pub struct ParentRestoreHooks(pub &'static str);
impl RestoreHooks for ParentRestoreHooks {
fn same_type_references(&self, final_version: &Map<String, Value>) -> Vec<Id> {
final_version
.get(self.0)
.and_then(Value::as_str)
.map(|s| vec![Id::from(s)])
.unwrap_or_default()
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RestoreAccumulators {
pub restored: Map<String, Value>,
pub not_restored: Map<String, Value>,
pub mutated: bool,
}
fn set_error(t: SetErrorType, description: impl Into<String>) -> SetError {
SetError::new(t).with_description(description)
}
fn set_error_value(e: SetError) -> Value {
serde_json::to_value(e).unwrap_or_else(|_| json!({"type": "serverFail"}))
}
pub async fn run_restore_phase<O, B>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
restore: Map<String, Value>,
hooks: &dyn RestoreHooks,
) -> Result<RestoreAccumulators, JmapError>
where
O: HistoryObject + SetObject,
B: ObjectHistoryBackend,
{
let mut acc = RestoreAccumulators::default();
if restore.is_empty() {
return Ok(acc);
}
require_history_support::<O, B>(backend, caller, account_id)?;
let mut ready: Vec<(String, Id, Option<PatchObject>)> = Vec::new();
for (key, value) in restore {
let Ok(id) = Id::new_validated(key.as_str()) else {
acc.not_restored.insert(
key,
set_error_value(set_error(
SetErrorType::NotFound,
"not the id of a destroyed object",
)),
);
continue;
};
let patch = match value {
Value::Null => None,
Value::Object(m) if m.is_empty() => None,
Value::Object(m) => Some(m),
v => {
acc.not_restored.insert(
key,
set_error_value(set_error(
SetErrorType::InvalidPatch,
format!("expected a PatchObject or null, got {}", json_type_name(&v)),
)),
);
continue;
}
};
if let Some(p) = &patch {
if let Err(e) = check_patch_paths(p) {
acc.not_restored.insert(
key,
set_error_value(set_error(SetErrorType::InvalidPatch, e.to_string())),
);
continue;
}
if p.keys()
.any(|k| k == OBJECT_HISTORY_PROPERTY || k.starts_with("objectHistory/"))
{
acc.not_restored
.insert(key, set_error_value(object_history_set_error()));
continue;
}
}
let patch = patch.map(PatchObject::from_map);
if let Err(e) = hooks.check_patch(&id, patch.as_ref()) {
acc.not_restored.insert(key, set_error_value(*e));
continue;
}
ready.push((key, id, patch));
}
let ready = order_restores::<O, B>(backend, caller, account_id, ready, hooks, &mut acc).await?;
for (key, id, patch) in ready {
match backend
.restore_object::<O>(caller, account_id, &id, patch)
.await
{
Ok(restored) => {
let stored = serialize_value(&restored.stored)?;
let baseline =
serde_json::from_value::<O>(Value::Object(restored.baseline.clone()))
.ok()
.and_then(|o| serde_json::to_value(o).ok())
.and_then(|v| match v {
Value::Object(m) => Some(m),
_ => None,
})
.unwrap_or(restored.baseline);
acc.restored
.insert(id.as_ref().to_owned(), restored_diff(&baseline, &stored));
acc.mutated = true;
}
Err(e) => {
acc.not_restored.insert(key, backend_set_error_value(e));
}
}
}
Ok(acc)
}
async fn order_restores<O, B>(
backend: &B,
caller: &B::CallerCtx,
account_id: &Id,
ready: Vec<(String, Id, Option<PatchObject>)>,
hooks: &dyn RestoreHooks,
acc: &mut RestoreAccumulators,
) -> Result<Vec<(String, Id, Option<PatchObject>)>, JmapError>
where
O: HistoryObject + SetObject,
B: ObjectHistoryBackend,
{
if ready.len() < 2 {
return Ok(ready);
}
let ids: Vec<Id> = ready.iter().map(|(_, id, _)| id.clone()).collect();
let mut args = HistoryGetArguments::new();
args.include_destroyed = true;
let finals = backend
.get_object_history::<O>(
caller,
account_id,
HistoryGetParams::new(&args).ids(Some(&ids)),
)
.await
.map_err(JmapError::from)?;
let keys: BTreeSet<&Id> = ids.iter().collect();
let mut deps: BTreeMap<Id, Vec<Id>> = BTreeMap::new();
for entry in &finals.entries {
let Ok(Value::Object(map)) = serde_json::to_value(&entry.object) else {
continue;
};
let Some(id) = map.get("id").and_then(Value::as_str).map(Id::from) else {
continue;
};
let refs: Vec<Id> = hooks
.same_type_references(&map)
.into_iter()
.filter(|r| r != &id && keys.contains(r))
.collect();
deps.entry(id).or_default().extend(refs);
}
if deps.values().all(Vec::is_empty) {
return Ok(ready);
}
let mut pending = ready;
let mut done: BTreeSet<Id> = BTreeSet::new();
let mut out = Vec::with_capacity(pending.len());
loop {
let before = pending.len();
let mut rest = Vec::new();
for entry in pending {
let blocked = deps
.get(&entry.1)
.is_some_and(|d| d.iter().any(|r| !done.contains(r)));
if blocked {
rest.push(entry);
} else {
done.insert(entry.1.clone());
out.push(entry);
}
}
pending = rest;
if pending.is_empty() {
return Ok(out);
}
if pending.len() == before {
for (key, _, _) in pending {
acc.not_restored.insert(
key,
set_error_value(
SetError::new(SetErrorType::InvalidProperties)
.with_properties(vec!["id".to_owned()])
.with_description("restore cycle among same-type references"),
),
);
}
return Ok(out);
}
}
}
pub async fn history_set<O, B, F, Fut>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
using: &[String],
hooks: &dyn RestoreHooks,
plain: F,
) -> Result<HandlerOutput, JmapError>
where
O: HistoryObject + SetObject,
B: ObjectHistoryBackend,
F: FnOnce(Value) -> Fut,
Fut: std::future::Future<Output = Result<HandlerOutput, JmapError>>,
{
let Value::Object(mut map) = args else {
return plain(args).await;
};
let restore = parse_restore_argument(&mut map, using)?;
let mut acc = RestoreAccumulators::default();
let mut old_state = None;
if !restore.is_empty() {
let account_id = match map.get("accountId").and_then(Value::as_str) {
Some(s) => Id::new_validated(s)
.map_err(|e| invalid(format!("accountId is not a valid Id: {e}")))?,
None => return Err(invalid("accountId is required")),
};
if !backend
.account_exists(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?
{
return Err(JmapError::account_not_found());
}
enforce_max_objects_in_set_with_restore(
&map,
restore.len(),
backend.max_objects_in_set(caller, &account_id),
)?;
let state = backend
.get_state::<O>(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?;
match map.remove("ifInState") {
None | Some(Value::Null) => {}
Some(Value::String(s)) if s == state.as_ref() => {}
Some(Value::String(_)) => return Err(JmapError::state_mismatch()),
Some(v) => {
return Err(invalid(format!(
"ifInState: expected a string or null, got {}",
json_type_name(&v)
)));
}
}
acc = run_restore_phase::<O, B>(backend, caller, &account_id, restore, hooks).await?;
old_state = Some(state);
}
let mut early_not_created = Map::new();
let mut early_not_updated = Map::new();
for (key, out) in [
("create", &mut early_not_created),
("update", &mut early_not_updated),
] {
if let Some(Value::Object(entries)) = map.get_mut(key) {
let named: Vec<String> = entries
.iter()
.filter(|(_, v)| v.as_object().is_some_and(names_object_history))
.map(|(k, _)| k.clone())
.collect();
for k in named {
entries.remove(&k);
out.insert(k, set_error_value(object_history_set_error()));
}
}
}
let mut out = plain(Value::Object(map)).await?;
if let Value::Object(primary) = &mut out.primary {
if let Some(state) = old_state
&& primary.contains_key("oldState")
{
primary.insert("oldState".to_owned(), json!(state.as_ref()));
}
for (key, early) in [
("notCreated", early_not_created),
("notUpdated", early_not_updated),
] {
if early.is_empty() {
continue;
}
let slot = primary.entry(key.to_owned()).or_insert(Value::Null);
if !slot.is_object() {
*slot = Value::Object(Map::new());
}
if let Value::Object(m) = slot {
m.extend(early);
}
}
}
if uses_history(using) {
append_restore_results(&mut out.primary, &acc);
}
Ok(out)
}
pub fn object_history_set_error() -> SetError {
SetError::new(SetErrorType::InvalidProperties)
.with_properties(vec![OBJECT_HISTORY_PROPERTY.to_owned()])
.with_description("objectHistory is returned by Foo/get only")
}
pub fn names_object_history(object: &Map<String, Value>) -> bool {
object
.keys()
.any(|k| k == OBJECT_HISTORY_PROPERTY || k.starts_with("objectHistory/"))
}
pub fn apply_restore_patch<O: SetObject>(
final_version: &Map<String, Value>,
patch: Option<&PatchObject>,
) -> Result<Map<String, Value>, Box<SetError>> {
let mut out = final_version.clone();
let Some(patch) = patch else {
return Ok(out);
};
apply_patch_object(&mut out, patch.as_map())
.map_err(|e| Box::new(set_error(SetErrorType::InvalidPatch, e.to_string())))?;
let mut changed: Vec<String> = O::SERVER_SET
.iter()
.chain(O::IMMUTABLE)
.filter(|p| **p != "id" || patch.as_map().contains_key("id"))
.filter(|p| final_version.get(**p) != out.get(**p))
.map(|p| (*p).to_owned())
.collect();
changed.sort();
changed.dedup();
if changed.is_empty() {
Ok(out)
} else {
Err(Box::new(
SetError::new(SetErrorType::InvalidProperties)
.with_properties(changed)
.with_description("a restore may not change a server-set or immutable property"),
))
}
}
pub fn restored_diff(baseline: &Map<String, Value>, stored: &Value) -> Value {
let Value::Object(stored) = stored else {
return Value::Null;
};
let diff: Map<String, Value> = stored
.iter()
.filter(|(k, v)| *k != "id" && baseline.get(*k) != Some(*v))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
if diff.is_empty() {
Value::Null
} else {
Value::Object(diff)
}
}
pub fn append_restore_results(primary: &mut Value, acc: &RestoreAccumulators) {
let Value::Object(map) = primary else {
return;
};
let as_value = |m: &Map<String, Value>| {
if m.is_empty() {
Value::Null
} else {
Value::Object(m.clone())
}
};
map.insert("restored".to_owned(), as_value(&acc.restored));
map.insert("notRestored".to_owned(), as_value(&acc.not_restored));
}
pub fn enforce_max_objects_in_set_with_restore(
args: &Map<String, Value>,
restore_count: usize,
max: u64,
) -> Result<(), JmapError> {
let len = |k: &str| -> u64 {
let n = match args.get(k) {
Some(Value::Object(m)) => m.len(),
Some(Value::Array(a)) => a.len(),
_ => 0,
};
u64::try_from(n).unwrap_or(u64::MAX)
};
let count = len("create")
.saturating_add(len("update"))
.saturating_add(len("destroy"))
.saturating_add(u64::try_from(restore_count).unwrap_or(u64::MAX));
if count > max {
let mut err = JmapError::request_too_large();
err.description = Some("maxObjectsInSet".into());
return Err(err);
}
Ok(())
}
impl<C: Clone + Send + 'static> Dispatcher<C> {
pub fn register_history_aware(
&mut self,
method: impl Into<String>,
handler: std::sync::Arc<dyn JmapHandler<C>>,
) {
let method = method.into();
self.register(method.clone(), handler);
self.history_aware.insert(method);
}
pub fn object_history_gaps(&self, supported_types: &[&str]) -> Vec<String> {
let mut gaps: Vec<String> = supported_types
.iter()
.flat_map(|t| {
["get", "set", "query", "queryChanges"]
.into_iter()
.map(move |m| format!("{t}/{m}"))
})
.filter(|m| self.handlers.contains_key(m) && !self.history_aware.contains(m))
.collect();
gaps.sort();
gaps
}
}
pub(crate) fn check_history_arguments(
method: &str,
args: &mut Value,
using: &[String],
history_aware: bool,
) -> Result<(), JmapError> {
let with_uri = uses_history(using);
if with_uri && history_aware {
return Ok(());
}
let Value::Object(map) = args else {
return Ok(());
};
let Some((_, verb)) = method.rsplit_once('/') else {
return Ok(());
};
let keys: &[&str] = match verb {
"get" => &["includeReplaced", "includeDestroyed"],
"query" | "queryChanges" => &["onlyDestroyed", "destroyedAfter", "destroyedBefore"],
"set" => &["restore"],
_ => return Ok(()),
};
for key in keys {
let at_default = match map.get(*key) {
None | Some(Value::Null) => true,
Some(Value::Bool(false)) => !key.starts_with("destroyed") && *key != "restore",
Some(Value::Object(m)) => *key == "restore" && m.is_empty(),
Some(_) => false,
};
if !at_default {
return Err(if with_uri {
invalid(format!(
"{method} keeps no object history, so {key} is not supported \
(draft-ietf-jmap-object-history §3.1, §4, §5.1)"
))
} else {
without_uri(key)
});
}
}
for key in keys {
map.remove(*key);
}
Ok(())
}