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.

fix(notifs): malformed payload (#2009)

Sean Ayee4cf7becc052
diff --git a/rust/cloud-storage/notification/src/outbound/repository.rs b/rust/cloud-storage/notification/src/outbound/repository.rs
index ae6141c671..08158fa169 100644
--- a/rust/cloud-storage/notification/src/outbound/repository.rs
+++ b/rust/cloud-storage/notification/src/outbound/repository.rs
@@ -269,7 +269,7 @@ impl NotificationDbOps for PgPool {
apns_collapse_key: Option<&str>,
) -> Result<Option<Vec<UserNotificationRow<Arc<T>>>>, Report> {
let entity_type: &str = request.notification_entity.entity_type.into();
- let metadata = serde_json::to_value(&request.notification)?;
+ let metadata = serde_json::to_value(&request.notification.content)?;
let mut tx = self.begin().await?;
diff --git a/rust/cloud-storage/notification/src/outbound/repository/test.rs b/rust/cloud-storage/notification/src/outbound/repository/test.rs
index 3d66ede6d1..a9182c2595 100644
--- a/rust/cloud-storage/notification/src/outbound/repository/test.rs
+++ b/rust/cloud-storage/notification/src/outbound/repository/test.rs
@@ -435,6 +435,54 @@ async fn test_create_notification_returns_none_on_conflict(pool: Pool<Postgres>)
assert_eq!(user_count, 1);
}
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn test_create_notification_stores_bare_metadata(pool: Pool<Postgres>) {
+ let recipient = test_user("[email protected]");
+ let notification_id = uuid::Uuid::new_v4();
+
+ let request = SendNotificationRequestBuilder {
+ notification_entity: EntityType::Document.with_entity_str("doc-1"),
+ notification: TaggedContent::new(TestNotification {
+ message: "hello".to_string(),
+ }),
+ sender_id: None,
+ recipient_ids: std::collections::HashSet::from([recipient.clone()]),
+ };
+
+ pool.create_notification(request, notification_id, "test_service", None)
+ .await
+ .unwrap();
+
+ // Read the raw metadata JSON from the DB
+ let row = sqlx::query!(
+ r#"SELECT metadata as "metadata: serde_json::Value" FROM notification WHERE id = $1"#,
+ notification_id
+ )
+ .fetch_one(&pool)
+ .await
+ .unwrap();
+
+ let metadata = row.metadata;
+
+ // The metadata column must store bare content, not the TaggedContent wrapper.
+ // Bare content: {"message": "hello"}
+ // Wrong (wrapped): {"tag": "test_notification", "content": {"message": "hello"}}
+ assert!(
+ !metadata.as_object().unwrap().contains_key("tag"),
+ "metadata should not contain a 'tag' key — it should be bare content, not TaggedContent. Got: {metadata}"
+ );
+ assert_eq!(metadata["message"], "hello");
+
+ // Verify round-trip: get_user_notifications deserializes the bare metadata back into T
+ let rows: Vec<UserNotificationRow<TestNotification>> = pool
+ .get_user_notifications(recipient, 10, Query::Sort(CreatedAt, ()))
+ .await
+ .unwrap();
+
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].notification_metadata.message, "hello");
+}
+
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(path = "../../../fixtures", scripts("user_notifications"))
diff --git a/rust/cloud-storage/notification_service/src/api/user_notification.rs b/rust/cloud-storage/notification_service/src/api/user_notification.rs
index c40793e0da..f214043d6b 100644
--- a/rust/cloud-storage/notification_service/src/api/user_notification.rs
+++ b/rust/cloud-storage/notification_service/src/api/user_notification.rs
@@ -91,6 +91,7 @@ pub struct GetAllUserNotificationsResponse {
/// Convert a [`UserNotificationRow<serde_json::Value>`] into a
/// [`UserNotificationRow<NotifEvent>`] by tagging and deserializing the metadata.
+#[tracing::instrument(err)]
pub fn to_typed_row(
row: UserNotificationRow<serde_json::Value>,
) -> Result<UserNotificationRow<NotifEvent>, serde_json::Error> {
diff --git a/rust/cloud-storage/notification_service/src/api/user_notification/test.rs b/rust/cloud-storage/notification_service/src/api/user_notification/test.rs
index 4a1e38f888..b3e0fabca5 100644
--- a/rust/cloud-storage/notification_service/src/api/user_notification/test.rs
+++ b/rust/cloud-storage/notification_service/src/api/user_notification/test.rs
@@ -166,6 +166,64 @@ fn to_typed_row_unknown_event_type_fails() {
assert!(to_typed_row(row).is_err());
}
+/// Simulates listing notifications when some DB rows have malformed metadata.
+/// Valid rows must still be returned; malformed rows are silently dropped.
+/// This mirrors the `partition_map` logic in `list_typed_notifications`.
+#[test]
+fn to_typed_row_returns_valid_notifications_despite_malformed_rows() {
+ let valid_mention = make_row(
+ "channel_mention",
+ serde_json::json!({
+ "messageId": "msg-1",
+ "messageContent": "hello @user",
+ "channelType": "Public",
+ "channelName": "general"
+ }),
+ );
+ let malformed_metadata = make_row("channel_mention", serde_json::json!({"garbage": true}));
+ let unknown_event = make_row("totally_bogus_event", serde_json::json!({"a": 1}));
+ let valid_email = make_row(
+ "new_email",
+ serde_json::json!({
+ "sender": "[email protected]",
+ "toEmail": "[email protected]",
+ "threadId": "thread-2",
+ "subject": "Hello",
+ "snippet": "Hi there"
+ }),
+ );
+ let empty_metadata = make_row("channel_message_send", serde_json::json!({}));
+
+ let rows = vec![
+ valid_mention,
+ malformed_metadata,
+ unknown_event,
+ valid_email,
+ empty_metadata,
+ ];
+
+ let (ok, err): (Vec<_>, Vec<_>) = rows
+ .into_iter()
+ .map(|r| to_typed_row(r))
+ .partition(Result::is_ok);
+
+ assert_eq!(ok.len(), 2, "expected exactly the 2 valid rows to succeed");
+ assert_eq!(
+ err.len(),
+ 3,
+ "expected exactly the 3 malformed rows to fail"
+ );
+
+ assert!(matches!(
+ ok[0].as_ref().unwrap().notification_metadata,
+ NotifEvent::ChannelMention(_)
+ ));
+ assert!(matches!(
+ ok[1].as_ref().unwrap().notification_metadata,
+ NotifEvent::NewEmail(_)
+ ));
+}
+
#[test]
fn to_typed_row_preserves_row_fields() {
let metadata = serde_json::json!({