From 623e220ec30eaa140eca7dcfc74e37c3db32787e Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Fri, 13 Dec 2024 21:18:08 +0800 Subject: [PATCH] refactor(notifications): add NotificationService.create (#3558) * refactor(notifications): change recipient constants to private and update method signatures * Update notification.rs --- ee/tabby-db/src/notifications.rs | 4 +- ee/tabby-schema/src/schema/mod.rs | 2 +- ee/tabby-schema/src/schema/notification.rs | 7 +- .../src/service/notification.rs | 123 +++++++++++------- 4 files changed, 88 insertions(+), 48 deletions(-) diff --git a/ee/tabby-db/src/notifications.rs b/ee/tabby-db/src/notifications.rs index 689c92a112e8..ceb2a855867b 100644 --- a/ee/tabby-db/src/notifications.rs +++ b/ee/tabby-db/src/notifications.rs @@ -4,8 +4,8 @@ use sqlx::{prelude::*, query, query_as}; use crate::DbConn; -pub const NOTIFICATION_RECIPIENT_ALL_USER: &str = "all_user"; -pub const NOTIFICATION_RECIPIENT_ADMIN: &str = "admin"; +const NOTIFICATION_RECIPIENT_ALL_USER: &str = "all_user"; +const NOTIFICATION_RECIPIENT_ADMIN: &str = "admin"; #[derive(FromRow)] pub struct NotificationDAO { diff --git a/ee/tabby-schema/src/schema/mod.rs b/ee/tabby-schema/src/schema/mod.rs index 476dbeef6650..ffef5bdc5ee9 100644 --- a/ee/tabby-schema/src/schema/mod.rs +++ b/ee/tabby-schema/src/schema/mod.rs @@ -1001,7 +1001,7 @@ impl Mutation { ctx.locator .notification() - .mark_read(&user.id, notification_id) + .mark_read(&user.id, notification_id.as_ref()) .await?; Ok(true) } diff --git a/ee/tabby-schema/src/schema/notification.rs b/ee/tabby-schema/src/schema/notification.rs index 8c34d3e24c5d..86f6ce30174a 100644 --- a/ee/tabby-schema/src/schema/notification.rs +++ b/ee/tabby-schema/src/schema/notification.rs @@ -21,7 +21,12 @@ pub struct Notification { #[async_trait] pub trait NotificationService: Send + Sync { + /// Create notification + async fn create(&self, recipient: NotificationRecipient, content: &str) -> Result; + + /// List notifications async fn list(&self, user_id: &ID) -> Result>; - async fn mark_read(&self, user_id: &ID, id: Option) -> Result<()>; + /// Mark notification as read for user + async fn mark_read(&self, user_id: &ID, id: Option<&ID>) -> Result<()>; } diff --git a/ee/tabby-webserver/src/service/notification.rs b/ee/tabby-webserver/src/service/notification.rs index 0ac99e0a443c..2200bdb6b266 100644 --- a/ee/tabby-webserver/src/service/notification.rs +++ b/ee/tabby-webserver/src/service/notification.rs @@ -2,8 +2,8 @@ use async_trait::async_trait; use juniper::ID; use tabby_db::DbConn; use tabby_schema::{ - notification::{Notification, NotificationService}, - AsRowid, Result, + notification::{Notification, NotificationRecipient, NotificationService}, + AsID, AsRowid, DbEnum, Result, }; struct NotificationServiceImpl { @@ -16,22 +16,30 @@ pub fn create(db: DbConn) -> impl NotificationService { #[async_trait] impl NotificationService for NotificationServiceImpl { + async fn create(&self, recipient: NotificationRecipient, content: &str) -> Result { + let id = self + .db + .create_notification(recipient.as_enum_str(), content) + .await?; + Ok(id.as_id()) + } + async fn list(&self, user_id: &ID) -> Result> { let notifications = self .db - .list_notifications_within_7days(user_id.as_rowid().unwrap()) + .list_notifications_within_7days(user_id.as_rowid()?) .await?; Ok(notifications.into_iter().map(|n| n.into()).collect()) } - async fn mark_read(&self, user_id: &ID, id: Option) -> Result<()> { + async fn mark_read(&self, user_id: &ID, id: Option<&ID>) -> Result<()> { if let Some(id) = id { self.db - .mark_notification_read(id.as_rowid().unwrap(), user_id.as_rowid().unwrap()) + .mark_notification_read(id.as_rowid()?, user_id.as_rowid()?) .await?; } else { self.db - .mark_all_notifications_read_by_user(user_id.as_rowid().unwrap()) + .mark_all_notifications_read_by_user(user_id.as_rowid()?) .await?; } Ok(()) @@ -56,11 +64,10 @@ mod tests { .await .unwrap() .as_id(); - let notification_id = db - .create_notification("admin", "admin_list") + let notification_id = service + .create(NotificationRecipient::Admin, "admin_list") .await - .unwrap() - .as_id(); + .unwrap(); let notifications = service.list(&user_id).await.unwrap(); assert_eq!(notifications.len(), 1); @@ -77,18 +84,20 @@ mod tests { let user_id = db .create_user("test".into(), None, true, None) .await - .unwrap(); - let notification_id = db - .create_notification("admin", "admin_list_read") + .unwrap() + .as_id(); + let notification_id = service + .create(NotificationRecipient::Admin, "admin_list_read") .await .unwrap(); - db.mark_notification_read(notification_id, user_id) + service + .mark_read(&user_id, Some(¬ification_id)) .await .unwrap(); - let notifications = service.list(&user_id.as_id()).await.unwrap(); + let notifications = service.list(&user_id).await.unwrap(); assert_eq!(notifications.len(), 1); - assert_eq!(notifications[0].id, notification_id.as_id()); + assert_eq!(notifications[0].id, notification_id); assert_eq!(notifications[0].content, "admin_list_read"); assert!(notifications[0].read); } @@ -103,14 +112,14 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list") + service + .create(NotificationRecipient::Admin, "admin_list") .await - .unwrap() - .as_id(); - db.create_notification("all_user", "admin_list_all_user") + .unwrap(); + service + .create(NotificationRecipient::AllUser, "admin_list_all_user") .await - .unwrap() - .as_id(); + .unwrap(); let notifications = service.list(&user_id).await.unwrap(); assert_eq!(notifications.len(), 2); @@ -130,7 +139,10 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); service.mark_read(&user_id, None).await.unwrap(); let notifications = service.list(&user_id).await.unwrap(); @@ -148,14 +160,13 @@ mod tests { .await .unwrap() .as_id(); - let notification_id = db - .create_notification("admin", "admin_list") + let notification_id = service + .create(NotificationRecipient::Admin, "admin_list") .await - .unwrap() - .as_id(); + .unwrap(); service - .mark_read(&user_id, Some(notification_id.clone())) + .mark_read(&user_id, Some(¬ification_id)) .await .unwrap(); let notifications = service.list(&user_id).await.unwrap(); @@ -163,7 +174,7 @@ mod tests { assert!(notifications[0].read); assert!(service - .mark_read(&user_id, Some(notification_id)) + .mark_read(&user_id, Some(¬ification_id)) .await .is_err()) } @@ -178,10 +189,10 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list") + service + .create(NotificationRecipient::Admin, "admin_list") .await - .unwrap() - .as_id(); + .unwrap(); service.mark_read(&user_id, None).await.unwrap(); let notifications = service.list(&user_id).await.unwrap(); @@ -205,8 +216,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap(); @@ -227,8 +242,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap(); @@ -253,8 +272,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap(); @@ -282,8 +305,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap(); @@ -320,8 +347,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap(); @@ -361,8 +392,12 @@ mod tests { .await .unwrap() .as_id(); - db.create_notification("admin", "admin_list").await.unwrap(); - db.create_notification("all_user", "all_user") + service + .create(NotificationRecipient::Admin, "admin_list") + .await + .unwrap(); + service + .create(NotificationRecipient::AllUser, "all_user") .await .unwrap();