forked from jmug/stoatchat
feat(core/database): clear expired FCM tokens
This commit is contained in:
@@ -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" }
|
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" }
|
# 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" }
|
# rocket_authifier = { package = "rocket_authifier", version = "1.0.8", path = "../authifier/crates/rocket_authifier" }
|
||||||
[profile.release]
|
|
||||||
debug = false
|
|
||||||
|
|||||||
@@ -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(ack::worker(db.clone(), authifier_db.clone()));
|
||||||
task::spawn(last_message_id::worker(db.clone()));
|
task::spawn(last_message_id::worker(db.clone()));
|
||||||
task::spawn(process_embeds::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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::{
|
|||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use authifier::Database;
|
use authifier::Database as AuthifierDatabase;
|
||||||
use base64::{
|
use base64::{
|
||||||
engine::{self},
|
engine::{self},
|
||||||
Engine as _,
|
Engine as _,
|
||||||
@@ -20,6 +20,8 @@ use web_push::{
|
|||||||
WebPushClient, WebPushMessageBuilder,
|
WebPushClient, WebPushMessageBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::Database;
|
||||||
|
|
||||||
use super::apple_notifications;
|
use super::apple_notifications;
|
||||||
|
|
||||||
/// Task information
|
/// Task information
|
||||||
@@ -54,7 +56,7 @@ pub async fn queue(recipients: Vec<String>, payload: PushNotification) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Start a new worker
|
/// Start a new worker
|
||||||
pub async fn worker(db: Database) {
|
pub async fn worker(db: Database, authifier_db: AuthifierDatabase) {
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
|
|
||||||
let web_push_client = IsahcWebPushClient::new().unwrap();
|
let web_push_client = IsahcWebPushClient::new().unwrap();
|
||||||
@@ -89,7 +91,10 @@ pub async fn worker(db: Database) {
|
|||||||
loop {
|
loop {
|
||||||
let task = Q.pop().await;
|
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 {
|
for session in sessions {
|
||||||
if let Some(sub) = session.subscription {
|
if let Some(sub) = session.subscription {
|
||||||
if sub.endpoint == "fcm" {
|
if sub.endpoint == "fcm" {
|
||||||
@@ -106,6 +111,19 @@ pub async fn worker(db: Database) {
|
|||||||
|
|
||||||
if let Err(err) = client.send(&message).await {
|
if let Err(err) = client.send(&message).await {
|
||||||
error!("Failed to send FCM notification! {:?}", err);
|
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 {
|
} else {
|
||||||
info!("Sent FCM notification to {:?}.", session.id);
|
info!("Sent FCM notification to {:?}.", session.id);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user