Skip to content

Commit

Permalink
Use ':' instead of '://' in contacts and credentials. These are not v…
Browse files Browse the repository at this point in the history
…alud RFC URLs, anyway
  • Loading branch information
GamePad64 committed Dec 7, 2024
1 parent efd1bd4 commit fe2fe11
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 36 deletions.
24 changes: 9 additions & 15 deletions notifico-core/src/contact.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::EngineError;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use utoipa::ToSchema;

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
Expand All @@ -8,21 +9,14 @@ pub struct Contact {
pub value: String,
}

impl Contact {
pub fn from_url(url: &str) -> Result<Self, EngineError> {
let mut iter = url.split("://");
let r#type = iter
.next()
.ok_or(EngineError::InvalidContactFormat(
"Invalid URL format".to_string(),
))?
.to_owned();
let value = iter
.next()
.ok_or(EngineError::InvalidContactFormat(
"Invalid URL format".to_string(),
))?
.to_owned();
impl FromStr for Contact {
type Err = EngineError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (r#type, value) = s.split_once(':').ok_or(EngineError::InvalidContactFormat(
"Invalid URL format".to_string(),
))?;
let (r#type, value) = (r#type.to_owned(), value.to_owned());
Ok(Self { r#type, value })
}
}
Expand Down
2 changes: 1 addition & 1 deletion notifico-core/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Credential {
pub fn transport(&self) -> &str {
match self {
Credential::Long { r#type, .. } => r#type,
Credential::Short(url) => url.split("://").next().unwrap(),
Credential::Short(url) => url.split(":").next().unwrap(),
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions notificox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,7 @@ async fn main() {
pipeline
};

let contacts: Vec<Contact> = {
let mut contacts = vec![];

for contact in to {
contacts.push(Contact::from_url(&contact).unwrap())
}

contacts
};
let contacts: Vec<Contact> = to.iter().map(|s| s.parse().unwrap()).collect();

let recipient = Recipient {
id: Uuid::nil(),
Expand Down
2 changes: 1 addition & 1 deletion transports/notifico-gotify/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl TryFrom<Credential> for GotifyCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => {
let mut url = url.splitn(2, "://");
let mut url = url.splitn(2, ":");
let _ = url.next();
let url = url.next().unwrap_or_default();
let url = Url::parse(url).map_err(|_| EngineError::InvalidCredentialFormat)?;
Expand Down
5 changes: 1 addition & 4 deletions transports/notifico-pushover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ impl TryFrom<Credential> for PushoverCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => Ok(Self {
token: url
.strip_prefix("pushover://")
.unwrap_or_default()
.to_owned(),
token: url.strip_prefix("pushover:").unwrap_or_default().to_owned(),
}),
}
}
Expand Down
2 changes: 1 addition & 1 deletion transports/notifico-slack/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl TryFrom<Credential> for SlackCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => Ok(Self {
token: url.strip_prefix("slack://").unwrap_or_default().to_owned(),
token: url.strip_prefix("slack:").unwrap_or_default().to_owned(),
}),
}
}
Expand Down
2 changes: 2 additions & 0 deletions transports/notifico-smpp/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ impl TryFrom<Credential> for SmppServerCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => {
let url = url.strip_prefix("smpp:").unwrap_or_default();
let url = String::from("smpp://") + url;
let url = Url::parse(&url).map_err(|_| EngineError::InvalidCredentialFormat)?;
Ok(Self {
host: url.host_str().unwrap_or_default().to_owned(),
Expand Down
2 changes: 2 additions & 0 deletions transports/notifico-smtp/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impl TryFrom<Credential> for SmtpServerCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => {
let url = url.strip_prefix("smtp:").unwrap_or_default();
let url = String::from("smtp://") + url;
let url = Url::parse(&url).map_err(|_| EngineError::InvalidCredentialFormat)?;
let query: BTreeMap<Cow<str>, Cow<str>> = url.query_pairs().collect();
let tls = query
Expand Down
5 changes: 1 addition & 4 deletions transports/notifico-telegram/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ impl TryFrom<Credential> for TelegramBotCredentials {
.map_err(|_| EngineError::InvalidCredentialFormat)?)
}
Credential::Short(url) => Ok(Self {
token: url
.strip_prefix("telegram://")
.unwrap_or_default()
.to_owned(),
token: url.strip_prefix("telegram:").unwrap_or_default().to_owned(),
}),
}
}
Expand Down
2 changes: 1 addition & 1 deletion transports/notifico-whatsapp/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl TryFrom<Credential> for WhatsAppCredentials {
Credential::Short(url) => {
static WABA_REGEX: OnceLock<Regex> = OnceLock::new();
let regex = WABA_REGEX
.get_or_init(|| Regex::new("^waba://([0-9]+):([0-9a-zA-Z]+)$").unwrap());
.get_or_init(|| Regex::new("^waba:([0-9]+):([0-9a-zA-Z]+)$").unwrap());

let caps = regex
.captures(&url)
Expand Down

0 comments on commit fe2fe11

Please sign in to comment.