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

fix(graphQL): fix error when retry mark notifications read #3562

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ee/tabby-db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ CREATE TABLE notifications(
-- content of notification, in markdown format.
content TEXT NOT NULL
);
CREATE TABLE readed_notifications(
CREATE TABLE read_notifications(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
notification_id INTEGER NOT NULL,
Expand Down
86 changes: 43 additions & 43 deletions ee/tabby-db/schema/schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions ee/tabby-db/src/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ impl DbConn {

pub async fn mark_notification_read(&self, id: i64, user_id: i64) -> Result<()> {
query!(
"INSERT INTO read_notifications (notification_id, user_id) VALUES (?, ?)",
"INSERT INTO read_notifications (notification_id, user_id)
zwpaper marked this conversation as resolved.
Show resolved Hide resolved
VALUES (?, ?)
ON CONFLICT (notification_id, user_id)
DO NOTHING",
id,
user_id
user_id,
)
.execute(&self.pool)
.await?;
Expand Down Expand Up @@ -71,7 +74,7 @@ ON
notifications.id = read_notifications.notification_id
AND read_notifications.user_id = ?
WHERE
{}
({})
AND read_notifications.notification_id IS NULL;
"#,
recipient_clause
Expand Down
75 changes: 74 additions & 1 deletion ee/tabby-webserver/src/service/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ mod tests {
assert!(service
.mark_read(&user_id, Some(&notification_id))
.await
.is_err())
.is_ok())
}

#[tokio::test]
Expand Down Expand Up @@ -419,4 +419,77 @@ mod tests {
assert_eq!(notifications.len(), 1);
assert!(notifications[0].read);
}

#[tokio::test]
async fn test_admin_mark_single_then_all() {
let db = DbConn::new_in_memory().await.unwrap();
let service = create(db.clone());

let user = db
.create_user("test1".into(), None, true, None)
.await
.unwrap()
.as_id();
let notification = db
.create_notification("admin", "notification1")
.await
.unwrap()
.as_id();
db.create_notification("admin", "notification2")
.await
.unwrap();

// mark single notification
service.mark_read(&user, Some(&notification)).await.unwrap();
let notifications = service.list(&user).await.unwrap();
assert_eq!(notifications.len(), 2);
assert!(notifications[0].read);
assert!(!notifications[1].read);

// mark all notifications
service.mark_read(&user, None).await.unwrap();
let notifications = service.list(&user).await.unwrap();
assert_eq!(notifications.len(), 2);
assert!(notifications[0].read);
assert!(notifications[1].read);
}

#[tokio::test]
async fn test_admin_mark_single_twice() {
let db = DbConn::new_in_memory().await.unwrap();
let service = create(db.clone());

let user = db
.create_user("test1".into(), None, true, None)
.await
.unwrap()
.as_id();
let notification = db
.create_notification("admin", "notification1")
.await
.unwrap()
.as_id();

service.mark_read(&user, Some(&notification)).await.unwrap();
assert!(service.mark_read(&user, Some(&notification)).await.is_ok());
}

#[tokio::test]
async fn test_admin_mark_all_twice() {
let db = DbConn::new_in_memory().await.unwrap();
let service = create(db.clone());

let user = db
.create_user("test1".into(), None, true, None)
.await
.unwrap()
.as_id();
db.create_notification("admin", "notification1")
.await
.unwrap()
.as_id();

service.mark_read(&user, None).await.unwrap();
assert!(service.mark_read(&user, None).await.is_ok());
}
}
Loading