Skip to main content

jmap_mail_client/methods/
vacation.rs

1//! JMAP Mail — VacationResponse/get and VacationResponse/set implementations
2//! on SessionClient.
3//!
4//! VacationResponse is a singleton object per account (RFC 8621 §8). Its `id`
5//! is always `"singleton"`. `VacationResponse/get` ignores the `ids` argument
6//! and always returns the single object; `VacationResponse/set` does not
7//! support `create` or `destroy` — only `update`.
8//!
9//! Each method follows the standard five-step pattern:
10//!   1. Validate arguments (empty-string guards).
11//!   2. Call `self.session_parts()?` → `(api_url, account_id)`.
12//!   3. Build args JSON with `serde_json::json!({…})`.
13//!   4. Call `build_request(method_name, args, USING_MAIL)`.
14//!   5. Call `self.call_internal(api_url, &req).await?`.
15//!   6. Call `jmap_base_client::extract_response(&resp, CALL_ID)?`.
16
17use std::collections::HashMap;
18
19use jmap_types::{Id, PatchObject};
20
21use super::{GetResponse, SetResponse};
22
23impl super::SessionClient {
24    /// Fetch the VacationResponse singleton for the account (RFC 8621 §8).
25    ///
26    /// The server always returns a single `VacationResponse` object whose `id`
27    /// is `"singleton"`. There is no need to pass ids.
28    pub async fn vacation_response_get(
29        &self,
30    ) -> Result<GetResponse<jmap_mail_types::VacationResponse>, jmap_base_client::ClientError> {
31        let (api_url, account_id) = self.session_parts()?;
32        let args = serde_json::json!({
33            "accountId": account_id,
34            "ids": ["singleton"],
35        });
36        let req = super::build_request("VacationResponse/get", args, super::USING_MAIL);
37        let resp = self.call_internal(api_url, &req).await?;
38        jmap_base_client::extract_response(&resp, super::CALL_ID)
39    }
40
41    /// Update the VacationResponse singleton (RFC 8621 §8).
42    ///
43    /// `update` should be a JSON object of the form:
44    /// ```json
45    /// { "singleton": { "isEnabled": true, "subject": "Out of office" } }
46    /// ```
47    ///
48    /// `create` and `destroy` are not supported by `VacationResponse/set`.
49    ///
50    /// `update` is `Option<HashMap<Id, PatchObject>>` (RFC 8620 §5.3). The
51    /// usual shape is `{"singleton": <patch>}`. Wire format is unchanged
52    /// from a plain JSON object because [`PatchObject`] is
53    /// `#[serde(transparent)]`.
54    pub async fn vacation_response_set(
55        &self,
56        update: Option<HashMap<Id, PatchObject>>,
57    ) -> Result<SetResponse<jmap_mail_types::VacationResponse>, jmap_base_client::ClientError> {
58        let (api_url, account_id) = self.session_parts()?;
59        let mut args = serde_json::json!({
60            "accountId": account_id,
61        });
62        if let Some(u) = update {
63            args["update"] = serde_json::to_value(&u).map_err(|e| {
64                jmap_base_client::ClientError::InvalidArgument(format!(
65                    "vacation_response_set: serializing update map failed: {e}"
66                ))
67            })?;
68        }
69        let req = super::build_request("VacationResponse/set", args, super::USING_MAIL);
70        let resp = self.call_internal(api_url, &req).await?;
71        jmap_base_client::extract_response(&resp, super::CALL_ID)
72    }
73}
74
75// ---------------------------------------------------------------------------
76// Tests
77// ---------------------------------------------------------------------------
78
79#[cfg(test)]
80mod tests {
81    use serde_json::json;
82
83    // Deleted in JMAP-tco1.5 as Pattern E (vacuous inline tests):
84    //   - vacation_response_get_request_shape
85    //   - vacation_response_set_request_shape
86    //   - vacation_response_set_no_update_sends_account_id_only
87    // Each hand-built `args = json!({...})` and fed it to `build_request`,
88    // never invoking the `vacation_response_get` / `vacation_response_set`
89    // production builders. The third was a "None field is absent" tautology
90    // on hand-built args. Real production-path coverage for these methods
91    // is tracked as a wiremock-smoke gap under JMAP-uuoi (no
92    // `tests/vacation_*.rs` smoke file exists yet). The singleton-id-passing
93    // and no-create/no-destroy invariants of `VacationResponse/{get,set}`
94    // (RFC 8621 §8) are tracked under JMAP-uuoi for a follow-up wiremock
95    // smoke test.
96    //
97    // `build_request`, `CALL_ID`, and `USING_MAIL` themselves have their
98    // own focused tests in `methods/mod.rs`.
99
100    /// Oracle: VacationResponse deserialization from RFC 8621 §8 shape.
101    #[test]
102    fn vacation_response_get_response_deserializes() {
103        let json = json!({
104            "accountId": "acc1",
105            "state": "s1",
106            "list": [
107                {
108                    "id": "singleton",
109                    "isEnabled": false
110                }
111            ],
112            "notFound": []
113        });
114        use super::super::GetResponse;
115        let resp: GetResponse<jmap_mail_types::VacationResponse> =
116            serde_json::from_value(json).expect("must deserialize VacationResponse GetResponse");
117        assert_eq!(resp.list.len(), 1);
118        assert_eq!(resp.list[0].id.as_ref(), "singleton");
119        assert!(!resp.list[0].is_enabled);
120    }
121}