mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-29 16:56:28 +00:00
Pushing notifications is working
This commit is contained in:
parent
28c0494fe9
commit
52ccdf26e4
@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::data::config::{conf, IndependentPushService};
|
use crate::data::config::{conf, IndependentPushService};
|
||||||
use crate::data::error::{ExecError, Res};
|
use crate::data::error::{ExecError, Res};
|
||||||
|
use crate::data::push_notification::PushNotification;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct CreateTokenResult {
|
struct CreateTokenResult {
|
||||||
@ -21,6 +22,23 @@ struct RemoveTokenRequest {
|
|||||||
client: String,
|
client: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct PushNotificationRequest {
|
||||||
|
clients: Vec<String>,
|
||||||
|
id: String,
|
||||||
|
title: String,
|
||||||
|
body: String,
|
||||||
|
image: Option<String>,
|
||||||
|
timeout: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct CancelNotificationRequest {
|
||||||
|
clients: Vec<String>,
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
fn svc_conf() -> Res<&'static IndependentPushService> {
|
fn svc_conf() -> Res<&'static IndependentPushService> {
|
||||||
match &conf().independent_push_service {
|
match &conf().independent_push_service {
|
||||||
Some(c) => Ok(c),
|
Some(c) => Ok(c),
|
||||||
@ -60,3 +78,43 @@ pub fn remove_token(t: &str) -> Res {
|
|||||||
Err(ExecError::boxed_string(
|
Err(ExecError::boxed_string(
|
||||||
format!("Failed to remove client, got a status {} for service!", client.status().as_u16())))
|
format!("Failed to remove client, got a status {} for service!", client.status().as_u16())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Push notifications
|
||||||
|
pub fn push_notifications(n: &PushNotification, targets: Vec<String>) -> Res {
|
||||||
|
let client = create_client()?
|
||||||
|
.get(&format!("{}{}", svc_conf()?.control_url, "push_notification"))
|
||||||
|
.json(&PushNotificationRequest {
|
||||||
|
clients: targets,
|
||||||
|
id: n.id.clone(),
|
||||||
|
title: n.title.clone(),
|
||||||
|
body: n.body.clone(),
|
||||||
|
image: n.image.clone(),
|
||||||
|
timeout: n.timeout.clone(),
|
||||||
|
})
|
||||||
|
.send()?;
|
||||||
|
|
||||||
|
if client.status() == StatusCode::OK {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(ExecError::boxed_string(
|
||||||
|
format!("Failed to push notification, got a status {} for service!", client.status().as_u16())))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancel notifications
|
||||||
|
pub fn cancel_notifications(id: String, targets: Vec<String>) -> Res {
|
||||||
|
let client = create_client()?
|
||||||
|
.get(&format!("{}{}", svc_conf()?.control_url, "remove_notification"))
|
||||||
|
.json(&CancelNotificationRequest {
|
||||||
|
clients: targets,
|
||||||
|
id: id.clone(),
|
||||||
|
})
|
||||||
|
.send()?;
|
||||||
|
|
||||||
|
if client.status() == StatusCode::OK {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(ExecError::boxed_string(
|
||||||
|
format!("Failed to remove notification, got a status {} for service!", client.status().as_u16())))
|
||||||
|
}
|
@ -25,18 +25,41 @@ pub fn un_register_from_previous_service(client: &UserAccessToken) -> Res {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Split tokens in categories : (Independent, Firebase)
|
||||||
|
fn split_tokens(targets: Vec<UserAccessToken>) -> (Vec<String>) {
|
||||||
|
let mut independent = vec![];
|
||||||
|
|
||||||
|
for target in targets {
|
||||||
|
match target.push_notifications_token {
|
||||||
|
PushNotificationToken::INDEPENDENT(token) => {
|
||||||
|
independent.push(token)
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(independent)
|
||||||
|
}
|
||||||
|
|
||||||
/// Push a notification to specific tokens
|
/// Push a notification to specific tokens
|
||||||
fn push_notification(n: &PushNotification, targets: Vec<PushNotificationToken>) -> Res {
|
fn push_notification(n: &PushNotification, targets: Vec<UserAccessToken>) -> Res {
|
||||||
// TODO : implement
|
let independents = split_tokens(targets);
|
||||||
println!("* Push notification: {:#?} \n* To {:?}", n, targets);
|
|
||||||
|
// Push independent notifications
|
||||||
|
if !independents.is_empty() {
|
||||||
|
independent_push_notifications_service_helper::push_notifications(n, independents)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cancel a notification for specific tokens (optional)
|
/// Cancel a notification for specific tokens (optional)
|
||||||
fn cancel_notification(id: String, targets: Vec<PushNotificationToken>) -> Res {
|
fn cancel_notification(id: String, targets: Vec<UserAccessToken>) -> Res {
|
||||||
// TODO : implement
|
let independents = split_tokens(targets);
|
||||||
println!("* Cancel push notification: {:#?} \n* For {:?}", id, targets);
|
|
||||||
|
if !independents.is_empty() {
|
||||||
|
independent_push_notifications_service_helper::cancel_notifications(id, independents)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -51,7 +74,7 @@ fn push_notification_to_users(n: &PushNotification, users: Vec<UserID>) -> Res {
|
|||||||
|
|
||||||
for user in dest_users {
|
for user in dest_users {
|
||||||
for token in account_helper::get_all_login_tokens(&user)? {
|
for token in account_helper::get_all_login_tokens(&user)? {
|
||||||
dest_tokens.push(token.push_notifications_token);
|
dest_tokens.push(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +87,7 @@ fn cancel_notification_for_users(id: String, users: Vec<UserID>) -> Res {
|
|||||||
|
|
||||||
for user in users {
|
for user in users {
|
||||||
for token in account_helper::get_all_login_tokens(&user)? {
|
for token in account_helper::get_all_login_tokens(&user)? {
|
||||||
dest_tokens.push(token.push_notifications_token);
|
dest_tokens.push(token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user