Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(notifications): split document storage out of notification store

Steven Enamakel0fce95cdae70
diff --git a/crates/openhuman-core/src/desktop/notifications/store.rs b/crates/openhuman-core/src/desktop/notifications/store.rs
index b2cb8acb82..ab9b28bbab 100644
--- a/crates/openhuman-core/src/desktop/notifications/store.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store.rs
@@ -151,7 +151,7 @@ pub fn insert(config: &Config, n: &IntegrationNotification) -> Result<()> {
/// `false` when an event with the same id already existed.
pub fn insert_core_notification(config: &Config, event: &CoreNotificationEvent) -> Result<bool> {
if let Some(docs) = super::store_documents::current()? {
- return docs.insert_core_notification(event);
+ return docs.insert_core_notification(&config.workspace_dir.to_string_lossy(), event);
}
with_connection(config, |conn| {
let payload = serde_json::to_string(event)
@@ -182,7 +182,7 @@ pub fn list_core_notifications(
limit: usize,
) -> Result<Vec<CoreNotificationEvent>> {
if let Some(docs) = super::store_documents::current()? {
- return docs.list_core_notifications(only_unread, limit);
+ return docs.list_core_notifications(&config.workspace_dir.to_string_lossy(), only_unread, limit);
}
with_connection(config, |conn| {
let sql = if only_unread {
@@ -220,7 +220,7 @@ pub fn list_core_notifications(
/// next sync-down. Returns `true` when a row was updated.
pub fn mark_core_notification_read(config: &Config, id: &str) -> Result<bool> {
if let Some(docs) = super::store_documents::current()? {
- return docs.mark_core_notification_read(id);
+ return docs.mark_core_notification_read(&config.workspace_dir.to_string_lossy(), id);
}
with_connection(config, |conn| {
let affected = conn
@@ -236,7 +236,7 @@ pub fn mark_core_notification_read(config: &Config, id: &str) -> Result<bool> {
/// Count unread persisted core notifications.
pub fn unread_core_notification_count(config: &Config) -> Result<i64> {
if let Some(docs) = super::store_documents::current()? {
- return docs.unread_core_notification_count();
+ return docs.unread_core_notification_count(&config.workspace_dir.to_string_lossy());
}
with_connection(config, |conn| {
let count: i64 = conn
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents.rs b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
index 7fec3557c5..f03983a6eb 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
@@ -58,7 +58,10 @@ fn collections() -> Vec<CollectionSpec> {
.index(IndexSpec::new("by_status", ["status"])),
CollectionSpec::new(DEDUP),
CollectionSpec::new(SETTINGS),
- CollectionSpec::new(CORE).index(IndexSpec::new("by_read", ["read", "timestamp_ms"])),
+ CollectionSpec::new(CORE).index(IndexSpec::new(
+ "by_read",
+ ["workspace", "read", "timestamp_ms"],
+ )),
]
}
@@ -217,6 +220,13 @@ async fn release_content(
}
}
+/// The core-notification document id: the workspace is part of the key, since
+/// each workspace's events are persisted separately in the SQL store (one
+/// database per workspace) and event ids repeat across them.
+fn core_id(workspace: &str, event_id: &str) -> String {
+ format!("{}:{workspace}/{event_id}", workspace.len())
+}
+
/// The notification store over one scoped document handle.
#[derive(Clone)]
pub(super) struct Docs(Repo);
@@ -421,9 +431,14 @@ impl Docs {
})
}
- pub(super) fn insert_core_notification(&self, event: &CoreNotificationEvent) -> Result<bool> {
- let id = event.id.clone();
+ pub(super) fn insert_core_notification(
+ &self,
+ workspace: &str,
+ event: &CoreNotificationEvent,
+ ) -> Result<bool> {
+ let id = core_id(workspace, &event.id);
let doc = json!({
+ "workspace": workspace,
"payload": serde_json::to_string(event)
.context("[notifications::store] serialize core notification failed")?,
"timestamp_ms": event.timestamp_ms,
@@ -442,16 +457,18 @@ impl Docs {
pub(super) fn list_core_notifications(
&self,
+ workspace: &str,
only_unread: bool,
limit: usize,
) -> Result<Vec<CoreNotificationEvent>> {
if limit == 0 {
return Ok(Vec::new());
}
+ let in_workspace = Filter::eq("workspace", workspace);
let filter = if only_unread {
- Filter::eq("read", false)
+ in_workspace.and(Filter::eq("read", false))
} else {
- Filter::All
+ in_workspace
};
let query = Query::filter(filter)
.sort(Sort::desc("timestamp_ms"))
@@ -477,8 +494,8 @@ impl Docs {
})
}
- pub(super) fn mark_core_notification_read(&self, id: &str) -> Result<bool> {
- let id = id.to_string();
+ pub(super) fn mark_core_notification_read(&self, workspace: &str, id: &str) -> Result<bool> {
+ let id = core_id(workspace, id);
self.0.run(|docs| async move {
let marked = compare_and_swap(&docs, CORE, &id, |doc| {
let mut next = doc.clone();
@@ -490,9 +507,10 @@ impl Docs {
})
}
- pub(super) fn unread_core_notification_count(&self) -> Result<i64> {
+ pub(super) fn unread_core_notification_count(&self, workspace: &str) -> Result<i64> {
+ let filter = Filter::eq("workspace", workspace).and(Filter::eq("read", false));
self.0.run(|docs| async move {
- let count = docs.count(CORE, &Filter::eq("read", false)).await?;
+ let count = docs.count(CORE, &filter).await?;
Ok(i64::try_from(count).unwrap_or(i64::MAX))
})
}
diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs b/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs
index 3b7d5fa527..963e3a28d6 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents_tests.rs
@@ -168,35 +168,35 @@ fn settings_default_then_upsert() {
#[test]
fn core_notifications_persist_once_and_mark_read() {
let store = docs();
- assert!(store.insert_core_notification(&event("a", 1)).unwrap());
- assert!(!store.insert_core_notification(&event("a", 1)).unwrap());
- assert!(store.insert_core_notification(&event("b", 2)).unwrap());
- let all = store.list_core_notifications(false, 10).unwrap();
+ assert!(store.insert_core_notification(WS, &event("a", 1)).unwrap());
+ assert!(!store.insert_core_notification(WS, &event("a", 1)).unwrap());
+ assert!(store.insert_core_notification(WS, &event("b", 2)).unwrap());
+ let all = store.list_core_notifications(WS, false, 10).unwrap();
assert_eq!(
all.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(),
["b", "a"]
);
assert_eq!(all[0], event("b", 2));
- assert_eq!(store.unread_core_notification_count().unwrap(), 2);
- assert!(store.mark_core_notification_read("b").unwrap());
+ assert_eq!(store.unread_core_notification_count(WS, WS).unwrap(), 2);
+ assert!(store.mark_core_notification_read(WS, "b").unwrap());
assert!(
- store.mark_core_notification_read("b").unwrap(),
+ store.mark_core_notification_read(WS, "b").unwrap(),
"still exists"
);
- assert!(!store.mark_core_notification_read("missing").unwrap());
- let unread = store.list_core_notifications(true, 10).unwrap();
+ assert!(!store.mark_core_notification_read(WS, "missing").unwrap());
+ let unread = store.list_core_notifications(WS, true, 10).unwrap();
assert_eq!(
unread.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(),
["a"]
);
- assert_eq!(store.unread_core_notification_count().unwrap(), 1);
- assert!(store.list_core_notifications(false, 0).unwrap().is_empty());
+ assert_eq!(store.unread_core_notification_count(WS, WS).unwrap(), 1);
+ assert!(store.list_core_notifications(WS, false, 0).unwrap().is_empty());
}
#[test]
fn a_corrupt_core_payload_is_skipped() {
let store = docs();
- store.insert_core_notification(&event("good", 1)).unwrap();
+ store.insert_core_notification(WS, &event("good", 1)).unwrap();
store
.0
.run(|docs| async move {
@@ -210,7 +210,7 @@ fn a_corrupt_core_payload_is_skipped() {
.map(|_| ())
})
.unwrap();
- let listed = store.list_core_notifications(false, 10).unwrap();
+ let listed = store.list_core_notifications(WS, false, 10).unwrap();
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].id, "good");
}