autoendpoint/routers/fcm/
settings.rs

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