autoendpoint/routers/fcm/settings.rs
1use std::collections::HashMap;
2
3use url::Url;
4
5/// Settings for `FcmRouter`
6#[derive(Clone, Debug, serde::Deserialize)]
7#[serde(default)]
8#[serde(deny_unknown_fields)]
9pub struct FcmSettings {
10 /// The minimum TTL to use for FCM notifications
11 pub min_ttl: u64,
12 /// A JSON dict of `FcmCredential`s. This must be a `String` because
13 /// environment variables cannot encode a `HashMap<String, FcmCredential>`
14 /// This contains both GCM and FCM credentials.
15 /// FCM (the more modern credential set) is specified as
16 ///
17 /// ```json
18 /// {"_project_id_":{"project_id": "_project_id_", "credential": "_key_"}, ...}
19 /// ```
20 /// For FCM, `credential` keys can be either a serialized JSON string, or the
21 /// path to the JSON key file.
22 ///
23 /// GCM follows the same pattern, where
24 ///
25 /// `_project_id_` == senderID and `_key_` == credential
26 /// e.g. "bar-project" is via FCM, with a serialized JSON key,
27 /// e.g. "gorp-project" is via FCM, with a key path,
28 /// and a GCM project with SenderID of "f00" specifies the server key as credential
29 ///
30 /// ```json
31 /// {"bar-project":{"project_id": "bar-project-1234", "credential": "{\"type\": ...}"},
32 /// "gorp-project":{"project_id": "gorp-project-abcd", "credential": "keys/gorp-project.json"},
33 /// "f00": {"project_id": "f00", "credential": "abcd0123457"},
34 /// ...
35 /// }
36 /// ```
37 #[serde(rename = "credentials")]
38 pub server_credentials: String,
39 /// The max size of notification data in bytes
40 pub max_data: usize,
41 /// The base URL to use for FCM requests
42 pub base_url: Url,
43 /// The number of seconds to wait for FCM requests to complete
44 pub timeout: usize,
45}
46
47/// Credential information for each application
48#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
49pub struct FcmServerCredential {
50 pub project_id: String,
51 pub is_gcm: Option<bool>,
52 #[serde(rename = "credential")]
53 pub server_access_token: String,
54}
55
56impl Default for FcmSettings {
57 fn default() -> Self {
58 Self {
59 min_ttl: 60,
60 server_credentials: "{}".to_string(),
61 max_data: 4096,
62 base_url: Url::parse("https://fcm.googleapis.com").unwrap(),
63 timeout: 3,
64 }
65 }
66}
67
68impl FcmSettings {
69 /// Read the credentials from the provided JSON
70 pub fn credentials(&self) -> serde_json::Result<HashMap<String, FcmServerCredential>> {
71 serde_json::from_str(&self.server_credentials)
72 }
73}