1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! JMAP Chat — ChatContact/* method implementations on SessionClient.
//!
//! Spec: JMAP Chat draft §5 (ChatContact/get, /changes, /set, /query, /queryChanges).
use jmap_types::{Id, PatchObject, State};
use super::{
ChangesResponse, ChatContactPatch, ChatContactQueryInput, GetResponse, QueryChangesResponse,
QueryResponse, SetResponse,
};
impl super::SessionClient {
/// Fetch ChatContact objects by IDs (JMAP Chat §5 ChatContact/get).
///
/// If `ids` is `None`, returns all ChatContacts for the account.
///
/// # Errors
///
/// - [`ClientError::InvalidSession`](jmap_base_client::ClientError::InvalidSession)
/// if the bound session has no primary account for
/// `urn:ietf:params:jmap:chat`.
/// - Any transport / protocol variant returned by
/// [`JmapClient::call`](jmap_base_client::JmapClient::call):
/// [`Http`](jmap_base_client::ClientError::Http),
/// [`Parse`](jmap_base_client::ClientError::Parse),
/// [`AuthFailed`](jmap_base_client::ClientError::AuthFailed),
/// [`MethodError`](jmap_base_client::ClientError::MethodError)
/// (wraps RFC 8620 §3.6.2 method-level errors such as
/// `accountNotFound`, `invalidArguments`, `serverFail`),
/// [`MethodNotFound`](jmap_base_client::ClientError::MethodNotFound),
/// [`ResponseTooLarge`](jmap_base_client::ClientError::ResponseTooLarge),
/// or
/// [`UnexpectedResponse`](jmap_base_client::ClientError::UnexpectedResponse).
pub async fn chat_contact_get(
&self,
ids: Option<&[Id]>,
properties: Option<&[&str]>,
) -> Result<GetResponse<jmap_chat_types::ChatContact>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
// Omit `ids` / `properties` when None — see the matching comment on
// `chat_get` for the rationale (consistent with set/changes/query).
let mut args = serde_json::json!({ "accountId": account_id });
if let Some(id_slice) = ids {
args["ids"] = serde_json::to_value(id_slice)
.map_err(jmap_base_client::ClientError::from_parse)?;
}
if let Some(props) = properties {
args["properties"] =
serde_json::to_value(props).map_err(jmap_base_client::ClientError::from_parse)?;
}
let req = super::build_request("ChatContact/get", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Fetch changes to ChatContact objects since `since_state` (RFC 8620 §5.2).
///
/// # Errors
///
/// - [`ClientError::InvalidArgument`](jmap_base_client::ClientError::InvalidArgument)
/// if `since_state` is the empty string (defence-in-depth —
/// `State` constructed via [`State::from`](jmap_types::State::from)
/// accepts empty strings, but an empty `sinceState` is never
/// useful and would otherwise generate a wasted round-trip).
/// - [`ClientError::InvalidSession`](jmap_base_client::ClientError::InvalidSession)
/// if the bound session has no primary account for
/// `urn:ietf:params:jmap:chat`.
/// - Any transport / protocol variant returned by
/// [`JmapClient::call`](jmap_base_client::JmapClient::call) — see
/// the matching error list on [`Self::chat_contact_get`].
pub async fn chat_contact_changes(
&self,
since_state: &State,
max_changes: Option<u64>,
) -> Result<ChangesResponse, jmap_base_client::ClientError> {
// Defence-in-depth: see `chat_changes`.
if since_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_contact_changes: since_state may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
"sinceState": since_state,
});
if let Some(mc) = max_changes {
args["maxChanges"] = mc.into();
}
let req = super::build_request("ChatContact/changes", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Update ChatContact properties (JMAP Chat §ChatContact/set).
///
/// Supports `blocked` (Boolean) and `displayName` (nullable String).
/// Create and destroy are not supported by spec; the server returns `forbidden`.
///
/// # Errors
///
/// - [`ClientError::Parse`](jmap_base_client::ClientError::Parse) if
/// serializing the typed `display_name` Clearable entry fails
/// (pathological conditions only).
/// - [`ClientError::InvalidSession`](jmap_base_client::ClientError::InvalidSession)
/// if the bound session has no primary account for
/// `urn:ietf:params:jmap:chat`.
/// - Any transport / protocol variant returned by
/// [`JmapClient::call`](jmap_base_client::JmapClient::call) — see
/// the matching error list on [`Self::chat_contact_get`]. /set
/// update errors appear in
/// [`SetResponse::not_updated`] rather than as
/// [`Err`].
pub async fn chat_contact_update(
&self,
id: &Id,
patch: &ChatContactPatch<'_>,
) -> Result<SetResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut patch_map = serde_json::Map::new();
if let Some(b) = patch.blocked {
patch_map.insert("blocked".into(), b.into());
}
if let Some(entry) = patch
.display_name
.map_entry()
.map_err(jmap_base_client::ClientError::from_parse)?
{
patch_map.insert("displayName".into(), entry);
}
// Wrap the constructed map in a PatchObject (RFC 8620 §5.3) before
// serializing. Wire bytes are unchanged because PatchObject is
// #[serde(transparent)]; the typed boundary documents the contract.
let patch_value = serde_json::Value::Object(PatchObject::from_map(patch_map).into_inner());
let args = serde_json::json!({
"accountId": account_id,
"update": { id.as_ref(): patch_value },
});
let req = super::build_request("ChatContact/set", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Query ChatContact IDs with optional filter (JMAP Chat §ChatContact/query).
///
/// Supported filter keys: `blocked`, `presence`. Supported sort properties:
/// `"lastSeenAt"`, `"login"`, `"lastActiveAt"`.
///
/// # Errors
///
/// - [`ClientError::Parse`](jmap_base_client::ClientError::Parse) if
/// serializing the typed `filter_presence` enum or
/// `sort_property` enum fails (pathological conditions only).
/// - [`ClientError::InvalidSession`](jmap_base_client::ClientError::InvalidSession)
/// if the bound session has no primary account for
/// `urn:ietf:params:jmap:chat`.
/// - Any transport / protocol variant returned by
/// [`JmapClient::call`](jmap_base_client::JmapClient::call) — see
/// the matching error list on [`Self::chat_contact_get`]. RFC
/// 8620 §5.5 defines additional /query method-level errors
/// (`anchorNotFound`, `unsupportedFilter`, `unsupportedSort`,
/// `tooManyChanges`) that surface as
/// [`MethodError`](jmap_base_client::ClientError::MethodError).
pub async fn chat_contact_query(
&self,
input: &ChatContactQueryInput,
) -> Result<QueryResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut filter = serde_json::Map::new();
if let Some(b) = input.filter_blocked {
filter.insert("blocked".into(), b.into());
}
if let Some(p) = &input.filter_presence {
filter.insert(
"presence".into(),
serde_json::to_value(p).map_err(jmap_base_client::ClientError::from_parse)?,
);
}
let filter_val = if filter.is_empty() {
serde_json::Value::Null
} else {
serde_json::Value::Object(filter)
};
let mut args = serde_json::json!({
"accountId": account_id,
"filter": filter_val,
});
if let Some(sp) = &input.sort_property {
let property =
serde_json::to_value(sp).map_err(jmap_base_client::ClientError::from_parse)?;
args["sort"] = serde_json::json!([{
"property": property,
"isAscending": input.sort_ascending.unwrap_or(false),
}]);
}
if let Some(p) = input.position {
args["position"] = p.into();
}
if let Some(l) = input.limit {
args["limit"] = l.into();
}
let req = super::build_request("ChatContact/query", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Fetch query-result changes for ChatContact since `since_query_state`
/// (RFC 8620 §5.6 / ChatContact/queryChanges).
///
/// `filter` and `sort` MUST match the `filter` / `sort` passed to the
/// original `ChatContact/query` call that returned `since_query_state`
/// — RFC 8620 §5.6 is explicit that the server uses them to compute
/// which entries entered or left the result set.
///
/// `up_to_id` is the highest-index id the client has cached;
/// `calculate_total` requests the new total result count.
///
/// # Errors
///
/// - [`ClientError::InvalidArgument`](jmap_base_client::ClientError::InvalidArgument)
/// if `since_query_state` is the empty string (defence-in-depth
/// empty-state guard; see [`Self::chat_contact_changes`]).
/// - [`ClientError::InvalidSession`](jmap_base_client::ClientError::InvalidSession)
/// if the bound session has no primary account for
/// `urn:ietf:params:jmap:chat`.
/// - Any transport / protocol variant returned by
/// [`JmapClient::call`](jmap_base_client::JmapClient::call) — see
/// the matching error list on [`Self::chat_contact_get`]. RFC
/// 8620 §5.6 also defines `cannotCalculateChanges` (returned when
/// the server cannot honour the request given the supplied
/// filter / sort); it surfaces as
/// [`MethodError`](jmap_base_client::ClientError::MethodError).
pub async fn chat_contact_query_changes(
&self,
since_query_state: &State,
max_changes: Option<u64>,
filter: Option<serde_json::Value>,
sort: Option<serde_json::Value>,
up_to_id: Option<&Id>,
calculate_total: Option<bool>,
) -> Result<QueryChangesResponse, jmap_base_client::ClientError> {
// Defence-in-depth: see `chat_changes`.
if since_query_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_contact_query_changes: since_query_state may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
"sinceQueryState": since_query_state,
});
if let Some(f) = filter {
args["filter"] = f;
}
if let Some(s) = sort {
args["sort"] = s;
}
if let Some(mc) = max_changes {
args["maxChanges"] = mc.into();
}
if let Some(uti) = up_to_id {
args["upToId"] =
serde_json::to_value(uti).map_err(jmap_base_client::ClientError::from_parse)?;
}
if let Some(ct) = calculate_total {
args["calculateTotal"] = ct.into();
}
let req = super::build_request("ChatContact/queryChanges", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
}