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.

actually enable the email lol (#2485)

Sean Aye2aa269dc52d8
diff --git a/rust/cloud-storage/comms_service/src/api/channels/add_participants.rs b/rust/cloud-storage/comms_service/src/api/channels/add_participants.rs
index 8c944d7d3c..6f066edf33 100644
--- a/rust/cloud-storage/comms_service/src/api/channels/add_participants.rs
+++ b/rust/cloud-storage/comms_service/src/api/channels/add_participants.rs
@@ -58,6 +58,10 @@ pub async fn handler(
));
}
+ let invited_by_user_id = MacroUserIdStr::parse_from_str(&channel_member.context.user_id)
+ .map_err(|_e| (StatusCode::BAD_REQUEST, "Invalid macro user id".to_string()))?
+ .into_owned();
+
let participants = to_lowercase(&req.participants);
for participant in participants.iter() {
@@ -100,14 +104,35 @@ pub async fn handler(
channel_type: model_notifications::ChannelType::mirror(channel_type),
channel_name: channel_name.clone(),
};
+
+ let sender_profile_picture_url =
+ comms_notification::get_sender_profile_picture_url(&ctx.db, &invited_by_user_id).await;
+
+ let parsed_recipients: Vec<_> = participants
+ .iter()
+ .filter_map(|id| MacroUserIdStr::parse_from_str(id).ok())
+ .collect();
+
+ let parsed_ids: Vec<_> = parsed_recipients.iter().map(|u| u.0.clone()).collect();
+
+ let existing_user_ids: std::collections::HashSet<String> =
+ macro_db_client::user::get_all::get_existing_users(&ctx.db, &parsed_ids)
+ .await
+ .inspect_err(|e| {
+ tracing::error!(error=?e, "unable to get existing users for invite");
+ })
+ .unwrap_or_default()
+ .into_iter()
+ .collect();
+
comms_notification::dispatch_notifications_for_invite(
- &ctx,
+ &*ctx.notification_ingress_service,
&channel_id,
- &MacroUserIdStr::parse_from_str(&channel_member.context.user_id)
- .map_err(|_e| (StatusCode::BAD_REQUEST, "Invalid macro user id".to_string()))?
- .into_owned(),
- req.participants.clone(),
- metadata.clone(),
+ &invited_by_user_id,
+ parsed_recipients,
+ existing_user_ids,
+ sender_profile_picture_url,
+ metadata,
)
.await
.inspect_err(|e| {
diff --git a/rust/cloud-storage/comms_service/src/notification.rs b/rust/cloud-storage/comms_service/src/notification.rs
index 91232ae58a..9ceea4a3a4 100644
--- a/rust/cloud-storage/comms_service/src/notification.rs
+++ b/rust/cloud-storage/comms_service/src/notification.rs
@@ -32,6 +32,9 @@ struct ChannelMessageEvent<'a> {
thread_participants: &'a [MacroUserIdStr<'static>],
thread_parent_sender_id: Option<MacroUserIdStr<'static>>,
sender_profile_picture_url: Option<String>,
+ /// Pre-computed set of existing user IDs; used by the `(0, None)` invite
+ /// branch to split recipients into push vs email delivery.
+ existing_user_ids: HashSet<String>,
}
fn recipients_excluding<'a>(
@@ -160,24 +163,16 @@ impl ChannelMessageEvent<'_> {
}
// Channel has no messages, send invite notification
(0, None) => {
- ingress
- .send_notification(
- SendNotificationRequestBuilder {
- notification_entity: entity(),
- notification: ChannelInviteMetadata {
- invited_by: self.message.sender_id.clone(),
- channel_name: self.channel_metadata.channel_name.clone(),
- sender_profile_picture_url: self.sender_profile_picture_url.clone(),
- },
- sender_id: sender(),
- recipient_ids: recipients_without_sender_and_mentions,
- }
- .into_request()
- .with_apns()
- .with_conn_gateway(),
- )
- .await
- .map_err(|e| anyhow::anyhow!("{e:?}"))?;
+ dispatch_notifications_for_invite(
+ ingress,
+ self.channel_id,
+ &self.message.sender_id,
+ recipients_without_sender_and_mentions.into_iter().collect(),
+ self.existing_user_ids.clone(),
+ self.sender_profile_picture_url.clone(),
+ self.channel_metadata.clone(),
+ )
+ .await?;
}
// Channel has messages, send message send notification
(_, None) => {
@@ -209,34 +204,20 @@ impl ChannelMessageEvent<'_> {
}
pub async fn dispatch_notifications_for_invite(
- api_context: &AppState,
+ ingres: &impl NotificationIngress,
channel_id: &Uuid,
invited_by_user_id: &MacroUserIdStr<'static>,
- recipient_user_ids: Vec<String>,
+ recipient_user_ids: Vec<MacroUserIdStr<'_>>,
+ existing_user_ids: HashSet<String>,
+ sender_profile_picture_url: Option<String>,
common: CommonChannelMetadata,
) -> anyhow::Result<()> {
- let parsed_recipients: Vec<_> = recipient_user_ids
- .iter()
- .filter_map(|id| MacroUserIdStr::parse_from_str(id).ok())
- .map(|u| u.0)
- .collect();
-
- let sender_profile_picture_url =
- get_sender_profile_picture_url(&api_context.db, invited_by_user_id).await;
-
- let existing_users: HashSet<String> =
- macro_db_client::user::get_all::get_existing_users(&api_context.db, &parsed_recipients)
- .await?
- .into_iter()
- .collect();
-
- let (existing_users, not_existing_users): (HashSet<_>, HashSet<_>) = parsed_recipients
+ let (existing_users, not_existing_users): (HashSet<_>, HashSet<_>) = recipient_user_ids
.into_iter()
- .map(MacroUserIdStr)
- .partition(|id| existing_users.contains(id.as_ref()));
+ .partition(|id| existing_user_ids.contains(id.as_ref()));
let _ = tokio::try_join!(
- api_context.notification_ingress_service.send_notification(
+ ingres.send_notification(
SendNotificationRequestBuilder {
notification_entity: EntityType::Channel.with_entity_string(channel_id.to_string()),
notification: ChannelInviteMetadata {
@@ -251,7 +232,7 @@ pub async fn dispatch_notifications_for_invite(
.with_apns()
.with_conn_gateway(),
),
- api_context.notification_ingress_service.send_notification(
+ ingres.send_notification(
SendNotificationRequestBuilder {
notification_entity: EntityType::Channel.with_entity_string(channel_id.to_string()),
notification: ChannelInviteMetadata {
@@ -263,8 +244,7 @@ pub async fn dispatch_notifications_for_invite(
recipient_ids: not_existing_users,
}
.into_request()
- .with_apns()
- .with_conn_gateway(),
+ .with_email(),
)
)
.map_err(|e| anyhow::anyhow!("{e:?}"))?;
@@ -283,6 +263,22 @@ pub async fn dispatch_notifications_for_message(
let channel_message_count =
check_if_channel_has_messages(&api_context.db, channel_id).await? as usize;
+ // When this is the first message in the channel, look up which
+ // participants already have accounts so the invite branch can split
+ // push (existing) vs email (non-existing) delivery.
+ let existing_user_ids: HashSet<String> = if channel_message_count == 0
+ && message.thread_id.is_none()
+ {
+ let participant_ids: Vec<_> = participants.iter().map(|p| p.user_id.0.clone()).collect();
+
+ macro_db_client::user::get_all::get_existing_users(&api_context.db, &participant_ids)
+ .await?
+ .into_iter()
+ .collect()
+ } else {
+ HashSet::new()
+ };
+
let (user_mentions, document_mention_ids) =
mentions
.into_iter()
@@ -337,12 +333,13 @@ pub async fn dispatch_notifications_for_message(
thread_participants: &thread_participants,
thread_parent_sender_id,
sender_profile_picture_url,
+ existing_user_ids,
}
.send(&*api_context.notification_ingress_service)
.await
}
-async fn get_sender_profile_picture_url(
+pub async fn get_sender_profile_picture_url(
db: &sqlx::PgPool,
sender_id: &MacroUserIdStr<'_>,
) -> Option<String> {
diff --git a/rust/cloud-storage/comms_service/src/notification/test.rs b/rust/cloud-storage/comms_service/src/notification/test.rs
index 7746b6a486..be15288114 100644
--- a/rust/cloud-storage/comms_service/src/notification/test.rs
+++ b/rust/cloud-storage/comms_service/src/notification/test.rs
@@ -143,6 +143,7 @@ async fn mentioned_users_get_mention_not_message_send() {
thread_participants: &[],
thread_parent_sender_id: None,
sender_profile_picture_url: None,
+ existing_user_ids: HashSet::new(),
}
.send(&ingress)
.await
@@ -199,6 +200,7 @@ async fn thread_reply_excludes_sender_and_mentions() {
thread_participants: &thread_participants,
thread_parent_sender_id: Some(uid("macro|[email protected]")),
sender_profile_picture_url: None,
+ existing_user_ids: HashSet::new(),
}
.send(&ingress)
.await
@@ -217,3 +219,69 @@ async fn thread_reply_excludes_sender_and_mentions() {
assert!(recipients.contains("macro|[email protected]"));
assert!(recipients.contains("macro|[email protected]"));
}
+
+fn has_email(req: &serde_json::Value) -> bool {
+ !req["build_email"].is_null()
+}
+
+#[tokio::test]
+async fn first_message_sends_email_invite_to_non_existing_users() {
+ let channel_id = Uuid::new_v4();
+ let participants = vec![
+ participant(uid("macro|[email protected]"), channel_id),
+ participant(uid("macro|[email protected]"), channel_id),
+ participant(uid("macro|[email protected]"), channel_id),
+ ];
+ let msg = message(channel_id, uid("macro|[email protected]"), None);
+ let metadata = private_metadata();
+
+ // Only [email protected] is a known user
+ let existing_user_ids: HashSet<String> = HashSet::from(["macro|[email protected]".to_string()]);
+
+ let ingress = MockNotificationIngress::new();
+ ChannelMessageEvent {
+ channel_id: &channel_id,
+ message: &msg,
+ channel_metadata: &metadata,
+ channel_message_count: 0,
+ user_mentions: &[],
+ document_mentions: &[],
+ participants: &participants,
+ thread_participants: &[],
+ thread_parent_sender_id: None,
+ sender_profile_picture_url: None,
+ existing_user_ids,
+ }
+ .send(&ingress)
+ .await
+ .unwrap();
+
+ let requests = ingress.recorded_requests();
+
+ // Should produce exactly two channel_invite notifications
+ let invites: Vec<_> = requests
+ .iter()
+ .filter(|r| get_type_name(r) == "channel_invite")
+ .collect();
+ assert_eq!(invites.len(), 2, "expected two invite notifications");
+
+ // The invite to the existing user should NOT have email
+ let existing_invite = invites
+ .iter()
+ .find(|r| get_recipient_ids(r).contains("macro|[email protected]"))
+ .expect("should have invite for existing user");
+ assert!(
+ !has_email(existing_invite),
+ "existing user invite should not have email"
+ );
+
+ // The invite to the non-existing user SHOULD have email
+ let new_user_invite = invites
+ .iter()
+ .find(|r| get_recipient_ids(r).contains("macro|[email protected]"))
+ .expect("should have invite for non-existing user");
+ assert!(
+ has_email(new_user_invite),
+ "non-existing user invite should have email"
+ );
+}