use crate::error::{ApiErrorKind, ApiResult};
use crate::extractors::routers::RouterType;
use crate::server::AppState;
use actix_http::StatusCode;
use autopush_common::db::{client::DbClient, User};
use cadence::{CountedExt, StatsdClient};
use uuid::Uuid;
pub async fn validate_user(
user: &User,
channel_id: &Uuid,
app_state: &AppState,
) -> ApiResult<RouterType> {
let router_type = match user.router_type.parse::<RouterType>() {
Ok(router_type) => router_type,
Err(_) => {
debug!("Unknown router type, dropping user"; "user" => ?user);
drop_user(user.uaid, app_state.db.as_ref(), &app_state.metrics).await?;
return Err(ApiErrorKind::NoSubscription.into());
}
};
if router_type == RouterType::GCM {
debug!("Encountered GCM record, dropping user"; "user" => ?user);
app_state
.metrics
.incr_with_tags("notification.bridge.error")
.with_tag("platform", "gcm")
.with_tag("reason", "gcm_kill")
.with_tag("error", &StatusCode::GONE.to_string())
.send();
drop_user(user.uaid, app_state.db.as_ref(), &app_state.metrics).await?;
return Err(ApiErrorKind::Router(crate::routers::RouterError::NotFound).into());
}
if router_type == RouterType::WebPush {
validate_webpush_user(user, channel_id, app_state.db.as_ref()).await?;
}
Ok(router_type)
}
async fn validate_webpush_user(user: &User, channel_id: &Uuid, db: &dyn DbClient) -> ApiResult<()> {
let channel_ids = db.get_channels(&user.uaid).await?;
if !channel_ids.contains(channel_id) {
return Err(ApiErrorKind::NoSubscription.into());
}
Ok(())
}
pub async fn drop_user(uaid: Uuid, db: &dyn DbClient, metrics: &StatsdClient) -> ApiResult<()> {
metrics
.incr_with_tags("updates.drop_user")
.with_tag("errno", "102")
.send();
db.remove_user(&uaid).await?;
Ok(())
}