OXYGENHUB

Coverage / crates/hub/src/db/push.rs 100.0%

32 of 32 lines run, 8 of 16 functions · run #511 on main · run missed

1
//! Phones that take alerts (oxygenhub-android#16).
2
3
use super::{now_ms, Pool};
4
use sqlx::FromRow;
5
6
#[derive(Clone, Debug, FromRow)]
7
pub struct Device {
8
    pub token_id: i64,
9
    pub account_id: i64,
10
    pub fcm_token: String,
11
    pub public_key: String,
12
}
13
14
/// Register (or refresh) the phone signed in with token `token_id`.
153×
pub async fn register(pool: &Pool, token_id: i64, account_id: i64, fcm_token: &str, public_key: &str) -> sqlx::Result<()> {
163×
    let now = now_ms();
173×
    sqlx::query(
183×
        "INSERT INTO push_devices (token_id, account_id, fcm_token, public_key, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $5) \
193×
         ON CONFLICT (token_id) DO UPDATE SET fcm_token = excluded.fcm_token, public_key = excluded.public_key, updated_ms = excluded.updated_ms",
203×
    )
213×
    .bind(token_id)
223×
    .bind(account_id)
233×
    .bind(fcm_token)
243×
    .bind(public_key)
253×
    .bind(now)
263×
    .execute(pool)
273×
    .await?;
283×
    Ok(())
293×
}
30
311×
pub async fn forget(pool: &Pool, token_id: i64) -> sqlx::Result<bool> {
321×
    Ok(sqlx::query("DELETE FROM push_devices WHERE token_id = $1").bind(token_id).execute(pool).await?.rows_affected() > 0)
331×
}
34
35
/// Forget a registration Cloud Messaging says is gone.
361×
pub async fn forget_fcm(pool: &Pool, fcm_token: &str) -> sqlx::Result<()> {
371×
    sqlx::query("DELETE FROM push_devices WHERE fcm_token = $1").bind(fcm_token).execute(pool).await?;
381×
    Ok(())
391×
}
40
415×
pub async fn of_account(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<Device>> {
425×
    sqlx::query_as("SELECT token_id, account_id, fcm_token, public_key FROM push_devices WHERE account_id = $1")
435×
        .bind(account_id)
445×
        .fetch_all(pool)
455×
        .await
465×
}