Skip to content

Commit

Permalink
fix: move welcome notification insertion sooner (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 authored Mar 6, 2024
1 parent 3a23ccc commit 956b10c
Showing 1 changed file with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, &notify_topic, state.metrics.as_ref())
.await
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 956b10c

Please sign in to comment.