eventsource/
eventsource.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * file at the top-level directory of this distribution.
8 *
9 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
10 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
11 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
12 * option. This file may not be copied, modified, or distributed
13 * except according to those terms.
14 */
15
16#[cfg(feature = "async")]
17use futures_util::StreamExt;
18#[cfg(feature = "async")]
19use jmap_client::{client::Client, DataType};
20
21#[cfg(feature = "async")]
22async fn event_source() {
23    // Connect to the JMAP server using Basic authentication
24    let client = Client::new()
25        .credentials(("john@example.org", "secret"))
26        .connect("https://jmap.example.org")
27        .await
28        .unwrap();
29
30    // Open EventSource connection
31    let mut stream = client
32        .event_source(
33            [
34                DataType::Email,
35                DataType::EmailDelivery,
36                DataType::Mailbox,
37                DataType::EmailSubmission,
38                DataType::Identity,
39            ]
40            .into(),
41            false,
42            60.into(),
43            None,
44        )
45        .await
46        .unwrap();
47
48    // Consume events
49    while let Some(event) = stream.next().await {
50        use jmap_client::event_source::PushNotification;
51
52        match event.unwrap() {
53            PushNotification::StateChange(changes) => {
54                println!("-> Change id: {:?}", changes.id());
55                for account_id in changes.changed_accounts() {
56                    println!(" Account {} has changes:", account_id);
57                    if let Some(account_changes) = changes.changes(account_id) {
58                        for (type_state, state_id) in account_changes {
59                            println!("   Type {:?} has a new state {}.", type_state, state_id);
60                        }
61                    }
62                }
63            }
64            PushNotification::CalendarAlert(calendar_alert) => {
65                println!(
66                    "-> Calendar alert received for event {} (alert id {}).",
67                    calendar_alert.calendar_event_id, calendar_alert.alert_id
68                );
69            }
70        }
71    }
72}
73
74fn main() {
75    #[cfg(feature = "async")]
76    let _c = event_source();
77}