summaryrefslogtreecommitdiff
path: root/src/notifications.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/notifications.rs')
-rw-r--r--src/notifications.rs337
1 files changed, 165 insertions, 172 deletions
diff --git a/src/notifications.rs b/src/notifications.rs
index 92d24d2..f725c26 100644
--- a/src/notifications.rs
+++ b/src/notifications.rs
@@ -13,6 +13,16 @@ pub struct NtfySettings {
pub message_template: Option<String>,
}
+pub struct NtfySettingsInput {
+ pub user_id: i64,
+ pub enabled: bool,
+ pub topic: String,
+ pub server_url: String,
+ pub priority: i32,
+ pub title_template: Option<String>,
+ pub message_template: Option<String>,
+}
+
#[derive(Debug, Serialize, Deserialize, FromRow, Clone, PartialEq)]
pub struct SmtpSettings {
pub id: i64,
@@ -30,6 +40,21 @@ pub struct SmtpSettings {
pub body_template: Option<String>,
}
+pub struct SmtpSettingsInput {
+ pub user_id: i64,
+ pub enabled: bool,
+ pub email: String,
+ pub smtp_server: String,
+ pub smtp_port: i32,
+ pub username: Option<String>,
+ pub password: Option<String>,
+ pub use_tls: bool,
+ pub from_email: Option<String>,
+ pub from_name: Option<String>,
+ pub subject_template: Option<String>,
+ pub body_template: Option<String>,
+}
+
pub struct NtfySettingsRepository<'a> {
pub db: &'a sqlx::SqlitePool,
}
@@ -42,26 +67,17 @@ impl<'a> NtfySettingsRepository<'a> {
.await
}
- pub async fn create(
- &self,
- user_id: i64,
- enabled: bool,
- topic: String,
- server_url: String,
- priority: i32,
- title_template: Option<String>,
- message_template: Option<String>,
- ) -> Result<NtfySettings, sqlx::Error> {
+ pub async fn create(&self, input: NtfySettingsInput) -> Result<NtfySettings, sqlx::Error> {
sqlx::query_as::<_, NtfySettings>(
"INSERT INTO user_ntfy_settings (user_id, enabled, topic, server_url, priority, title_template, message_template) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING *"
)
- .bind(user_id)
- .bind(enabled)
- .bind(topic)
- .bind(server_url)
- .bind(priority)
- .bind(title_template)
- .bind(message_template)
+ .bind(input.user_id)
+ .bind(input.enabled)
+ .bind(input.topic)
+ .bind(input.server_url)
+ .bind(input.priority)
+ .bind(input.title_template)
+ .bind(input.message_template)
.fetch_one(self.db)
.await
}
@@ -69,22 +85,17 @@ impl<'a> NtfySettingsRepository<'a> {
pub async fn update(
&self,
id: i64,
- enabled: bool,
- topic: String,
- server_url: String,
- priority: i32,
- title_template: Option<String>,
- message_template: Option<String>,
+ input: NtfySettingsInput,
) -> Result<NtfySettings, sqlx::Error> {
sqlx::query_as::<_, NtfySettings>(
"UPDATE user_ntfy_settings SET enabled = ?, topic = ?, server_url = ?, priority = ?, title_template = ?, message_template = ? WHERE id = ? RETURNING *"
)
- .bind(enabled)
- .bind(topic)
- .bind(server_url)
- .bind(priority)
- .bind(title_template)
- .bind(message_template)
+ .bind(input.enabled)
+ .bind(input.topic)
+ .bind(input.server_url)
+ .bind(input.priority)
+ .bind(input.title_template)
+ .bind(input.message_template)
.bind(id)
.fetch_one(self.db)
.await
@@ -111,36 +122,22 @@ impl<'a> SmtpSettingsRepository<'a> {
.await
}
- pub async fn create(
- &self,
- user_id: i64,
- enabled: bool,
- email: String,
- smtp_server: String,
- smtp_port: i32,
- username: Option<String>,
- password: Option<String>,
- use_tls: bool,
- from_email: Option<String>,
- from_name: Option<String>,
- subject_template: Option<String>,
- body_template: Option<String>,
- ) -> Result<SmtpSettings, sqlx::Error> {
+ pub async fn create(&self, input: SmtpSettingsInput) -> Result<SmtpSettings, sqlx::Error> {
sqlx::query_as::<_, SmtpSettings>(
"INSERT INTO user_smtp_settings (user_id, enabled, email, smtp_server, smtp_port, username, password, use_tls, from_email, from_name, subject_template, body_template) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *"
)
- .bind(user_id)
- .bind(enabled)
- .bind(email)
- .bind(smtp_server)
- .bind(smtp_port)
- .bind(username)
- .bind(password)
- .bind(use_tls)
- .bind(from_email)
- .bind(from_name)
- .bind(subject_template)
- .bind(body_template)
+ .bind(input.user_id)
+ .bind(input.enabled)
+ .bind(input.email)
+ .bind(input.smtp_server)
+ .bind(input.smtp_port)
+ .bind(input.username)
+ .bind(input.password)
+ .bind(input.use_tls)
+ .bind(input.from_email)
+ .bind(input.from_name)
+ .bind(input.subject_template)
+ .bind(input.body_template)
.fetch_one(self.db)
.await
}
@@ -148,32 +145,22 @@ impl<'a> SmtpSettingsRepository<'a> {
pub async fn update(
&self,
id: i64,
- enabled: bool,
- email: String,
- smtp_server: String,
- smtp_port: i32,
- username: Option<String>,
- password: Option<String>,
- use_tls: bool,
- from_email: Option<String>,
- from_name: Option<String>,
- subject_template: Option<String>,
- body_template: Option<String>,
+ input: SmtpSettingsInput,
) -> Result<SmtpSettings, sqlx::Error> {
sqlx::query_as::<_, SmtpSettings>(
"UPDATE user_smtp_settings SET enabled = ?, email = ?, smtp_server = ?, smtp_port = ?, username = ?, password = ?, use_tls = ?, from_email = ?, from_name = ?, subject_template = ?, body_template = ? WHERE id = ? RETURNING *"
)
- .bind(enabled)
- .bind(email)
- .bind(smtp_server)
- .bind(smtp_port)
- .bind(username)
- .bind(password)
- .bind(use_tls)
- .bind(from_email)
- .bind(from_name)
- .bind(subject_template)
- .bind(body_template)
+ .bind(input.enabled)
+ .bind(input.email)
+ .bind(input.smtp_server)
+ .bind(input.smtp_port)
+ .bind(input.username)
+ .bind(input.password)
+ .bind(input.use_tls)
+ .bind(input.from_email)
+ .bind(input.from_name)
+ .bind(input.subject_template)
+ .bind(input.body_template)
.bind(id)
.fetch_one(self.db)
.await
@@ -255,16 +242,16 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(NtfySettingsInput {
user_id,
- true,
- "topic1".to_string(),
- "https://ntfy.sh".to_string(),
- 3,
- Some("title".to_string()),
- Some("msg".to_string()),
- )
+ enabled: true,
+ topic: "topic1".to_string(),
+ server_url: "https://ntfy.sh".to_string(),
+ priority: 3,
+ title_template: Some("title".to_string()),
+ message_template: Some("msg".to_string()),
+ })
.await
.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
@@ -280,31 +267,34 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(NtfySettingsInput {
user_id,
- true,
- "topic1".to_string(),
- "https://ntfy.sh".to_string(),
- 3,
- None,
- None,
- )
+ enabled: true,
+ topic: "topic1".to_string(),
+ server_url: "https://ntfy.sh".to_string(),
+ priority: 3,
+ title_template: None,
+ message_template: None,
+ })
.await
.unwrap();
let updated = repo
.update(
- settings.id,
- false,
- "topic2".to_string(),
- "https://ntfy2.sh".to_string(),
- 4,
- Some("t2".to_string()),
- Some("m2".to_string()),
+ _settings.id,
+ NtfySettingsInput {
+ user_id,
+ enabled: false,
+ topic: "topic2".to_string(),
+ server_url: "https://ntfy2.sh".to_string(),
+ priority: 4,
+ title_template: Some("t2".to_string()),
+ message_template: Some("m2".to_string()),
+ },
)
.await
.unwrap();
- assert_eq!(updated.enabled, false);
+ assert!(!updated.enabled);
assert_eq!(updated.topic, "topic2");
assert_eq!(updated.server_url, "https://ntfy2.sh");
assert_eq!(updated.priority, 4);
@@ -317,19 +307,19 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(NtfySettingsInput {
user_id,
- true,
- "topic1".to_string(),
- "https://ntfy.sh".to_string(),
- 3,
- None,
- None,
- )
+ enabled: true,
+ topic: "topic1".to_string(),
+ server_url: "https://ntfy.sh".to_string(),
+ priority: 3,
+ title_template: None,
+ message_template: None,
+ })
.await
.unwrap();
- repo.delete(settings.id).await.unwrap();
+ repo.delete(_settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
}
@@ -339,21 +329,21 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(SmtpSettingsInput {
user_id,
- true,
- "test@example.com".to_string(),
- "smtp.example.com".to_string(),
- 587,
- Some("user".to_string()),
- Some("pass".to_string()),
- true,
- Some("from@example.com".to_string()),
- Some("Alerts".to_string()),
- Some("subj".to_string()),
- Some("body".to_string()),
- )
+ enabled: true,
+ email: "test@example.com".to_string(),
+ smtp_server: "smtp.example.com".to_string(),
+ smtp_port: 587,
+ username: Some("user".to_string()),
+ password: Some("pass".to_string()),
+ use_tls: true,
+ from_email: Some("from@example.com".to_string()),
+ from_name: Some("Alerts".to_string()),
+ subject_template: Some("subj".to_string()),
+ body_template: Some("body".to_string()),
+ })
.await
.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
@@ -362,7 +352,7 @@ mod tests {
assert_eq!(fetched.smtp_port, 587);
assert_eq!(fetched.username, Some("user".to_string()));
assert_eq!(fetched.password, Some("pass".to_string()));
- assert_eq!(fetched.use_tls, true);
+ assert!(fetched.use_tls);
assert_eq!(fetched.from_email, Some("from@example.com".to_string()));
assert_eq!(fetched.from_name, Some("Alerts".to_string()));
assert_eq!(fetched.subject_template, Some("subj".to_string()));
@@ -374,47 +364,50 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(SmtpSettingsInput {
user_id,
- true,
- "test@example.com".to_string(),
- "smtp.example.com".to_string(),
- 587,
- None,
- None,
- true,
- None,
- None,
- None,
- None,
- )
+ enabled: true,
+ email: "test@example.com".to_string(),
+ smtp_server: "smtp.example.com".to_string(),
+ smtp_port: 587,
+ username: None,
+ password: None,
+ use_tls: true,
+ from_email: None,
+ from_name: None,
+ subject_template: None,
+ body_template: None,
+ })
.await
.unwrap();
let updated = repo
.update(
- settings.id,
- false,
- "other@example.com".to_string(),
- "smtp2.example.com".to_string(),
- 465,
- Some("u2".to_string()),
- Some("p2".to_string()),
- false,
- Some("f2@example.com".to_string()),
- Some("N2".to_string()),
- Some("s2".to_string()),
- Some("b2".to_string()),
+ _settings.id,
+ SmtpSettingsInput {
+ user_id,
+ enabled: false,
+ email: "other@example.com".to_string(),
+ smtp_server: "smtp2.example.com".to_string(),
+ smtp_port: 465,
+ username: Some("u2".to_string()),
+ password: Some("p2".to_string()),
+ use_tls: false,
+ from_email: Some("f2@example.com".to_string()),
+ from_name: Some("N2".to_string()),
+ subject_template: Some("s2".to_string()),
+ body_template: Some("b2".to_string()),
+ },
)
.await
.unwrap();
- assert_eq!(updated.enabled, false);
+ assert!(!updated.enabled);
assert_eq!(updated.email, "other@example.com");
assert_eq!(updated.smtp_server, "smtp2.example.com");
assert_eq!(updated.smtp_port, 465);
assert_eq!(updated.username, Some("u2".to_string()));
assert_eq!(updated.password, Some("p2".to_string()));
- assert_eq!(updated.use_tls, false);
+ assert!(!updated.use_tls);
assert_eq!(updated.from_email, Some("f2@example.com".to_string()));
assert_eq!(updated.from_name, Some("N2".to_string()));
assert_eq!(updated.subject_template, Some("s2".to_string()));
@@ -426,24 +419,24 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo
- .create(
+ let _settings = repo
+ .create(SmtpSettingsInput {
user_id,
- true,
- "test@example.com".to_string(),
- "smtp.example.com".to_string(),
- 587,
- None,
- None,
- true,
- None,
- None,
- None,
- None,
- )
+ enabled: true,
+ email: "test@example.com".to_string(),
+ smtp_server: "smtp.example.com".to_string(),
+ smtp_port: 587,
+ username: None,
+ password: None,
+ use_tls: true,
+ from_email: None,
+ from_name: None,
+ subject_template: None,
+ body_template: None,
+ })
.await
.unwrap();
- repo.delete(settings.id).await.unwrap();
+ repo.delete(_settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
}