Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(notif): dont send on no account (#2151)

Sean Aye66b62f6ecf13
diff --git a/rust/cloud-storage/notification/src/domain/models/email_notification_digest.rs b/rust/cloud-storage/notification/src/domain/models/email_notification_digest.rs
index 39e5445797..ff96bd1534 100644
--- a/rust/cloud-storage/notification/src/domain/models/email_notification_digest.rs
+++ b/rust/cloud-storage/notification/src/domain/models/email_notification_digest.rs
@@ -124,7 +124,7 @@ impl NotificationSet {
/// IsNewEmail -->|YES| DontSend
///
/// HasAccount -->|YES| HasPush
-/// HasAccount -->|NO| BatchSend
+/// HasAccount -->|NO| DontSend
///
/// HasPush -->|NO| IsOnline
/// HasPush -->|YES| GotPush
@@ -182,15 +182,15 @@ impl<T> AllowedNotification<T> {
/// Check if the notification recipient has a Macro account.
///
/// Returns [`AccountExists`] if the user has an account (check push settings next),
- /// or [`BatchSend`] if they don't (no account means batch send immediately).
+ /// or [`DontSend`] if they don't (no account means skip email entirely).
pub async fn check_user_existence(
self,
checker: &impl UserExistenceChecker,
- ) -> Result<Either<AccountExists<T>, BatchSend<UserNotificationRow<Arc<T>>>>, Report> {
+ ) -> Result<Either<AccountExists<T>, DontSend>, Report> {
let owner = self.inner.owner_id.copied();
match checker.user_exists(owner).await {
Ok(true) => Ok(Either::Left(AccountExists { prev: self })),
- Ok(false) => Ok(Either::Right(BatchSend(self.inner))),
+ Ok(false) => Ok(Either::Right(DontSend(()))),
Err(e) => Err(e),
}
}
@@ -371,9 +371,7 @@ where
.await?
}
Either::Right(r) => {
- return Ok(StateMachineDecisionA::BatchWasQueued(
- self.inner_store_batch(r).await?,
- ));
+ return Ok(StateMachineDecisionA::DontSend(r));
}
};
let last_online = match push_notification_state {
diff --git a/rust/cloud-storage/notification/src/domain/models/email_notification_digest/test.rs b/rust/cloud-storage/notification/src/domain/models/email_notification_digest/test.rs
index 351084b1e1..d9e3b0cd80 100644
--- a/rust/cloud-storage/notification/src/domain/models/email_notification_digest/test.rs
+++ b/rust/cloud-storage/notification/src/domain/models/email_notification_digest/test.rs
@@ -128,6 +128,49 @@ impl PushNotificationChecker for MockPushNotificationChecker {
}
}
+/// Mock digest batcher that tracks calls.
+struct MockDigestBatcher {
+ call_count: Arc<std::sync::atomic::AtomicUsize>,
+ should_error: bool,
+}
+
+impl MockDigestBatcher {
+ fn new() -> (Self, Arc<std::sync::atomic::AtomicUsize>) {
+ let call_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ (
+ Self {
+ call_count: Arc::clone(&call_count),
+ should_error: false,
+ },
+ call_count,
+ )
+ }
+}
+
+impl DigestBatcher for MockDigestBatcher {
+ fn add_to_digest(
+ &self,
+ _notification: &UserNotificationRow<serde_json::Value>,
+ _send_after: Duration,
+ ) -> impl Future<Output = Result<(), Report>> + Send {
+ self.call_count
+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ let should_error = self.should_error;
+ async move {
+ if should_error {
+ rootcause::bail!("digest batcher error");
+ }
+ Ok(())
+ }
+ }
+
+ fn claim_ready_digest(
+ &self,
+ ) -> impl Future<Output = Result<ports::ClaimResult<ports::DigestBatch>, Report>> + Send {
+ async { Ok(ports::ClaimResult::Empty) }
+ }
+}
+
/// Mock last online checker with configurable behavior.
struct MockLastOnlineChecker {
last_online: Option<Duration>,
@@ -270,7 +313,7 @@ async fn test_check_user_existence_returns_account_does_not_exist() {
assert!(
result.is_right(),
- "Should return AccountDoesNotExist when user does not exist"
+ "Should return DontSend when user does not exist"
);
}
@@ -818,12 +861,162 @@ async fn test_full_flow_user_not_exists() {
.left()
.expect("Should be allowed");
- // Step 2: Check user existence - does not exist → always BatchSend
+ // Step 2: Check user existence - does not exist → DontSend (no account = no email)
let user_checker = MockUserExistenceChecker::with_user_not_exists();
let result = allowed.check_user_existence(&user_checker).await.unwrap();
assert!(
result.is_right(),
- "Full flow: user does not exist should return BatchSend"
+ "Full flow: user does not exist should return DontSend"
+ );
+}
+
+// ============================================================================
+// StateMachineDriverA::ingest Tests
+// ============================================================================
+
+fn create_driver(
+ user_checker: MockUserExistenceChecker,
+ notification_checker: MockPushNotificationChecker,
+ online_checker: MockLastOnlineChecker,
+) -> (
+ StateMachineDriverA<
+ MockUserExistenceChecker,
+ MockPushNotificationChecker,
+ MockLastOnlineChecker,
+ MockDigestBatcher,
+ >,
+ Arc<std::sync::atomic::AtomicUsize>,
+) {
+ let (batcher, call_count) = MockDigestBatcher::new();
+ (
+ StateMachineDriverA::new_with_defaults(
+ user_checker,
+ notification_checker,
+ online_checker,
+ batcher,
+ create_block_list(),
+ ),
+ call_count,
+ )
+}
+
+#[tokio::test]
+async fn test_ingest_blocked_notification_returns_dont_send() {
+ let (driver, digest_calls) = create_driver(
+ MockUserExistenceChecker::with_user_exists(),
+ MockPushNotificationChecker::with_push_disabled(),
+ MockLastOnlineChecker::with_last_online(Duration::from_secs(0)),
+ );
+ let notif = create_test_notification_row(BlockedNotification);
+
+ let result = driver.ingest(notif).await.unwrap();
+
+ assert!(
+ matches!(result, StateMachineDecisionA::DontSend(_)),
+ "Blocked notification should return DontSend"
+ );
+ assert_eq!(
+ digest_calls.load(std::sync::atomic::Ordering::Relaxed),
+ 0,
+ "Blocked notification should not queue a digest"
+ );
+}
+
+#[tokio::test]
+async fn test_ingest_user_not_exists_returns_dont_send() {
+ let (driver, digest_calls) = create_driver(
+ MockUserExistenceChecker::with_user_not_exists(),
+ MockPushNotificationChecker::with_push_disabled(),
+ MockLastOnlineChecker::with_last_online(Duration::from_secs(0)),
+ );
+ let notif = create_test_notification_row(TestNotification {
+ message: "hello".to_string(),
+ });
+
+ let result = driver.ingest(notif).await.unwrap();
+
+ assert!(
+ matches!(result, StateMachineDecisionA::DontSend(_)),
+ "User without account should return DontSend"
+ );
+ assert_eq!(
+ digest_calls.load(std::sync::atomic::Ordering::Relaxed),
+ 0,
+ "No-account path should not queue a digest"
+ );
+}
+
+#[tokio::test]
+async fn test_ingest_user_exists_push_enabled_returns_indeterminate() {
+ let (driver, digest_calls) = create_driver(
+ MockUserExistenceChecker::with_user_exists(),
+ MockPushNotificationChecker::with_push_enabled(),
+ MockLastOnlineChecker::with_last_online(Duration::from_secs(0)),
+ );
+ let notif = create_test_notification_row(TestNotification {
+ message: "hello".to_string(),
+ });
+
+ let result = driver.ingest(notif).await.unwrap();
+
+ assert!(
+ matches!(result, StateMachineDecisionA::Indeterminate(_)),
+ "User with push enabled should return Indeterminate (awaiting push delivery result)"
+ );
+ assert_eq!(
+ digest_calls.load(std::sync::atomic::Ordering::Relaxed),
+ 0,
+ "Indeterminate path should not queue a digest yet"
+ );
+}
+
+#[tokio::test]
+async fn test_ingest_user_exists_push_disabled_recently_online_returns_dont_send() {
+ let (driver, digest_calls) = create_driver(
+ MockUserExistenceChecker::with_user_exists(),
+ MockPushNotificationChecker::with_push_disabled(),
+ // Online 5 mins ago, threshold is 60 mins (default)
+ MockLastOnlineChecker::with_last_online(Duration::from_secs(5 * 60)),
+ );
+ let notif = create_test_notification_row(TestNotification {
+ message: "hello".to_string(),
+ });
+
+ let result = driver.ingest(notif).await.unwrap();
+
+ assert!(
+ matches!(result, StateMachineDecisionA::DontSend(_)),
+ "Recently online user with push disabled should return DontSend"
+ );
+ assert_eq!(
+ digest_calls.load(std::sync::atomic::Ordering::Relaxed),
+ 0,
+ "Recently online user should not queue a digest"
+ );
+}
+
+#[tokio::test]
+async fn test_ingest_user_exists_push_disabled_offline_returns_batch_was_queued() {
+ let (driver, digest_calls) = create_driver(
+ MockUserExistenceChecker::with_user_exists(),
+ MockPushNotificationChecker::with_push_disabled(),
+ // Offline for 2 hours, threshold is 60 mins (default)
+ MockLastOnlineChecker::with_last_online(Duration::from_secs(2 * 60 * 60)),
+ );
+ let notif = create_test_notification_row(TestNotification {
+ message: "hello".to_string(),
+ });
+
+ let result = driver.ingest(notif).await.unwrap();
+
+ assert!(
+ matches!(result, StateMachineDecisionA::BatchWasQueued(_)),
+ "Offline user with push disabled should return BatchWasQueued"
+ );
+ assert_eq!(
+ digest_calls.load(std::sync::atomic::Ordering::Relaxed),
+ 1,
+ "Offline user should have queued exactly one digest"
);
}