Skip to content

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

fix(notifications): key core notifications by workspace scope

Steven Enamakel425ebf6f8cfc
diff --git a/crates/openhuman-core/src/desktop/notifications/store.rs b/crates/openhuman-core/src/desktop/notifications/store.rs
index efa52d7d97..af03cd365d 100644
--- a/crates/openhuman-core/src/desktop/notifications/store.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store.rs
@@ -158,7 +158,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(config)? {
- return docs.insert_core_notification(&config.workspace_dir.to_string_lossy(), event);
+ return docs.insert_core_notification(docs.workspace(), event);
}
with_connection(config, |conn| {
let payload = serde_json::to_string(event)
@@ -190,7 +190,7 @@ pub fn list_core_notifications(
) -> Result<Vec<CoreNotificationEvent>> {
if let Some(docs) = super::store_documents::current(config)? {
return docs.list_core_notifications(
- &config.workspace_dir.to_string_lossy(),
+ docs.workspace(),
only_unread,
limit,
);
@@ -231,7 +231,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(config)? {
- return docs.mark_core_notification_read(&config.workspace_dir.to_string_lossy(), id);
+ return docs.mark_core_notification_read(docs.workspace(), id);
}
with_connection(config, |conn| {
let affected = conn
@@ -247,7 +247,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(config)? {
- return docs.unread_core_notification_count(&config.workspace_dir.to_string_lossy());
+ return docs.unread_core_notification_count(docs.workspace());
}
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 16d7bd2dff..5d529c9659 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs
@@ -48,6 +48,10 @@ pub(super) const NOTIFICATIONS: &str = "integration_notifications";
const DEDUP: &str = "notification_dedup";
pub(super) const SETTINGS: &str = "notification_settings";
pub(super) const CORE: &str = "core_notifications";
+
+/// The `workspace` key of core notifications in the per-workspace default
+/// file (see [`current`]).
+pub(super) const LOCAL_WORKSPACE: &str = "local";
const DOMAIN: &str = "notifications::store";
/// How long identical content counts as a duplicate.
@@ -71,13 +75,23 @@ fn collections() -> Vec<CollectionSpec> {
/// default, the document tables in `notifications.db` (the legacy tables
/// imported on first open). `None` keeps the legacy tables.
pub(super) fn current(config: &Config) -> Result<Option<Docs>> {
+ // An installed backend may be shared by several workspaces, so it keeps
+ // core notifications apart by workspace path. The default file lives in
+ // the workspace and moves with it, so it uses a fixed key: a renamed or
+ // restored workspace still finds its notifications.
+ let workspace = if crate::storage::installed().is_some() {
+ config.workspace_dir.to_string_lossy().into_owned()
+ } else {
+ LOCAL_WORKSPACE.to_string()
+ };
let plan = ImportPlan {
domain: DOMAIN,
tables: super::store::import::TABLES,
read: &|| super::store::import::read(config),
};
let db_path = super::store::db_path(config);
- Ok(local::repo(config, &db_path, DOMAIN, collections, &plan)?.map(Docs))
+ Ok(local::repo(config, &db_path, DOMAIN, collections, &plan)?
+ .map(|repo| Docs(repo, workspace)))
}
fn status_of(raw: Option<&str>) -> NotificationStatus {
@@ -239,12 +253,17 @@ pub(super) fn core_id(workspace: &str, event_id: &str) -> String {
/// The notification store over one scoped document handle.
#[derive(Clone)]
-pub(super) struct Docs(Repo);
+pub(super) struct Docs(Repo, String);
impl Docs {
#[cfg(test)]
pub(super) fn over(scoped: &crate::storage::ScopedStorage) -> Self {
- Self(Repo::over(scoped, DOMAIN, collections))
+ Self(Repo::over(scoped, DOMAIN, collections), String::new())
+ }
+
+ /// The `workspace` key this handle files core notifications under.
+ pub(super) fn workspace(&self) -> &str {
+ &self.1
}
/// Inserts `n`, failing if its id exists. With `skip_recent`, returns
diff --git a/crates/openhuman-core/src/desktop/notifications/store_import.rs b/crates/openhuman-core/src/desktop/notifications/store_import.rs
index 51e73dfdb1..001c2ae0b2 100644
--- a/crates/openhuman-core/src/desktop/notifications/store_import.rs
+++ b/crates/openhuman-core/src/desktop/notifications/store_import.rs
@@ -5,11 +5,12 @@
//! | --- | --- | --- |
//! | `integration_notifications` | `integration_notifications` | row id |
//! | `notification_settings` | `notification_settings` | provider |
-//! | `core_notifications` | `core_notifications` | `core_id(workspace, id)` |
+//! | `core_notifications` | `core_notifications` | `core_id("local", id)` |
//!
//! Documents come from the store's own `to_doc` and field names. The legacy
-//! `core_notifications` rows carry no workspace, so they take this
-//! workspace's directory, which is the one value a later read filters on.
+//! `core_notifications` rows carry no workspace, so they take the fixed
+//! `LOCAL_WORKSPACE` key the per-workspace default file filters on (not the
+//! directory path, which changes when the workspace is moved).
//! (The content-dedup window needs no import: it lasts a minute.) The driver
//! ([`crate::storage::local`]) writes the documents and then renames the
//! tables to `_legacy_<name>`.
@@ -18,7 +19,7 @@ use anyhow::{Context, Result};
use rusqlite::Connection;
use serde_json::json;
-use super::super::store_documents::{core_id, to_doc, CORE, NOTIFICATIONS, SETTINGS};
+use super::super::store_documents::{core_id, to_doc, CORE, LOCAL_WORKSPACE, NOTIFICATIONS, SETTINGS};
use crate::config::Config;
use crate::storage::local::{table_exists, ImportDoc};
@@ -31,8 +32,7 @@ pub(crate) const TABLES: &[&str] = &[
/// Reads every legacy row.
pub(crate) fn read(config: &Config) -> Result<Vec<ImportDoc>> {
- let workspace = config.workspace_dir.to_string_lossy().into_owned();
- super::with_connection(config, |conn| read_rows(conn, &workspace))
+ super::with_connection(config, |conn| read_rows(conn, LOCAL_WORKSPACE))
}
fn read_rows(conn: &Connection, workspace: &str) -> Result<Vec<ImportDoc>> {