Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(notifications): add NotificationService.create #3558

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ee/tabby-db/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-schema/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@

ctx.locator
.notification()
.mark_read(&user.id, notification_id)
.mark_read(&user.id, notification_id.as_ref())

Check warning on line 1004 in ee/tabby-schema/src/schema/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-schema/src/schema/mod.rs#L1004

Added line #L1004 was not covered by tests
.await?;
Ok(true)
}
Expand Down
4 changes: 3 additions & 1 deletion ee/tabby-schema/src/schema/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub struct Notification {

#[async_trait]
pub trait NotificationService: Send + Sync {
async fn create(&self, recipient: NotificationRecipient, content: &str) -> Result<ID>;

async fn list(&self, user_id: &ID) -> Result<Vec<Notification>>;

async fn mark_read(&self, user_id: &ID, id: Option<ID>) -> Result<()>;
async fn mark_read(&self, user_id: &ID, id: Option<&ID>) -> Result<()>;
}
123 changes: 79 additions & 44 deletions ee/tabby-webserver/src/service/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<ID> {
let id = self
.db
.create_notification(recipient.as_enum_str(), content)
.await?;
Ok(id.as_id())
}

async fn list(&self, user_id: &ID) -> Result<Vec<Notification>> {
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<ID>) -> 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(())
Expand All @@ -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);
Expand All @@ -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(&notification_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);
}
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -148,22 +160,21 @@ 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(&notification_id))
.await
.unwrap();
let notifications = service.list(&user_id).await.unwrap();
assert_eq!(notifications.len(), 1);
assert!(notifications[0].read);

assert!(service
.mark_read(&user_id, Some(notification_id))
.mark_read(&user_id, Some(&notification_id))
.await
.is_err())
}
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Loading