feat: Push Notifications

Co-authored-by: samb-devel <125741162+samb-devel@users.noreply.github.com>
Co-authored-by: Zoruk <Zoruk@users.noreply.github.com>
This commit is contained in:
GeekCornerGH
2023-06-11 13:28:18 +02:00
parent adf67a8ee8
commit 2d66292350
22 changed files with 526 additions and 70 deletions

View File

@ -21,7 +21,10 @@ use tokio_tungstenite::{
use crate::{
auth::ClientIp,
db::models::{Cipher, Folder, Send as DbSend, User},
db::{
models::{Cipher, Folder, Send as DbSend, User},
DbConn,
},
Error, CONFIG,
};
@ -33,6 +36,8 @@ static WS_USERS: Lazy<Arc<WebSocketUsers>> = Lazy::new(|| {
})
});
use super::{push_cipher_update, push_folder_update, push_logout, push_send_update, push_user_update};
pub fn routes() -> Vec<Route> {
routes![websockets_hub]
}
@ -233,19 +238,33 @@ impl WebSocketUsers {
);
self.send_update(&user.uuid, &data).await;
if CONFIG.push_enabled() {
push_user_update(ut, user).await;
}
}
pub async fn send_logout(&self, user: &User, acting_device_uuid: Option<String>) {
pub async fn send_logout(&self, user: &User, acting_device_uuid: Option<String>, conn: &mut DbConn) {
let data = create_update(
vec![("UserId".into(), user.uuid.clone().into()), ("Date".into(), serialize_date(user.updated_at))],
UpdateType::LogOut,
acting_device_uuid,
acting_device_uuid.clone(),
);
self.send_update(&user.uuid, &data).await;
if CONFIG.push_enabled() {
push_logout(user, acting_device_uuid, conn).await;
}
}
pub async fn send_folder_update(&self, ut: UpdateType, folder: &Folder, acting_device_uuid: &String) {
pub async fn send_folder_update(
&self,
ut: UpdateType,
folder: &Folder,
acting_device_uuid: &String,
conn: &mut DbConn,
) {
let data = create_update(
vec![
("Id".into(), folder.uuid.clone().into()),
@ -257,6 +276,10 @@ impl WebSocketUsers {
);
self.send_update(&folder.user_uuid, &data).await;
if CONFIG.push_enabled() {
push_folder_update(ut, folder, acting_device_uuid, conn).await;
}
}
pub async fn send_cipher_update(
@ -266,6 +289,7 @@ impl WebSocketUsers {
user_uuids: &[String],
acting_device_uuid: &String,
collection_uuids: Option<Vec<String>>,
conn: &mut DbConn,
) {
let org_uuid = convert_option(cipher.organization_uuid.clone());
// Depending if there are collections provided or not, we need to have different values for the following variables.
@ -295,9 +319,13 @@ impl WebSocketUsers {
for uuid in user_uuids {
self.send_update(uuid, &data).await;
}
if CONFIG.push_enabled() && user_uuids.len() == 1 {
push_cipher_update(ut, cipher, acting_device_uuid, conn).await;
}
}
pub async fn send_send_update(&self, ut: UpdateType, send: &DbSend, user_uuids: &[String]) {
pub async fn send_send_update(&self, ut: UpdateType, send: &DbSend, user_uuids: &[String], conn: &mut DbConn) {
let user_uuid = convert_option(send.user_uuid.clone());
let data = create_update(
@ -313,6 +341,9 @@ impl WebSocketUsers {
for uuid in user_uuids {
self.send_update(uuid, &data).await;
}
if CONFIG.push_enabled() && user_uuids.len() == 1 {
push_send_update(ut, send, conn).await;
}
}
}
@ -354,7 +385,7 @@ fn create_ping() -> Vec<u8> {
}
#[allow(dead_code)]
#[derive(Eq, PartialEq)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum UpdateType {
SyncCipherUpdate = 0,
SyncCipherCreate = 1,