mod apns;
mod fcm;
pub(crate) mod layer;
#[cfg(feature = "server")]
mod send;
#[cfg(feature = "server")]
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
#[doc(no_inline)]
pub use ::reqwest;
#[cfg(feature = "server")]
#[cfg_attr(docsrs, doc(cfg(feature = "server")))]
pub use send::{Apns, ApnsKey, Delivery, Fcm, FcmAccount, SendError, Sender, SenderBuilder};
use crate::{Alert, EndPolicy, Error, LayoutSnapshot, LayoutStartInput, Result};
use serde::{Deserialize, Serialize};
#[must_use]
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
event: Event,
key: String,
alert: Option<Alert>,
priority: Priority,
final_content: Option<LayoutSnapshot>,
policy: Option<EndPolicy>,
sent_at: Option<u64>,
}
#[derive(Debug, Clone, PartialEq)]
enum Event {
Start {
id: String,
input: Box<LayoutStartInput>,
},
Update(Box<LayoutSnapshot>),
End,
}
impl Message {
fn new(event: Event, key: String) -> Self {
Self {
event,
key,
alert: None,
priority: Priority::Now,
final_content: None,
policy: None,
sent_at: None,
}
}
pub fn start(id: impl Into<String>, input: LayoutStartInput) -> Self {
let key = input.activity_key.clone();
Self::new(
Event::Start {
id: id.into(),
input: Box::new(input),
},
key,
)
}
pub fn update(key: impl Into<String>, snapshot: LayoutSnapshot) -> Self {
Self::new(Event::Update(Box::new(snapshot)), key.into())
}
pub fn end(key: impl Into<String>) -> Self {
Self::new(Event::End, key.into())
}
pub fn snapshot(self, snapshot: LayoutSnapshot) -> Self {
Self {
final_content: Some(snapshot),
..self
}
}
pub fn policy(self, policy: EndPolicy) -> Self {
Self {
policy: Some(policy),
..self
}
}
pub fn alert(self, alert: Alert) -> Self {
Self {
alert: Some(alert),
..self
}
}
pub fn priority(self, priority: Priority) -> Self {
Self { priority, ..self }
}
pub fn sent_at(self, at: u64) -> Self {
Self {
sent_at: Some(at),
..self
}
}
pub fn key(&self) -> &str {
&self.key
}
pub fn request(&self, target: &Target, application: &Application) -> Result<Request> {
if !matches!(self.event, Event::End)
&& (self.final_content.is_some() || self.policy.is_some())
{
return Err(Error::new(
"INVALID_INPUT",
"The last content and the policy belong to an end.",
));
}
match target {
Target::ApnsActivity { .. }
| Target::ApnsPushToStart { .. }
| Target::ApnsChannel { .. } => apns::request(self, target, application),
Target::Fcm { token } => fcm::request(self, token, application),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Priority {
#[default]
Now,
Low,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase", deny_unknown_fields)]
pub enum Target {
ApnsActivity {
token: String,
},
ApnsPushToStart {
token: String,
},
ApnsChannel {
channel: String,
},
Fcm {
token: String,
},
}
impl Target {
pub fn platform(&self) -> Platform {
match self {
Self::ApnsActivity { .. } | Self::ApnsPushToStart { .. } | Self::ApnsChannel { .. } => {
Platform::Apns
}
Self::Fcm { .. } => Platform::Fcm,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Platform {
Apns,
Fcm,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Application {
bundle_id: Option<String>,
fcm_project: Option<String>,
}
impl Application {
pub fn new() -> Self {
Self::default()
}
pub fn bundle_id(self, bundle_id: impl Into<String>) -> Self {
Self {
bundle_id: Some(bundle_id.into()),
..self
}
}
pub fn fcm_project(self, project: impl Into<String>) -> Self {
Self {
fcm_project: Some(project.into()),
..self
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
pub platform: Platform,
pub path: String,
pub headers: Vec<(&'static str, String)>,
pub body: String,
}