From 956b10c7ad57cadc96cfb215888f41f7d3a14bb4 Mon Sep 17 00:00:00 2001 From: Chris Smith <1979423+chris13524@users.noreply.github.com> Date: Wed, 6 Mar 2024 14:17:17 -0800 Subject: [PATCH] fix: move welcome notification insertion sooner (#393) --- .../handlers/notify_subscribe.rs | 92 +++++++++---------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/src/services/public_http_server/handlers/relay_webhook/handlers/notify_subscribe.rs b/src/services/public_http_server/handlers/relay_webhook/handlers/notify_subscribe.rs index be572391..84a2df8c 100644 --- a/src/services/public_http_server/handlers/relay_webhook/handlers/notify_subscribe.rs +++ b/src/services/public_http_server/handlers/relay_webhook/handlers/notify_subscribe.rs @@ -177,7 +177,50 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R }; info!("Timing: Finished upserting subscriber"); - let notify_topic = subscriber.topic; + // TODO do in same txn as upsert_subscriber() + if subscriber.inserted { + let welcome_notification = + get_welcome_notification(project.id, &state.postgres, state.metrics.as_ref()) + .await + .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; // TODO change to client error? + if let Some(welcome_notification) = welcome_notification { + info!("Welcome notification enabled"); + if welcome_notification.enabled && scope.contains(&welcome_notification.r#type) { + info!("Scope contains welcome notification type, sending welcome notification"); + let notification = upsert_notification( + Uuid::new_v4().to_string(), + project.id, + Notification { + r#type: welcome_notification.r#type, + title: welcome_notification.title, + body: welcome_notification.body, + url: welcome_notification.url, + icon: None, + }, + &state.postgres, + state.metrics.as_ref(), + ) + .await + .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; // TODO change to client error? + + upsert_subscriber_notifications( + notification.id, + &[subscriber.id], + &state.postgres, + state.metrics.as_ref(), + ) + .await + .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; + // TODO change to client error? + } else { + info!("Scope does not contain welcome notification type, not sending welcome notification"); + } + } else { + info!("Welcome notification not enabled"); + } + } else { + info!("Subscriber already existed, not sending welcome notification"); + } // TODO do in same transaction as upsert_subscriber() state @@ -194,6 +237,8 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R .await .map_err(RelayMessageServerError::NotifyServerError)?; // TODO change to client error? + let notify_topic = subscriber.topic; + info!("Timing: Subscribing to notify_topic: {notify_topic}"); subscribe_relay_topic(&state.relay_client, ¬ify_topic, state.metrics.as_ref()) .await @@ -273,51 +318,6 @@ pub async fn handle(msg: RelayIncomingMessage, state: &AppState) -> Result<(), R info!("Finished publishing subscribe response"); } - // TODO do in same txn as upsert_subscriber() - if subscriber.inserted { - let welcome_notification = - get_welcome_notification(project.id, &state.postgres, state.metrics.as_ref()) - .await - .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; // TODO change to client error? - if let Some(welcome_notification) = welcome_notification { - info!("Welcome notification enabled"); - if welcome_notification.enabled && scope.contains(&welcome_notification.r#type) { - info!("Scope contains welcome notification type, sending welcome notification"); - let notification = upsert_notification( - Uuid::new_v4().to_string(), - project.id, - Notification { - r#type: welcome_notification.r#type, - title: welcome_notification.title, - body: welcome_notification.body, - url: welcome_notification.url, - icon: None, - }, - &state.postgres, - state.metrics.as_ref(), - ) - .await - .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; // TODO change to client error? - - upsert_subscriber_notifications( - notification.id, - &[subscriber.id], - &state.postgres, - state.metrics.as_ref(), - ) - .await - .map_err(|e| RelayMessageServerError::NotifyServerError(e.into()))?; - // TODO change to client error? - } else { - info!("Scope does not contain welcome notification type, not sending welcome notification"); - } - } else { - info!("Welcome notification not enabled"); - } - } else { - info!("Subscriber already existed, not sending welcome notification"); - } - send_to_subscription_watchers( watchers_with_subscriptions, &state.notify_keys.authentication_secret,