Page MenuHomePhabricator

D5642.id18470.diff
No OneTemporary

D5642.id18470.diff

diff --git a/services/tunnelbroker/src/main.rs b/services/tunnelbroker/src/main.rs
--- a/services/tunnelbroker/src/main.rs
+++ b/services/tunnelbroker/src/main.rs
@@ -1,5 +1,6 @@
pub mod constants;
pub mod cxx_bridge;
+pub mod notifications;
pub mod server;
use anyhow::Result;
diff --git a/services/tunnelbroker/src/notifications/apns.rs b/services/tunnelbroker/src/notifications/apns.rs
--- a/services/tunnelbroker/src/notifications/apns.rs
+++ b/services/tunnelbroker/src/notifications/apns.rs
@@ -1,56 +1,59 @@
-use crate::ffi::apns_status;
use a2::{
Client, Endpoint, Error,
ErrorReason::{BadDeviceToken, Unregistered},
NotificationBuilder, NotificationOptions, PlainNotificationBuilder,
};
-use anyhow::{bail, Result};
use std::fs::File;
+pub enum ApnsErrors {
+ BadDeviceToken,
+ Unregistered,
+ CommonError(anyhow::Error),
+}
+
pub async fn send_by_a2_client(
- certificate_path: &str,
- certificate_password: &str,
device_token: &str,
- topic: &str,
message: &str,
- sandbox: bool,
-) -> Result<apns_status> {
- let mut certificate = File::open(certificate_path)?;
- let endpoint = if sandbox {
+) -> Result<(), ApnsErrors> {
+ let mut certificate = File::open(&super::CONFIG.apns_certificate_path)
+ .expect("Error opening apns certificate file");
+ let endpoint = if super::CONFIG.is_sandbox {
Endpoint::Sandbox
} else {
Endpoint::Production
};
- let client =
- Client::certificate(&mut certificate, certificate_password, endpoint)?;
+ let client = Client::certificate(
+ &mut certificate,
+ &super::CONFIG.apns_certificate_password,
+ endpoint,
+ )
+ .expect("Error creating client on apns certificate");
let options = NotificationOptions {
- apns_topic: Some(topic),
+ apns_topic: Some(&super::CONFIG.apns_topic),
..Default::default()
};
let builder = PlainNotificationBuilder::new(message);
let mut payload = builder.build(device_token, options);
payload.aps.content_available = Some(1);
match client.send(payload).await {
- Ok(_) => Ok(apns_status::Ok),
+ Ok(_) => Ok(()),
Err(Error::ResponseError(response)) => {
if let Some(error_body) = response.error {
match error_body.reason {
- // We are returning `Ok` with the error types here to distinguish the exact
- // error type in a C++ side
- BadDeviceToken => Ok(apns_status::BadDeviceToken),
- Unregistered => Ok(apns_status::Unregistered),
- _ => bail!(
+ BadDeviceToken => Err(ApnsErrors::BadDeviceToken),
+ Unregistered => Err(ApnsErrors::Unregistered),
+ _ => Err(ApnsErrors::CommonError(anyhow::Error::msg(format!(
"Notification was not accepted by APNs, reason: {:?}",
error_body.reason
- ),
+ )))),
}
} else {
- bail!(
+ Err(ApnsErrors::CommonError(anyhow::Error::msg(format!(
"Unhandled response error from APNs, response: {:?}",
response
- )
+ ))))
}
}
- Err(error) => Err(anyhow::Error::new(error)),
+ Err(error) => Err(ApnsErrors::CommonError(anyhow::Error::new(error))),
}
}
diff --git a/services/tunnelbroker/src/notifications/fcm.rs b/services/tunnelbroker/src/notifications/fcm.rs
--- a/services/tunnelbroker/src/notifications/fcm.rs
+++ b/services/tunnelbroker/src/notifications/fcm.rs
@@ -1,16 +1,20 @@
-use crate::ffi::fcm_status;
-use anyhow::{bail, ensure, Result};
+use anyhow::Result;
use fcm::{
response::ErrorReason::{InvalidRegistration, NotRegistered},
Client, MessageBuilder, NotificationBuilder,
};
+pub enum FcmErrors {
+ InvalidRegistration,
+ NotRegistered,
+ CommonError(anyhow::Error),
+}
+
pub async fn send_by_fcm_client(
- fcm_api_key: &str,
device_registration_id: &str,
message_title: &str,
message_body: &str,
-) -> Result<fcm_status> {
+) -> Result<(), FcmErrors> {
let client = Client::new();
let mut notification_builder = NotificationBuilder::new();
notification_builder.title(message_title);
@@ -18,27 +22,38 @@
let notification = notification_builder.finalize();
let mut message_builder =
- MessageBuilder::new(fcm_api_key, device_registration_id);
+ MessageBuilder::new(&super::CONFIG.fcm_api_key, device_registration_id);
message_builder.notification(notification);
- let result = client.send(message_builder.finalize()).await?;
- match result.results {
- Some(results) => {
- ensure!(results.len() > 0, "FCM client returned zero size results");
- if let Some(result_error) = results[0].error {
- match result_error {
- // We are returning `Ok` with the error types here to distinguish the exact
- // error type in a C++ side
- InvalidRegistration => return Ok(fcm_status::InvalidRegistration),
- NotRegistered => return Ok(fcm_status::NotRegistered),
- _ => bail!(
- "Notification was not accepted by FCM, reason: {:?}",
- result_error,
- ),
+ let result = client.send(message_builder.finalize()).await;
+ match result {
+ Ok(result) => match result.results {
+ Some(results) => {
+ if results.len() == 0 {
+ return Err(FcmErrors::CommonError(anyhow::Error::msg(
+ "FCM client returned zero size results",
+ )));
+ };
+ if let Some(result_error) = results[0].error {
+ match result_error {
+ InvalidRegistration => return Err(FcmErrors::InvalidRegistration),
+ NotRegistered => return Err(FcmErrors::NotRegistered),
+ _ => {
+ return Err(FcmErrors::CommonError(anyhow::Error::msg(format!(
+ "Notification was not accepted by FCM, reason: {:?}",
+ result_error
+ ))))
+ }
+ }
}
}
- }
- None => bail!("FCM client has no results set"),
+ None => {
+ return Err(FcmErrors::CommonError(anyhow::Error::msg(
+ "FCM client has no results set",
+ )))
+ }
+ },
+ Err(err) => return Err(FcmErrors::CommonError(anyhow::Error::from(err))),
}
- Ok(fcm_status::Ok)
+ Ok(())
}

File Metadata

Mime Type
text/plain
Expires
Mon, Dec 23, 5:28 PM (19 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2695526
Default Alt Text
D5642.id18470.diff (6 KB)

Event Timeline