-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add admin api to send targeted push notifications
- Loading branch information
Showing
7 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use crate::db; | ||
use crate::notifications::FcmToken; | ||
use crate::notifications::Notification; | ||
use crate::notifications::NotificationKind; | ||
use crate::routes::AppState; | ||
use crate::AppError; | ||
use axum::extract::State; | ||
use axum::Json; | ||
use bitcoin::secp256k1::PublicKey; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use std::sync::Arc; | ||
use tracing::instrument; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct PushCampaignParams { | ||
pub node_ids: Vec<PublicKey>, | ||
pub title: String, | ||
pub message: String, | ||
pub dry_run: Option<bool>, | ||
} | ||
|
||
#[instrument(skip_all, err(Debug))] | ||
pub async fn post_push_campaign( | ||
State(state): State<Arc<AppState>>, | ||
params: Json<PushCampaignParams>, | ||
) -> Result<String, AppError> { | ||
let params = params.0; | ||
tracing::info!(?params, "Sending campaign with push notifications"); | ||
|
||
let mut conn = state | ||
.pool | ||
.get() | ||
.map_err(|e| AppError::InternalServerError(format!("Could not get connection: {e:#}")))?; | ||
|
||
let users = db::user::get_users(&mut conn, params.node_ids) | ||
.map_err(|e| AppError::InternalServerError(format!("Failed to get users: {e:#}")))?; | ||
|
||
let fcm_tokens = users | ||
.iter() | ||
.map(|user| user.fcm_token.clone()) | ||
.filter(|token| !token.is_empty() && token != "unavailable") | ||
.map(|token| FcmToken::new(token).expect("token to not be empty")) | ||
.collect::<Vec<_>>(); | ||
|
||
let notification_kind = NotificationKind::Campaign { | ||
title: params.title.clone(), | ||
message: params.message.clone(), | ||
}; | ||
|
||
tracing::info!( | ||
"Sending push notification campaign (title: {}, message: {} to {} users", | ||
params.title, | ||
params.message, | ||
fcm_tokens.len(), | ||
); | ||
|
||
if params.dry_run.unwrap_or(true) { | ||
tracing::debug!("Not sending push notification campaign because of dry run flag."); | ||
} else { | ||
state | ||
.notification_sender | ||
.send(Notification::new_batch( | ||
fcm_tokens.clone(), | ||
notification_kind, | ||
)) | ||
.await | ||
.map_err(|e| { | ||
AppError::InternalServerError(format!("Failed to send push notifications: {e:#}")) | ||
})?; | ||
} | ||
|
||
Ok(format!( | ||
"Sending push notification campaign (title: {}, message: {} to {} users", | ||
params.title, | ||
params.message, | ||
fcm_tokens.len(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters