//! Phones that take alerts (oxygenhub-android#16). use super::{now_ms, Pool}; use sqlx::FromRow; #[derive(Clone, Debug, FromRow)] pub struct Device { pub token_id: i64, pub account_id: i64, pub fcm_token: String, pub public_key: String, } /// Register (or refresh) the phone signed in with token `token_id`. pub async fn register(pool: &Pool, token_id: i64, account_id: i64, fcm_token: &str, public_key: &str) -> sqlx::Result<()> { let now = now_ms(); sqlx::query( "INSERT INTO push_devices (token_id, account_id, fcm_token, public_key, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $5) \ ON CONFLICT (token_id) DO UPDATE SET fcm_token = excluded.fcm_token, public_key = excluded.public_key, updated_ms = excluded.updated_ms", ) .bind(token_id) .bind(account_id) .bind(fcm_token) .bind(public_key) .bind(now) .execute(pool) .await?; Ok(()) } pub async fn forget(pool: &Pool, token_id: i64) -> sqlx::Result { Ok(sqlx::query("DELETE FROM push_devices WHERE token_id = $1").bind(token_id).execute(pool).await?.rows_affected() > 0) } /// Forget a registration Cloud Messaging says is gone. pub async fn forget_fcm(pool: &Pool, fcm_token: &str) -> sqlx::Result<()> { sqlx::query("DELETE FROM push_devices WHERE fcm_token = $1").bind(fcm_token).execute(pool).await?; Ok(()) } pub async fn of_account(pool: &Pool, account_id: i64) -> sqlx::Result> { sqlx::query_as("SELECT token_id, account_id, fcm_token, public_key FROM push_devices WHERE account_id = $1") .bind(account_id) .fetch_all(pool) .await }