Skip to content

Commit

Permalink
Addressed remarks given and some updates
Browse files Browse the repository at this point in the history
- Addressed comments given during review
- Updated crates, including Rocket to the latest merged v0.5 changes
- Removed an extra header which should not be sent for websocket connections
  • Loading branch information
BlackDex committed Feb 16, 2024
1 parent f44fec3 commit 917fb50
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
### WebSocket ###
#################

## Disable websocket notifications
# WEBSOCKET_DISABLED=false
## Enable websocket notifications
# ENABLE_WEBSOCKET=true

##########################
### Push notifications ###
Expand Down
4 changes: 2 additions & 2 deletions src/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ async fn test_smtp(data: Json<InviteData>, _token: AdminToken) -> EmptyResult {

#[get("/logout")]
fn logout(cookies: &CookieJar<'_>) -> Redirect {
cookies.remove(Cookie::build(COOKIE_NAME).path(admin_path()));
cookies.remove(Cookie::build(COOKIE_NAME, "").path(admin_path()).finish());
Redirect::to(admin_path())
}

Expand Down Expand Up @@ -797,7 +797,7 @@ impl<'r> FromRequest<'r> for AdminToken {

if decode_admin(access_token).is_err() {
// Remove admin cookie
cookies.remove(Cookie::build(COOKIE_NAME).path(admin_path()));
cookies.remove(Cookie::build(COOKIE_NAME, "").path(admin_path()).finish());
error!("Invalid or expired admin JWT. IP: {}.", &ip.ip);
return Outcome::Error((Status::Unauthorized, "Session expired"));
}
Expand Down
43 changes: 23 additions & 20 deletions src/api/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ use super::{
push_send_update, push_user_update,
};

static NOTIFICATIONS_DISABLED: Lazy<bool> = Lazy::new(|| !CONFIG.enable_websocket() && !CONFIG.push_enabled());

pub fn routes() -> Vec<Route> {
if CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
routes![websockets_hub, anonymous_websockets_hub]
} else {
info!("WebSocket are disabled, realtime sync functionality will not work!");
routes![]
} else {
routes![websockets_hub, anonymous_websockets_hub]
}
}

Expand Down Expand Up @@ -339,7 +341,7 @@ impl WebSocketUsers {
// NOTE: The last modified date needs to be updated before calling these methods
pub async fn send_user_update(&self, ut: UpdateType, user: &User) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let data = create_update(
Expand All @@ -348,7 +350,7 @@ impl WebSocketUsers {
None,
);

if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
self.send_update(&user.uuid, &data).await;
}

Expand All @@ -359,7 +361,7 @@ impl WebSocketUsers {

pub async fn send_logout(&self, user: &User, acting_device_uuid: Option<String>) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let data = create_update(
Expand All @@ -368,7 +370,7 @@ impl WebSocketUsers {
acting_device_uuid.clone(),
);

if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
self.send_update(&user.uuid, &data).await;
}

Expand All @@ -385,7 +387,7 @@ impl WebSocketUsers {
conn: &mut DbConn,
) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let data = create_update(
Expand All @@ -398,7 +400,7 @@ impl WebSocketUsers {
Some(acting_device_uuid.into()),
);

if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
self.send_update(&folder.user_uuid, &data).await;
}

Expand All @@ -417,7 +419,7 @@ impl WebSocketUsers {
conn: &mut DbConn,
) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let org_uuid = convert_option(cipher.organization_uuid.clone());
Expand Down Expand Up @@ -445,7 +447,7 @@ impl WebSocketUsers {
Some(acting_device_uuid.into()),
);

if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
for uuid in user_uuids {
self.send_update(uuid, &data).await;
}
Expand All @@ -465,7 +467,7 @@ impl WebSocketUsers {
conn: &mut DbConn,
) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let user_uuid = convert_option(send.user_uuid.clone());
Expand All @@ -480,7 +482,7 @@ impl WebSocketUsers {
None,
);

if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
for uuid in user_uuids {
self.send_update(uuid, &data).await;
}
Expand All @@ -498,15 +500,15 @@ impl WebSocketUsers {
conn: &mut DbConn,
) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let data = create_update(
vec![("Id".into(), auth_request_uuid.clone().into()), ("UserId".into(), user_uuid.clone().into())],
UpdateType::AuthRequest,
Some(acting_device_uuid.to_string()),
);
if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
self.send_update(user_uuid, &data).await;
}

Expand All @@ -523,15 +525,15 @@ impl WebSocketUsers {
conn: &mut DbConn,
) {
// Skip any processing if both WebSockets and Push are not active
if CONFIG.websocket_disabled() && !CONFIG.push_enabled() {
if *NOTIFICATIONS_DISABLED {
return;
}
let data = create_update(
vec![("Id".into(), auth_response_uuid.to_owned().into()), ("UserId".into(), user_uuid.clone().into())],
UpdateType::AuthRequestResponse,
approving_device_uuid.clone().into(),
);
if !CONFIG.websocket_disabled() {
if CONFIG.enable_websocket() {
self.send_update(auth_response_uuid, &data).await;
}

Expand All @@ -557,14 +559,15 @@ impl AnonymousWebSocketSubscriptions {
}

pub async fn send_auth_response(&self, user_uuid: &String, auth_response_uuid: &str) {
if !CONFIG.enable_websocket() {
return;
}
let data = create_anonymous_update(
vec![("Id".into(), auth_response_uuid.to_owned().into()), ("UserId".into(), user_uuid.clone().into())],
UpdateType::AuthRequestResponse,
user_uuid.to_string(),
);
if !CONFIG.websocket_disabled() {
self.send_update(auth_response_uuid, &data).await;
}
self.send_update(auth_response_uuid, &data).await;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ make_config! {
web_vault_folder: String, false, def, "web-vault/".to_string();
},
ws {
/// Disable websocket notifications
websocket_disabled: bool, false, def, false;
/// Enable websocket notifications
enable_websocket: bool, false, def, true;
},
push {
/// Enable push notifications
Expand Down

0 comments on commit 917fb50

Please sign in to comment.