io_jmap/rfc8620/session.rs
1//! JMAP session object (RFC 8620 §2): the account map and capability set
2//! returned by the well-known session URL.
3
4use alloc::{collections::BTreeMap, string::String};
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use url::Url;
9
10/// The JMAP session object returned by the well-known URL (RFC 8620 §2).
11#[derive(Clone, Debug, Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct JmapSession {
14 /// The username associated with the credentials used to fetch the
15 /// session.
16 pub username: String,
17 /// The accounts the user has access to, keyed by account id.
18 pub accounts: BTreeMap<String, JmapAccountInfo>,
19 /// The primary account id per capability URN.
20 pub primary_accounts: BTreeMap<String, String>,
21 /// The capabilities the server supports, keyed by capability URN.
22 pub capabilities: BTreeMap<String, Value>,
23 /// The URL to POST JMAP API requests to.
24 pub api_url: Url,
25 /// The blob download URL template (RFC 6570).
26 pub download_url: String,
27 /// The blob upload URL template (RFC 6570).
28 pub upload_url: String,
29 /// The URL of the event source push channel.
30 pub event_source_url: String,
31 /// The opaque server state; changes when the session object changes.
32 pub state: String,
33}
34
35impl JmapSession {
36 /// Returns the primary account ID for the given capability URN, or an empty
37 /// string if none is advertised.
38 pub fn primary_account_id_for(&self, capability: &str) -> String {
39 self.primary_accounts
40 .get(capability)
41 .cloned()
42 .unwrap_or_default()
43 }
44}
45
46/// Information about a single JMAP account within a session.
47#[derive(Clone, Debug, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct JmapAccountInfo {
50 /// The human-readable account name.
51 pub name: String,
52 /// Whether the account belongs to the authenticated user.
53 pub is_personal: bool,
54 /// Whether the account is read-only.
55 pub is_read_only: bool,
56 /// Account-level capability objects, keyed by capability URN.
57 pub account_capabilities: BTreeMap<String, Value>,
58}