Update telemetry
This commit is contained in:
@@ -1,48 +1,39 @@
|
||||
use crate::{
|
||||
routes::{Query, generate_token},
|
||||
routes::{AppError, Query, generate_token, not_found_html},
|
||||
startup::AppState,
|
||||
templates::ConfirmTemplate,
|
||||
templates::{ConfirmTemplate, HtmlTemplate},
|
||||
};
|
||||
use askama::Template;
|
||||
use anyhow::Context;
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tracing::instrument(name = "Confirming new subscriber", skip(params))]
|
||||
#[tracing::instrument(name = "Confirming new subscriber", skip_all)]
|
||||
pub async fn confirm(
|
||||
State(AppState {
|
||||
connection_pool, ..
|
||||
}): State<AppState>,
|
||||
Query(params): Query<Params>,
|
||||
) -> Response {
|
||||
let Ok(subscriber_id) =
|
||||
get_subscriber_id_from_token(&connection_pool, ¶ms.subscription_token).await
|
||||
else {
|
||||
return StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||
};
|
||||
if let Some(subscriber_id) = subscriber_id {
|
||||
if confirm_subscriber(&connection_pool, &subscriber_id)
|
||||
) -> Result<Response, AppError> {
|
||||
let subscriber_id = get_subscriber_id_from_token(&connection_pool, ¶ms.subscription_token)
|
||||
.await
|
||||
.context("Could not fetch subscriber id given subscription token.")?;
|
||||
if let Some(id) = subscriber_id {
|
||||
confirm_subscriber(&connection_pool, &id)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
StatusCode::INTERNAL_SERVER_ERROR.into_response()
|
||||
} else {
|
||||
Html(ConfirmTemplate.render().unwrap()).into_response()
|
||||
}
|
||||
.context("Failed to update subscriber status.")?;
|
||||
let template = HtmlTemplate(ConfirmTemplate);
|
||||
Ok(template.into_response())
|
||||
} else {
|
||||
StatusCode::UNAUTHORIZED.into_response()
|
||||
Ok(not_found_html())
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "Mark subscriber as confirmed",
|
||||
skip(connection_pool, subscriber_id)
|
||||
)]
|
||||
#[tracing::instrument(name = "Mark subscriber as confirmed", skip(connection_pool))]
|
||||
async fn confirm_subscriber(
|
||||
connection_pool: &PgPool,
|
||||
subscriber_id: &Uuid,
|
||||
@@ -53,18 +44,11 @@ async fn confirm_subscriber(
|
||||
subscriber_id
|
||||
)
|
||||
.execute(connection_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("Failed to execute query: {:?}", e);
|
||||
e
|
||||
})?;
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "Get subscriber_id from token",
|
||||
skip(connection, subscription_token)
|
||||
)]
|
||||
#[tracing::instrument(name = "Get subscriber id from token", skip(connection))]
|
||||
async fn get_subscriber_id_from_token(
|
||||
connection: &PgPool,
|
||||
subscription_token: &str,
|
||||
@@ -74,11 +58,7 @@ async fn get_subscriber_id_from_token(
|
||||
subscription_token
|
||||
)
|
||||
.fetch_optional(connection)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("Failed to execute query: {:?}", e);
|
||||
e
|
||||
})?;
|
||||
.await?;
|
||||
Ok(saved.map(|r| r.subscriber_id))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user