From acbc1b8956d68c218f5b7cbd22ef9d55c46699a1 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Fri, 30 Aug 2024 15:51:20 +0100 Subject: [PATCH] feat(core/database): clear expired FCM tokens --- Cargo.toml | 2 -- crates/core/database/src/tasks/mod.rs | 2 +- crates/core/database/src/tasks/web_push.rs | 24 +++++++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9004c46c..61f6fa51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,3 @@ redis22 = { package = "redis", version = "0.22.3", git = "https://github.com/rev redis23 = { package = "redis", version = "0.23.1", git = "https://github.com/revoltchat/redis-rs", rev = "f8ca28ab85da59d2ccde526b4d2fb390eff5a5f9" } # authifier = { package = "authifier", version = "1.0.8", path = "../authifier/crates/authifier" } # rocket_authifier = { package = "rocket_authifier", version = "1.0.8", path = "../authifier/crates/rocket_authifier" } -[profile.release] -debug = false diff --git a/crates/core/database/src/tasks/mod.rs b/crates/core/database/src/tasks/mod.rs index cc204bf6..36531cc7 100644 --- a/crates/core/database/src/tasks/mod.rs +++ b/crates/core/database/src/tasks/mod.rs @@ -21,7 +21,7 @@ pub async fn start_workers(db: Database, authifier_db: authifier::Database) { task::spawn(ack::worker(db.clone(), authifier_db.clone())); task::spawn(last_message_id::worker(db.clone())); task::spawn(process_embeds::worker(db.clone())); - task::spawn(web_push::worker(authifier_db.clone())); + task::spawn(web_push::worker(db.clone(), authifier_db.clone())); } } diff --git a/crates/core/database/src/tasks/web_push.rs b/crates/core/database/src/tasks/web_push.rs index 97588a6d..43d30e07 100644 --- a/crates/core/database/src/tasks/web_push.rs +++ b/crates/core/database/src/tasks/web_push.rs @@ -3,7 +3,7 @@ use std::{ time::Duration, }; -use authifier::Database; +use authifier::Database as AuthifierDatabase; use base64::{ engine::{self}, Engine as _, @@ -20,6 +20,8 @@ use web_push::{ WebPushClient, WebPushMessageBuilder, }; +use crate::Database; + use super::apple_notifications; /// Task information @@ -54,7 +56,7 @@ pub async fn queue(recipients: Vec, payload: PushNotification) { } /// Start a new worker -pub async fn worker(db: Database) { +pub async fn worker(db: Database, authifier_db: AuthifierDatabase) { let config = config().await; let web_push_client = IsahcWebPushClient::new().unwrap(); @@ -89,7 +91,10 @@ pub async fn worker(db: Database) { loop { let task = Q.pop().await; - if let Ok(sessions) = db.find_sessions_with_subscription(&task.recipients).await { + if let Ok(sessions) = authifier_db + .find_sessions_with_subscription(&task.recipients) + .await + { for session in sessions { if let Some(sub) = session.subscription { if sub.endpoint == "fcm" { @@ -106,6 +111,19 @@ pub async fn worker(db: Database) { if let Err(err) = client.send(&message).await { error!("Failed to send FCM notification! {:?}", err); + + if let fcm_v1::Error::FCM(fcm_error) = err { + if fcm_error.contains("404 (Not Found)") { + println!("Unregistering {:?}", session.id); + + if let Err(err) = db + .remove_push_subscription_by_session_id(&session.id) + .await + { + revolt_config::capture_error(&err); + } + } + } } else { info!("Sent FCM notification to {:?}.", session.id); }