summaryrefslogtreecommitdiff
path: root/src/notifications.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/notifications.rs')
-rw-r--r--src/notifications.rs220
1 files changed, 188 insertions, 32 deletions
diff --git a/src/notifications.rs b/src/notifications.rs
index fb49e97..92d24d2 100644
--- a/src/notifications.rs
+++ b/src/notifications.rs
@@ -36,15 +36,22 @@ pub struct NtfySettingsRepository<'a> {
impl<'a> NtfySettingsRepository<'a> {
pub async fn get_by_user(&self, user_id: i64) -> Result<Option<NtfySettings>, sqlx::Error> {
- sqlx::query_as::<_, NtfySettings>(
- "SELECT * FROM user_ntfy_settings WHERE user_id = ?"
- )
- .bind(user_id)
- .fetch_optional(self.db)
- .await
+ sqlx::query_as::<_, NtfySettings>("SELECT * FROM user_ntfy_settings WHERE user_id = ?")
+ .bind(user_id)
+ .fetch_optional(self.db)
+ .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,
+ user_id: i64,
+ enabled: bool,
+ topic: String,
+ server_url: String,
+ priority: i32,
+ title_template: Option<String>,
+ message_template: Option<String>,
+ ) -> 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 *"
)
@@ -59,7 +66,16 @@ impl<'a> NtfySettingsRepository<'a> {
.await
}
- pub async fn update(&self, 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 update(
+ &self,
+ id: i64,
+ enabled: bool,
+ topic: String,
+ server_url: String,
+ priority: i32,
+ title_template: Option<String>,
+ message_template: Option<String>,
+ ) -> 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 *"
)
@@ -89,15 +105,27 @@ pub struct SmtpSettingsRepository<'a> {
impl<'a> SmtpSettingsRepository<'a> {
pub async fn get_by_user(&self, user_id: i64) -> Result<Option<SmtpSettings>, sqlx::Error> {
- sqlx::query_as::<_, SmtpSettings>(
- "SELECT * FROM user_smtp_settings WHERE user_id = ?"
- )
- .bind(user_id)
- .fetch_optional(self.db)
- .await
+ sqlx::query_as::<_, SmtpSettings>("SELECT * FROM user_smtp_settings WHERE user_id = ?")
+ .bind(user_id)
+ .fetch_optional(self.db)
+ .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,
+ 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> {
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 *"
)
@@ -117,7 +145,21 @@ impl<'a> SmtpSettingsRepository<'a> {
.await
}
- 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>) -> Result<SmtpSettings, sqlx::Error> {
+ 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>,
+ ) -> 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 *"
)
@@ -150,7 +192,7 @@ impl<'a> SmtpSettingsRepository<'a> {
mod tests {
use super::*;
use crate::users::{UserRepository, UserRole};
- use sqlx::{SqlitePool, Executor};
+ use sqlx::{Executor, SqlitePool};
use tokio;
async fn setup_db() -> SqlitePool {
@@ -160,8 +202,10 @@ mod tests {
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL UNIQUE,
role TEXT NOT NULL DEFAULT 'user'
- );"
- ).await.unwrap();
+ );",
+ )
+ .await
+ .unwrap();
pool.execute(
"CREATE TABLE user_ntfy_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -173,8 +217,10 @@ mod tests {
title_template TEXT,
message_template TEXT,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
- );"
- ).await.unwrap();
+ );",
+ )
+ .await
+ .unwrap();
pool.execute(
"CREATE TABLE user_smtp_settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -191,8 +237,10 @@ mod tests {
subject_template TEXT,
body_template TEXT,
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
- );"
- ).await.unwrap();
+ );",
+ )
+ .await
+ .unwrap();
pool
}
@@ -207,7 +255,18 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo.create(user_id, true, "topic1".to_string(), "https://ntfy.sh".to_string(), 3, Some("title".to_string()), Some("msg".to_string())).await.unwrap();
+ let settings = repo
+ .create(
+ user_id,
+ true,
+ "topic1".to_string(),
+ "https://ntfy.sh".to_string(),
+ 3,
+ Some("title".to_string()),
+ Some("msg".to_string()),
+ )
+ .await
+ .unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
assert_eq!(fetched.topic, "topic1");
assert_eq!(fetched.server_url, "https://ntfy.sh");
@@ -221,8 +280,30 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo.create(user_id, true, "topic1".to_string(), "https://ntfy.sh".to_string(), 3, None, 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())).await.unwrap();
+ let settings = repo
+ .create(
+ user_id,
+ true,
+ "topic1".to_string(),
+ "https://ntfy.sh".to_string(),
+ 3,
+ None,
+ 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()),
+ )
+ .await
+ .unwrap();
assert_eq!(updated.enabled, false);
assert_eq!(updated.topic, "topic2");
assert_eq!(updated.server_url, "https://ntfy2.sh");
@@ -236,7 +317,18 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = NtfySettingsRepository { db: &db };
- let settings = repo.create(user_id, true, "topic1".to_string(), "https://ntfy.sh".to_string(), 3, None, None).await.unwrap();
+ let settings = repo
+ .create(
+ user_id,
+ true,
+ "topic1".to_string(),
+ "https://ntfy.sh".to_string(),
+ 3,
+ None,
+ None,
+ )
+ .await
+ .unwrap();
repo.delete(settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
@@ -247,7 +339,23 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo.create(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())).await.unwrap();
+ let settings = repo
+ .create(
+ 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()),
+ )
+ .await
+ .unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap().unwrap();
assert_eq!(fetched.email, "test@example.com");
assert_eq!(fetched.smtp_server, "smtp.example.com");
@@ -266,8 +374,40 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo.create(user_id, true, "test@example.com".to_string(), "smtp.example.com".to_string(), 587, None, None, true, None, None, None, 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())).await.unwrap();
+ let settings = repo
+ .create(
+ user_id,
+ true,
+ "test@example.com".to_string(),
+ "smtp.example.com".to_string(),
+ 587,
+ None,
+ None,
+ true,
+ None,
+ None,
+ None,
+ 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()),
+ )
+ .await
+ .unwrap();
assert_eq!(updated.enabled, false);
assert_eq!(updated.email, "other@example.com");
assert_eq!(updated.smtp_server, "smtp2.example.com");
@@ -286,9 +426,25 @@ mod tests {
let db = setup_db().await;
let user_id = create_user(&db).await;
let repo = SmtpSettingsRepository { db: &db };
- let settings = repo.create(user_id, true, "test@example.com".to_string(), "smtp.example.com".to_string(), 587, None, None, true, None, None, None, None).await.unwrap();
+ let settings = repo
+ .create(
+ user_id,
+ true,
+ "test@example.com".to_string(),
+ "smtp.example.com".to_string(),
+ 587,
+ None,
+ None,
+ true,
+ None,
+ None,
+ None,
+ None,
+ )
+ .await
+ .unwrap();
repo.delete(settings.id).await.unwrap();
let fetched = repo.get_by_user(user_id).await.unwrap();
assert!(fetched.is_none());
}
-} \ No newline at end of file
+}