use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Message {
pub target: Target,
pub notification: Option<Notification>,
pub data: HashMap<String, String>,
pub android: Option<AndroidConfig>,
pub apns: Option<ApnsConfig>,
pub webpush: Option<WebpushConfig>,
}
impl Message {
pub fn to_token(token: impl Into<String>) -> Self {
Self::new(Target::Token(token.into()))
}
pub fn to_topic(topic: impl Into<String>) -> Self {
Self::new(Target::Topic(topic.into()))
}
pub fn to_condition(condition: impl Into<String>) -> Self {
Self::new(Target::Condition(condition.into()))
}
fn new(target: Target) -> Self {
Self {
target,
notification: None,
data: HashMap::new(),
android: None,
apns: None,
webpush: None,
}
}
pub fn with_notification(mut self, notification: Notification) -> Self {
self.notification = Some(notification);
self
}
pub fn with_data(mut self, data: HashMap<String, String>) -> Self {
self.data = data;
self
}
pub fn with_android(mut self, android: AndroidConfig) -> Self {
self.android = Some(android);
self
}
pub fn with_apns(mut self, apns: ApnsConfig) -> Self {
self.apns = Some(apns);
self
}
pub fn with_webpush(mut self, webpush: WebpushConfig) -> Self {
self.webpush = Some(webpush);
self
}
}
#[derive(Debug, Clone)]
pub enum Target {
Token(String),
Topic(String),
Condition(String),
}
#[derive(Debug, Default, Clone)]
pub struct Notification {
pub title: Option<String>,
pub body: Option<String>,
pub image: Option<String>,
}
#[derive(Debug, Default, Clone)]
pub struct AndroidConfig {
pub ttl_seconds: Option<u64>,
pub collapse_key: Option<String>,
pub priority: Option<String>,
}
#[derive(Debug, Default, Clone)]
pub struct ApnsConfig {
pub headers: HashMap<String, String>,
pub payload: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(Debug, Default, Clone)]
pub struct WebpushConfig {
pub headers: HashMap<String, String>,
pub data: HashMap<String, String>,
pub notification: Option<WebpushNotification>,
}
#[derive(Debug, Default, Clone)]
pub struct WebpushNotification {
pub title: Option<String>,
pub body: Option<String>,
pub icon: Option<String>,
pub extra: serde_json::Map<String, serde_json::Value>,
}
#[derive(Debug, Clone)]
pub enum SendResult {
Success {
message_id: String,
},
Failure {
error: SendError,
},
}
#[derive(Debug, Clone)]
pub struct SendError {
pub status: u16,
pub message: String,
pub error_code: Option<String>,
}
#[derive(Debug, Clone)]
pub struct BatchResponse {
pub responses: Vec<SendResult>,
pub success_count: usize,
pub failure_count: usize,
}
impl BatchResponse {
pub(crate) fn from_results(responses: Vec<SendResult>) -> Self {
let success_count = responses
.iter()
.filter(|r| matches!(r, SendResult::Success { .. }))
.count();
let failure_count = responses.len() - success_count;
Self {
responses,
success_count,
failure_count,
}
}
}
#[derive(Debug, Clone)]
pub struct TopicManagementResponse {
pub success_count: usize,
pub failure_count: usize,
pub errors: Vec<TopicManagementError>,
}
#[derive(Debug, Clone)]
pub struct TopicManagementError {
pub index: usize,
pub reason: String,
}
impl TopicManagementResponse {
pub(crate) fn from_errors(total: usize, errors: Vec<TopicManagementError>) -> Self {
Self {
success_count: total - errors.len(),
failure_count: errors.len(),
errors,
}
}
}