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: free inbox limit = 2 (#4383)

Peter Chinman3d7835dc28c3
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 498c9f577f..ae1e7a69fe 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -903,6 +903,7 @@ dependencies = [
"document_storage_service_client",
"doppleganger",
"doppler-config",
+ "email_db_client",
"email_validator",
"entity_access",
"foreign_entity",
diff --git a/rust/cloud-storage/authentication_service/Cargo.toml b/rust/cloud-storage/authentication_service/Cargo.toml
index ff1887f0c1..ef859f4018 100644
--- a/rust/cloud-storage/authentication_service/Cargo.toml
+++ b/rust/cloud-storage/authentication_service/Cargo.toml
@@ -45,6 +45,7 @@ comms_db_client = { path = "../comms_db_client" }
cookie = { workspace = true }
document_storage_service_client = { path = "../document_storage_service_client" }
doppleganger = { workspace = true }
+email_db_client = { path = "../email_db_client" }
email_validator = { path = "../email_validator" }
entity_access = { path = "../entity_access" }
foreign_entity = { path = "../foreign_entity" }
diff --git a/rust/cloud-storage/authentication_service/src/api/link/gmail.rs b/rust/cloud-storage/authentication_service/src/api/link/gmail.rs
index 817f5da017..5010e3e9a3 100644
--- a/rust/cloud-storage/authentication_service/src/api/link/gmail.rs
+++ b/rust/cloud-storage/authentication_service/src/api/link/gmail.rs
@@ -7,6 +7,7 @@ use axum::{
};
use macro_middleware::tracking::ClientIp;
use macro_middleware::user_permissions::attach_user_permissions::PermissionsExtractor;
+use macro_user_id::user_id::MacroUserIdStr;
use model::response::ErrorResponse;
use model_user::axum_extractor::MacroUserExtractor;
use roles_and_permissions::domain::model::PermissionId;
@@ -23,6 +24,7 @@ mod test;
const GOOGLE_AUTHORIZATION_URL: &str = "https://accounts.google.com/o/oauth2/v2/auth";
const GMAIL_IDENTITY_PROVIDER_NAME: &str = "google_gmail";
const GMAIL_SCOPES: &str = "openid profile email https://www.googleapis.com/auth/gmail.modify https://www.googleapis.com/auth/contacts.readonly https://www.googleapis.com/auth/contacts.other.readonly https://www.googleapis.com/auth/gmail.settings.basic";
+const FREE_INBOX_LIMIT: i64 = 2;
#[derive(serde::Deserialize, serde::Serialize, Debug, utoipa::ToSchema)]
pub struct InitGmailLinkResponse {
@@ -106,7 +108,7 @@ pub async fn init_gmail_link_handler(
enforce_inbox_paywall(
permissions.contains(&PermissionId::ReadProfessionalFeatures.to_string()),
- || macro_db_client::email::check_user_email_link(&ctx.db, &user_context.macro_user_id),
+ || count_accessible_email_inboxes(&ctx.db, &user_context.macro_user_id),
)
.await?;
@@ -161,20 +163,30 @@ pub async fn init_gmail_link_handler(
}))
}
-/// Enforces the inbox paywall. The first inbox is free, so the existing-inbox
-/// check only runs for users without professional features, and only such a user
-/// who already has an inbox needs a professional subscription.
+#[tracing::instrument(skip(db, macro_user_id), err)]
+async fn count_accessible_email_inboxes(
+ db: &sqlx::Pool<sqlx::Postgres>,
+ macro_user_id: &MacroUserIdStr<'static>,
+) -> anyhow::Result<i64> {
+ let inboxes =
+ email_db_client::links::get::fetch_inboxes_for_macro_id(db, macro_user_id.as_ref()).await?;
+
+ Ok(inboxes.len() as i64)
+}
+
+/// Enforces the inbox paywall. Free users can connect inboxes until they reach
+/// `FREE_INBOX_LIMIT`; professional users skip the count entirely.
async fn enforce_inbox_paywall<F, Fut>(
has_professional_features: bool,
- check_existing_inbox: F,
+ count_connected_inboxes: F,
) -> Result<(), InitGmailLinkError>
where
F: FnOnce() -> Fut,
- Fut: std::future::Future<Output = anyhow::Result<bool>>,
+ Fut: std::future::Future<Output = anyhow::Result<i64>>,
{
if !has_professional_features {
- let has_own_inbox = check_existing_inbox().await?;
- if has_own_inbox {
+ let connected_inbox_count = count_connected_inboxes().await?;
+ if connected_inbox_count >= FREE_INBOX_LIMIT {
return Err(InitGmailLinkError::PaymentRequired);
}
}
diff --git a/rust/cloud-storage/authentication_service/src/api/link/gmail/test.rs b/rust/cloud-storage/authentication_service/src/api/link/gmail/test.rs
index f5db4aefe0..4567a99889 100644
--- a/rust/cloud-storage/authentication_service/src/api/link/gmail/test.rs
+++ b/rust/cloud-storage/authentication_service/src/api/link/gmail/test.rs
@@ -1,25 +1,25 @@
use super::*;
use std::cell::Cell;
-/// Stub for the existing-inbox check. Reports a fixed answer and records whether
+/// Stub for the connected-inbox count. Reports a fixed count and records whether
/// the paywall actually invoked it, so tests can assert the db is only queried
/// when it should be.
-struct InboxCheckSpy {
- has_inbox: bool,
+struct InboxCountSpy {
+ connected_inbox_count: i64,
called: Cell<bool>,
}
-impl InboxCheckSpy {
- fn returning(has_inbox: bool) -> Self {
+impl InboxCountSpy {
+ fn returning(connected_inbox_count: i64) -> Self {
Self {
- has_inbox,
+ connected_inbox_count,
called: Cell::new(false),
}
}
- async fn check(&self) -> anyhow::Result<bool> {
+ async fn count(&self) -> anyhow::Result<i64> {
self.called.set(true);
- Ok(self.has_inbox)
+ Ok(self.connected_inbox_count)
}
fn was_called(&self) -> bool {
@@ -28,40 +28,53 @@ impl InboxCheckSpy {
}
#[tokio::test]
-async fn paywalls_additional_inbox_without_professional_features() {
- let inbox = InboxCheckSpy::returning(true);
+async fn paywalls_inbox_at_free_limit_without_professional_features() {
+ let inboxes = InboxCountSpy::returning(FREE_INBOX_LIMIT);
- let result = enforce_inbox_paywall(false, || inbox.check()).await;
+ let result = enforce_inbox_paywall(false, || inboxes.count()).await;
assert!(
- inbox.was_called(),
- "non-professional users should be checked for an existing inbox"
+ inboxes.was_called(),
+ "non-professional users should be checked for connected inbox count"
);
assert!(matches!(result, Err(InitGmailLinkError::PaymentRequired)));
}
#[tokio::test]
async fn first_inbox_is_free_without_professional_features() {
- let inbox = InboxCheckSpy::returning(false);
+ let inboxes = InboxCountSpy::returning(0);
- let result = enforce_inbox_paywall(false, || inbox.check()).await;
+ let result = enforce_inbox_paywall(false, || inboxes.count()).await;
assert!(
- inbox.was_called(),
- "non-professional users should be checked for an existing inbox"
+ inboxes.was_called(),
+ "non-professional users should be checked for connected inbox count"
+ );
+ assert!(result.is_ok());
+}
+
+#[tokio::test]
+async fn second_inbox_is_free_without_professional_features() {
+ let inboxes = InboxCountSpy::returning(FREE_INBOX_LIMIT - 1);
+
+ let result = enforce_inbox_paywall(false, || inboxes.count()).await;
+
+ assert!(
+ inboxes.was_called(),
+ "non-professional users should be checked for connected inbox count"
);
assert!(result.is_ok());
}
#[tokio::test]
async fn professional_features_skip_existing_inbox_check() {
- let inbox = InboxCheckSpy::returning(true);
+ let inboxes = InboxCountSpy::returning(FREE_INBOX_LIMIT);
- let result = enforce_inbox_paywall(true, || inbox.check()).await;
+ let result = enforce_inbox_paywall(true, || inboxes.count()).await;
assert!(
- !inbox.was_called(),
- "professional users should never trigger the existing-inbox check"
+ !inboxes.was_called(),
+ "professional users should never trigger the connected-inbox count"
);
assert!(result.is_ok());
}