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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! JMAP Chat — Chat/* method implementations on SessionClient.
//!
//! Each method follows the standard five-step pattern:
//! 1. Validate arguments (defence-in-depth empty-state guards).
//! 2. Call `self.session_parts()?` → `(api_url, account_id)`.
//! 3. Build args JSON with `serde_json::json!({…})`.
//! 4. Call `build_request(method_name, args, USING_CHAT)`.
//! 5. Call `self.call_internal(api_url, &req).await?`.
//! 6. Call `jmap_base_client::extract_response(&resp, CALL_ID)?`.
use jmap_types::{Id, PatchObject, State};
use super::{
AddMemberInput, ChangesResponse, ChatCreateInput, ChatPatch, ChatQueryInput, GetResponse,
QueryChangesResponse, QueryResponse, SetResponse, TypingResponse, UpdateMemberRoleInput,
};
impl super::SessionClient {
/// Fetch Chat objects by IDs (RFC 8620 §5.1 / JMAP Chat §Chat/get).
///
/// If `ids` is `None`, the server returns all Chats for the account.
/// Pass `properties: None` to return all fields.
pub async fn chat_get(
&self,
ids: Option<&[Id]>,
properties: Option<&[&str]>,
) -> Result<GetResponse<jmap_chat_types::Chat>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
// Omit `ids` / `properties` entirely when None rather than sending
// an explicit JSON null. RFC 8620 §5.1 accepts both shapes, but the
// crate's other builders (set/changes/query) consistently use the
// conditional-add idiom; matching it here keeps the wire request
// canonical and avoids "present-but-null vs absent" interop quirks
// in proxies / audit loggers.
let mut args = serde_json::json!({ "accountId": account_id });
if let Some(id_slice) = ids {
args["ids"] = serde_json::to_value(id_slice).expect("Id slice Serialize is infallible");
}
if let Some(props) = properties {
args["properties"] = serde_json::Value::Array(
props.iter().copied().map(serde_json::Value::from).collect(),
);
}
let req = super::build_request("Chat/get", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Query Chat IDs with optional filter (RFC 8620 §5.5 / JMAP Chat §Chat/query).
///
/// Only keys that are `Some` in `input` are included in the filter object;
/// an empty filter object is sent as JSON `null`.
pub async fn chat_query(
&self,
input: &ChatQueryInput,
) -> Result<QueryResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut filter = serde_json::Map::new();
if let Some(ref k) = input.filter_kind {
let kind_str = serde_json::to_value(k).map_err(jmap_base_client::ClientError::Parse)?;
filter.insert("kind".into(), kind_str);
}
if let Some(m) = input.filter_muted {
filter.insert("muted".into(), m.into());
}
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(p) = input.position {
args["position"] = p.into();
}
if let Some(l) = input.limit {
args["limit"] = l.into();
}
let req = super::build_request("Chat/query", 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 Chat objects since `since_state` (RFC 8620 §5.2 / Chat/changes).
///
/// If `has_more_changes` is true in the response, call again with `new_state`
/// as `since_state` until the flag is false.
pub async fn chat_changes(
&self,
since_state: &State,
max_changes: Option<u64>,
) -> Result<ChangesResponse, jmap_base_client::ClientError> {
// Defence-in-depth: even with the typed-`State` parameter (a transparent
// newtype around `String`), an empty state token is still a logically
// invalid value that should be caught client-side rather than producing
// a confusing server-side `cannotCalculateChanges` error.
if since_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_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("Chat/changes", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Send a typing indicator for a Chat (JMAP Chat §Chat/typing).
///
/// Notifies other participants that the account is (or has stopped) typing.
/// The server silently drops the event if `Chat.receiveTypingIndicators` is
/// `false` for a recipient (direct/group chats); for channel chats the
/// preference has no effect. The server SHOULD rate-limit to one call per
/// account per chat per 3 seconds — excess calls MAY be silently discarded.
/// Debouncing (send once per keypress, stop event on idle) is the caller's
/// responsibility.
pub async fn chat_typing(
&self,
chat_id: &Id,
typing: bool,
) -> Result<TypingResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let args = serde_json::json!({
"accountId": account_id,
"chatId": chat_id,
"typing": typing,
});
let req = super::build_request("Chat/typing", 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 Chat since `since_query_state`
/// (RFC 8620 §5.6 / Chat/queryChanges).
///
/// Returns which Chat IDs were removed from or added to the query result set
/// since the given state. `max_changes` may be `None`.
pub async fn chat_query_changes(
&self,
since_query_state: &State,
max_changes: Option<u64>,
) -> 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_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(mc) = max_changes {
args["maxChanges"] = mc.into();
}
let req = super::build_request("Chat/queryChanges", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Create a Chat (JMAP Chat §Chat/set create).
///
/// Dispatches to the correct spec `kind` based on the `input` variant:
/// `Direct`, `Group`, or `Channel`. When `client_id` inside the variant is
/// `None`, a ULID is generated automatically.
///
/// For `Direct` chats: if one already exists with the given `contact_id`,
/// the server returns it in `SetResponse.updated` rather than `created`
/// (dedup rule per spec).
pub async fn chat_create(
&self,
input: &ChatCreateInput<'_>,
) -> Result<SetResponse, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let create_obj;
let client_id_opt = match input {
ChatCreateInput::Direct {
client_id,
contact_id,
} => {
create_obj = serde_json::json!({
"kind": "direct",
"contactId": contact_id,
});
*client_id
}
ChatCreateInput::Group {
client_id,
name,
member_ids,
description,
avatar_blob_id,
message_expiry_seconds,
} => {
if name.is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_create: name may not be empty".into(),
));
}
let mut obj = serde_json::json!({
"kind": "group",
"name": name,
"memberIds": member_ids,
});
if let Some(d) = description {
obj["description"] = (*d).into();
}
if let Some(b) = avatar_blob_id {
obj["avatarBlobId"] = b.as_ref().into();
}
if let Some(s) = message_expiry_seconds {
obj["messageExpirySeconds"] = (*s).into();
}
create_obj = obj;
*client_id
}
ChatCreateInput::Channel {
client_id,
space_id,
name,
description,
} => {
if name.is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_create: name may not be empty".into(),
));
}
let mut obj = serde_json::json!({
"kind": "channel",
"spaceId": space_id,
"name": name,
});
if let Some(d) = description {
obj["description"] = (*d).into();
}
create_obj = obj;
*client_id
}
};
let client_id = super::resolve_client_id(client_id_opt);
let args = serde_json::json!({
"accountId": account_id,
"create": { client_id: create_obj },
});
let req = super::build_request("Chat/set", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Update Chat properties (JMAP Chat §Chat/set update).
///
/// Issues an `update` operation patching only the fields present in `patch`.
/// Use `Patch::Set(v)` to set nullable fields, `Patch::Clear` to null-clear
/// them, and `Patch::Keep` (default) to leave them unchanged. Slice fields
/// default to `None` for no-change.
///
/// If all fields are `Keep`/`None`, an empty patch is sent — RFC 8620 §5.3
/// permits this; the server treats it as a no-op but still returns the chat
/// in `updated`.
pub async fn chat_update(
&self,
id: &Id,
patch: &ChatPatch<'_>,
) -> 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(m) = patch.muted {
patch_map.insert("muted".into(), m.into());
}
if let Some(entry) = patch
.mute_until
.map_entry()
.map_err(jmap_base_client::ClientError::Parse)?
{
patch_map.insert("muteUntil".into(), entry);
}
if let Some(rti) = patch.receive_typing_indicators {
patch_map.insert("receiveTypingIndicators".into(), rti.into());
}
if let Some(ids) = patch.pinned_message_ids {
patch_map.insert(
"pinnedMessageIds".into(),
serde_json::to_value(ids).expect("Id slice Serialize is infallible"),
);
}
if let Some(s) = patch.message_expiry_seconds {
patch_map.insert("messageExpirySeconds".into(), s.into());
}
if let Some(rs) = patch.receipt_sharing {
patch_map.insert("receiptSharing".into(), rs.into());
}
if let Some(n) = patch.name {
patch_map.insert("name".into(), n.into());
}
if let Some(entry) = patch
.description
.map_entry()
.map_err(jmap_base_client::ClientError::Parse)?
{
patch_map.insert("description".into(), entry);
}
if let Some(entry) = patch
.avatar_blob_id
.map_entry()
.map_err(jmap_base_client::ClientError::Parse)?
{
patch_map.insert("avatarBlobId".into(), entry);
}
if let Some(members) = patch.add_members {
if !members.is_empty() {
let arr = members
.iter()
.map(|m: &AddMemberInput<'_>| {
let mut obj = serde_json::json!({ "id": m.id });
if let Some(ref role) = m.role {
obj["role"] = serde_json::to_value(role)
.map_err(jmap_base_client::ClientError::Parse)?;
}
Ok(obj)
})
.collect::<Result<Vec<_>, jmap_base_client::ClientError>>()?;
patch_map.insert("addMembers".into(), serde_json::Value::Array(arr));
}
}
if let Some(rm) = patch.remove_members {
if !rm.is_empty() {
patch_map.insert(
"removeMembers".into(),
serde_json::to_value(rm).expect("Id slice Serialize is infallible"),
);
}
}
if let Some(umr) = patch.update_member_roles {
if !umr.is_empty() {
let arr = umr
.iter()
.map(|u: &UpdateMemberRoleInput<'_>| {
Ok(serde_json::json!({
"id": u.id,
"role": serde_json::to_value(&u.role)
.map_err(jmap_base_client::ClientError::Parse)?,
}))
})
.collect::<Result<Vec<_>, jmap_base_client::ClientError>>()?;
patch_map.insert("updateMemberRoles".into(), serde_json::Value::Array(arr));
}
}
// 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 that this
// value is a JMAP patch, not arbitrary JSON.
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("Chat/set", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
/// Destroy Chat objects (RFC 8620 §5.3 / Chat/set destroy).
///
/// Permanently removes the listed Chat IDs from the account.
/// `ids` must be non-empty; the guard fires before any network call.
pub async fn chat_destroy(
&self,
ids: &[Id],
) -> Result<SetResponse, jmap_base_client::ClientError> {
if ids.is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"chat_destroy: ids may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let args = serde_json::json!({
"accountId": account_id,
"destroy": ids,
});
let req = super::build_request("Chat/set", args, super::USING_CHAT);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
}