use jmap_chat_types::{DeliveryState, Message, SenderId};
use jmap_types::{Id, Invocation, JmapError, PatchObject, State, UTCDate};
use serde_json::{json, Value};
use crate::backend::{BackendSetError, ChatBackend, SetError, SetErrorType};
use std::collections::HashSet;
use crate::helpers::{
enforce_max_objects_in_set, extract_account_id, filter_properties, finalize_set_response,
iso8601_before, not_found_json, now_utc_string, serialize_value, set_error_value,
SetAccumulators,
};
use jmap_server::{server_fail_from_backend, server_fail_value_from_backend};
pub async fn handle_message_get<B: ChatBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
) -> Result<(Value, Vec<Invocation>), JmapError> {
let (account_id, mut args) = extract_account_id(args)?;
let ids: Option<Vec<Id>> = match args.remove("ids") {
None | Some(Value::Null) => None,
Some(v) => Some(
serde_json::from_value(v)
.map_err(|_| JmapError::invalid_arguments("ids must be an Id array"))?,
),
};
let properties: Option<Vec<String>> = match args.remove("properties") {
None | Some(Value::Null) => None,
Some(v) => Some(
serde_json::from_value(v)
.map_err(|_| JmapError::invalid_arguments("properties must be a string array"))?,
),
};
let ids_slice = ids.as_deref();
let (mut list, not_found) = backend
.get_objects::<Message>(caller, &account_id, ids_slice, properties.as_deref())
.await
.map_err(|e| server_fail_from_backend(&e))?;
if !backend.retains_edit_history() {
for msg in list.iter_mut() {
msg.edit_history = None;
}
}
let state = backend
.get_state::<Message>(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?;
let list_json: Vec<Value> = if let Some(ref props) = properties {
let mut prop_set: HashSet<&str> = props.iter().map(|s| s.as_str()).collect();
prop_set.insert("id");
list.iter()
.map(|obj| {
let val = serialize_value(obj)?;
Ok(filter_properties(&val, &prop_set))
})
.collect::<Result<Vec<_>, JmapError>>()?
} else {
list.iter()
.map(serialize_value)
.collect::<Result<Vec<_>, _>>()?
};
Ok((
json!({
"accountId": account_id.as_ref(),
"state": state.as_ref(),
"list": list_json,
"notFound": not_found_json(¬_found),
}),
vec![],
))
}
pub async fn handle_message_changes<B: ChatBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
) -> Result<(Value, Vec<Invocation>), JmapError> {
jmap_server::handlers::handle_changes::<Message, B>(backend, caller, args).await
}
pub async fn handle_message_query<B: ChatBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
) -> Result<(Value, Vec<Invocation>), JmapError> {
let (account_id, mut args) = extract_account_id(args)?;
let calculate_total: bool = args
.get("calculateTotal")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let limit: Option<u64> = match args.remove("limit") {
None | Some(Value::Null) => None,
Some(v) => match v.as_u64() {
Some(n) => Some(n),
None => {
return Err(JmapError::invalid_arguments(format!(
"limit: expected a non-negative integer, got {v}"
)))
}
},
};
let position: i64 = match args.remove("position") {
None | Some(Value::Null) => 0,
Some(v) => v.as_i64().ok_or_else(|| {
JmapError::invalid_arguments(format!("position: expected an integer, got {v}"))
})?,
};
let filter: Option<serde_json::Value> = match args.remove("filter") {
None | Some(Value::Null) => None,
Some(v) => Some(v),
};
let has_chat_id = filter
.as_ref()
.and_then(|f| f.get("chatId"))
.map(|v| !v.is_null())
.unwrap_or(false);
let has_mention_true = filter
.as_ref()
.and_then(|f| f.get("hasMention"))
.and_then(|v| v.as_bool())
.unwrap_or(false);
if !has_chat_id && !has_mention_true {
return Err(JmapError::unsupported_filter());
}
let sort: Option<Vec<serde_json::Value>> = match args.remove("sort") {
None | Some(Value::Null) => None,
Some(v) => Some(
serde_json::from_value(v)
.map_err(|_| JmapError::invalid_arguments("sort must be an array"))?,
),
};
let result = backend
.query_objects::<Message>(
caller,
&account_id,
filter.as_ref(),
sort.as_deref(),
limit,
position,
)
.await
.map_err(|e| server_fail_from_backend(&e))?;
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.iter().map(|id| id.as_ref()).collect::<Vec<_>>(),
});
if calculate_total {
if let Some(t) = result.total {
resp["total"] = json!(t);
}
}
Ok((resp, vec![]))
}
pub async fn handle_message_query_changes<B: ChatBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
) -> Result<(Value, Vec<Invocation>), JmapError> {
let (account_id, args) = extract_account_id(args)?;
let since_query_state: State = match args.get("sinceQueryState").and_then(|v| v.as_str()) {
Some(s) => State::from(s),
None => return Err(JmapError::invalid_arguments("sinceQueryState is required")),
};
let max_changes: Option<u64> = match args.get("maxChanges") {
None | Some(Value::Null) => None,
Some(v) => Some(v.as_u64().filter(|&n| n > 0).ok_or_else(|| {
JmapError::invalid_arguments("maxChanges must be a positive integer")
})?),
};
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(JmapError::invalid_arguments(
"upToId must be a string Id or null",
))
}
};
let calculate_total: bool = args
.get("calculateTotal")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let result = backend
.query_changes::<Message>(
caller,
&account_id,
&since_query_state,
None,
None,
max_changes,
up_to_id.as_ref(),
false,
)
.await
.map_err(JmapError::from)?;
let removed: Vec<&str> = result.removed.iter().map(|id| id.as_ref()).collect();
let added: Vec<Value> = result
.added
.iter()
.map(|item| {
json!({
"id": item.id.as_ref(),
"index": item.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": removed,
"added": added,
});
if calculate_total {
if let Some(t) = result.total {
resp["total"] = json!(t);
}
}
Ok((resp, vec![]))
}
pub async fn handle_message_set<B: ChatBackend>(
backend: &B,
caller: &B::CallerCtx,
args: Value,
) -> Result<(Value, Vec<Invocation>), JmapError> {
let (account_id, mut args) = extract_account_id(args)?;
enforce_max_objects_in_set(&args, backend.max_objects_in_set(caller, &account_id))?;
let old_state = backend
.get_state::<Message>(caller, &account_id)
.await
.map_err(|e| server_fail_from_backend(&e))?;
if let Some(if_in_state) = args.get("ifInState").and_then(|v| v.as_str()) {
if if_in_state != old_state.as_ref() {
return Err(JmapError::state_mismatch());
}
}
let mut created = serde_json::Map::new();
let mut not_created = serde_json::Map::new();
let mut updated = serde_json::Map::new();
let mut not_updated = serde_json::Map::new();
let mut destroyed_list: Vec<Value> = Vec::new();
let mut not_destroyed = serde_json::Map::new();
let mut mutated = false;
if let Some(create_map) = args.get("create").and_then(|v| v.as_object()) {
for (create_id, obj_val) in create_map {
let Some(chat_id_str) = obj_val.get("chatId").and_then(|v| v.as_str()) else {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["chatId"] }),
);
continue;
};
let chat_id = Id::from(chat_id_str);
let Some(body) = obj_val
.get("body")
.and_then(|v| v.as_str())
.map(str::to_owned)
else {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["body"] }),
);
continue;
};
if body.len() > 100_000 {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["body"] }),
);
continue;
}
let Some(sent_at_str) = obj_val.get("sentAt").and_then(|v| v.as_str()) else {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["sentAt"] }),
);
continue;
};
let Ok(sent_at) = UTCDate::new_validated(sent_at_str) else {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["sentAt"] }),
);
continue;
};
let body_type = obj_val
.get("bodyType")
.and_then(|v| v.as_str())
.unwrap_or("text/plain")
.to_owned();
let reply_to: Option<Id> = obj_val
.get("replyTo")
.and_then(|v| v.as_str())
.map(Id::from);
let thread_root_id: Option<Id> = obj_val
.get("threadRootId")
.and_then(|v| v.as_str())
.map(Id::from);
let sender_expires_at: Option<UTCDate> =
if let Some(s) = obj_val.get("senderExpiresAt").and_then(|v| v.as_str()) {
let Ok(d) = UTCDate::new_validated(s) else {
not_created.insert(
create_id.clone(),
json!({
"type": "invalidProperties",
"properties": ["senderExpiresAt"],
}),
);
continue;
};
Some(d)
} else {
None
};
let burn_on_read: Option<bool> = obj_val.get("burnOnRead").and_then(|v| v.as_bool());
if let Some(ref expires_at) = sender_expires_at {
let now = now_utc_string();
if !iso8601_before(&now, expires_at) {
not_created.insert(
create_id.clone(),
json!({ "type": "invalidProperties", "properties": ["senderExpiresAt"] }),
);
continue;
}
}
if let Err(slow_err) = backend.slow_mode_check(caller, &account_id, &chat_id).await {
let retry_after_str: String = slow_err.retry_after.as_ref().to_owned();
let set_err = SetError::new(SetErrorType::custom("rateLimited"))
.with_description("Slow mode is active for this chat")
.with_extra("serverRetryAfter", json!(retry_after_str));
not_created.insert(create_id.clone(), set_error_value(&set_err));
continue;
}
let now_str = now_utc_string();
let received_at: UTCDate = UTCDate::from(now_str.as_ref());
let mut msg = Message::new(
Id::from("placeholder"),
Id::from(create_id.as_str()),
SenderId::Owner,
chat_id,
body,
body_type,
sent_at,
received_at,
DeliveryState::Pending,
);
msg.reply_to = reply_to;
msg.thread_root_id = thread_root_id;
if let Some(d) = sender_expires_at {
msg.sender_expires_at = Some(d);
}
msg.burn_on_read = burn_on_read;
match backend
.create_object::<Message>(caller, &account_id, create_id, msg)
.await
{
Ok((_server_id, created_obj)) => {
mutated = true;
created.insert(
create_id.clone(),
serde_json::to_value(&created_obj)
.expect("derive(Serialize) on plain data is infallible"),
);
}
Err(BackendSetError::SetError(set_err)) => {
not_created.insert(create_id.clone(), set_error_value(&set_err));
}
Err(BackendSetError::Other(e)) => {
not_created.insert(create_id.clone(), server_fail_value_from_backend(&e));
}
Err(_) => {
not_created.insert(
create_id.clone(),
json!({
"type": "serverFail",
"description": "unhandled backend error variant",
}),
);
}
}
}
}
if let Some(Value::Object(update_map)) = args.remove("update") {
for (id_str, patch_val) in update_map {
let id = Id::from(id_str.as_str());
const MESSAGE_READONLY: &[&str] = &[
"id",
"senderMsgId",
"senderId",
"chatId",
"sentAt",
"receivedAt",
"deliveryState",
];
let bad_props: Vec<&str> = MESSAGE_READONLY
.iter()
.copied()
.filter(|&field| patch_val.get(field).is_some())
.collect();
if !bad_props.is_empty() {
not_updated.insert(
id_str,
json!({ "type": "invalidProperties", "properties": bad_props }),
);
continue;
}
let mut augmented = patch_val;
if augmented.get("body").is_some() {
if let Some(obj) = augmented.as_object_mut() {
obj.insert("editedAt".to_owned(), json!(now_utc_string()));
}
}
if augmented.get("deletedForAll").and_then(|v| v.as_bool()) == Some(true) {
if let Some(obj) = augmented.as_object_mut() {
obj.insert("deletedAt".to_owned(), json!(now_utc_string()));
}
}
if let Some(obj) = augmented.as_object_mut() {
if obj.contains_key("readAt") && !obj.contains_key("readDisposition") {
obj.insert("readDisposition".to_owned(), json!("displayed"));
}
}
let reaction_pointer_keys: Vec<String> = augmented
.as_object()
.map(|obj| {
obj.keys()
.filter(|k| k.starts_with("reactions/"))
.cloned()
.collect()
})
.unwrap_or_default();
if !reaction_pointer_keys.is_empty() {
if augmented
.as_object()
.is_some_and(|obj| obj.contains_key("reactions"))
{
not_updated.insert(
id_str,
json!({
"type": "invalidPatch",
"description":
"patch must not combine top-level `reactions` with `reactions/{id}` entries",
}),
);
continue;
}
let decoded: Result<Vec<(String, String)>, String> = reaction_pointer_keys
.iter()
.map(|raw_key| {
let suffix = &raw_key["reactions/".len()..];
if suffix.is_empty() || suffix.contains('/') || suffix.contains('~') {
return Err(format!(
"reactions/{{id}} pointer key {raw_key:?} has empty or forbidden suffix; '/' and '~' are reserved by RFC 6901",
));
}
let val = augmented.get(raw_key).unwrap_or(&Value::Null);
if !val.is_null() {
let ok = val.as_object().is_some_and(|m| {
m.get("emoji")
.and_then(|v| v.as_str())
.is_some_and(|s| !s.is_empty())
});
if !ok {
return Err(format!(
"reaction value for {raw_key:?} must be null or an object with a non-empty `emoji` string",
));
}
}
Ok((raw_key.clone(), suffix.to_owned()))
})
.collect();
let decoded = match decoded {
Ok(d) => d,
Err(desc) => {
not_updated.insert(
id_str,
json!({ "type": "invalidPatch", "description": desc }),
);
continue;
}
};
let forbidden = match backend
.get_objects::<Message>(
caller,
&account_id,
Some(std::slice::from_ref(&id)),
None,
)
.await
{
Ok((found, _not_found)) => {
let mut bad: Vec<String> = Vec::new();
if let Some(msg) = found.first() {
for (_, suffix) in &decoded {
if let Some(existing) = msg.reactions.get(suffix) {
if existing.sender_id != SenderId::Owner {
bad.push(suffix.clone());
}
}
}
}
bad
}
Err(e) => {
not_updated.insert(id_str, server_fail_value_from_backend(&e));
continue;
}
};
if !forbidden.is_empty() {
not_updated.insert(
id_str,
json!({
"type": "forbidden",
"description": format!(
"cannot modify reactions authored by another sender: {}",
forbidden.join(", "),
),
}),
);
continue;
}
let now_str = now_utc_string();
if let Some(obj) = augmented.as_object_mut() {
let mut sub = serde_json::Map::new();
for (raw_key, suffix) in &decoded {
let val = obj.remove(raw_key).unwrap_or(Value::Null);
let final_val = match val {
Value::Null => Value::Null,
Value::Object(mut map) => {
map.insert("senderId".to_owned(), json!("self"));
if !map.contains_key("sentAt") {
map.insert("sentAt".to_owned(), json!(now_str));
}
Value::Object(map)
}
other => other,
};
sub.insert(suffix.clone(), final_val);
}
obj.insert("reactions".to_owned(), Value::Object(sub));
}
}
let patch_sets_read_at = augmented.get("readAt").is_some_and(|v| !v.is_null());
let pre_patch_burn_on_read = if patch_sets_read_at {
match backend
.get_objects::<Message>(
caller,
&account_id,
Some(std::slice::from_ref(&id)),
None,
)
.await
{
Ok((found, _not_found)) => {
found.first().and_then(|m| m.burn_on_read) == Some(true)
}
Err(_) => false,
}
} else {
false
};
let patch = match serde_json::from_value::<PatchObject>(augmented) {
Ok(p) => p,
Err(e) => {
not_updated.insert(
id_str,
json!({ "type": "invalidPatch", "description": e.to_string() }),
);
continue;
}
};
let update_outcome = backend
.update_object::<Message>(caller, &account_id, &id, patch)
.await;
let maybe_updated_obj = match update_outcome {
Ok(maybe_obj) => maybe_obj,
Err(BackendSetError::SetError(set_err)) => {
not_updated.insert(id_str, set_error_value(&set_err));
continue;
}
Err(BackendSetError::Other(e)) => {
not_updated.insert(id_str, server_fail_value_from_backend(&e));
continue;
}
Err(_) => {
not_updated.insert(
id_str,
json!({
"type": "serverFail",
"description": "unhandled backend error variant",
}),
);
continue;
}
};
if patch_sets_read_at && pre_patch_burn_on_read {
if let Err(burn_err) = backend.expire_message(caller, &account_id, &id).await {
not_updated.insert(id_str, server_fail_value_from_backend(&burn_err));
mutated = true;
continue;
}
}
mutated = true;
match maybe_updated_obj {
Some(obj) => {
updated.insert(
id_str,
serde_json::to_value(&obj)
.expect("derive(Serialize) on plain data is infallible"),
);
}
None => {
updated.insert(id_str, Value::Null);
}
}
}
}
if let Some(destroy_arr) = args.get("destroy").and_then(|v| v.as_array()) {
if let Some(bad) = destroy_arr.iter().find(|v| !v.is_string()) {
return Err(JmapError::invalid_arguments(format!(
"destroy: every element must be a string Id; got {bad}"
)));
}
for id_val in destroy_arr {
let Some(id_str) = id_val.as_str() else {
continue; };
let id = Id::from(id_str);
match backend
.destroy_object::<Message>(caller, &account_id, &id)
.await
{
Ok(()) => {
mutated = true;
destroyed_list.push(Value::String(id_str.to_owned()));
}
Err(BackendSetError::SetError(set_err)) => {
not_destroyed.insert(id_str.to_owned(), set_error_value(&set_err));
}
Err(BackendSetError::Other(e)) => {
not_destroyed.insert(id_str.to_owned(), server_fail_value_from_backend(&e));
}
Err(_) => {
not_destroyed.insert(
id_str.to_owned(),
json!({
"type": "serverFail",
"description": "unhandled backend error variant",
}),
);
}
}
}
}
finalize_set_response::<B, Message>(
backend,
caller,
&account_id,
old_state,
mutated,
SetAccumulators {
created,
updated,
destroyed: destroyed_list,
not_created,
not_updated,
not_destroyed,
},
)
.await
}