From 8941790f98206adce441d7bdc42374af39edc786 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 2 May 2021 18:33:57 -0300 Subject: [PATCH] fix(core): notification permission check when !allowlisted, closes #1666 (#1677) --- .changes/notification-permission.md | 6 ++++++ core/tauri/src/endpoints/notification.rs | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 .changes/notification-permission.md diff --git a/.changes/notification-permission.md b/.changes/notification-permission.md new file mode 100644 index 000000000..6eae571a4 --- /dev/null +++ b/.changes/notification-permission.md @@ -0,0 +1,6 @@ +--- +"tauri": patch +--- + +`Notification.requestPermission()` now returns `"denied"` when not allowlisted. +`IsNotificationPermissionGranted` returns `false` when not allowlisted. diff --git a/core/tauri/src/endpoints/notification.rs b/core/tauri/src/endpoints/notification.rs index 819a07d90..2ff842d0d 100644 --- a/core/tauri/src/endpoints/notification.rs +++ b/core/tauri/src/endpoints/notification.rs @@ -8,6 +8,12 @@ use serde::Deserialize; #[cfg(notification_all)] use crate::api::notification::Notification; +// `Granted` response from `request_permission`. Matches the Web API return value. +#[cfg(notification_all)] +const PERMISSION_GRANTED: &str = "granted"; +// `Denied` response from `request_permission`. Matches the Web API return value. +const PERMISSION_DENIED: &str = "denied"; + /// The options for the notification API. #[derive(Deserialize)] pub struct NotificationOptions { @@ -43,13 +49,13 @@ impl Cmd { #[cfg(notification_all)] return is_permission_granted().map(Into::into); #[cfg(not(notification_all))] - Err(crate::Error::ApiNotAllowlisted("notification".to_string())) + Ok(false.into()) } Self::RequestNotificationPermission => { #[cfg(notification_all)] return request_permission().map(Into::into); #[cfg(not(notification_all))] - Err(crate::Error::ApiNotAllowlisted("notification".to_string())) + Ok(PERMISSION_DENIED.into()) } } } @@ -81,10 +87,12 @@ pub fn is_permission_granted() -> crate::Result { #[cfg(notification_all)] pub fn request_permission() -> crate::Result { let mut settings = crate::settings::read_settings()?; - let granted = "granted".to_string(); - let denied = "denied".to_string(); if let Some(allow_notification) = settings.allow_notification { - return Ok(if allow_notification { granted } else { denied }); + return Ok(if allow_notification { + PERMISSION_GRANTED.to_string() + } else { + PERMISSION_DENIED.to_string() + }); } let answer = crate::api::dialog::ask( "Permissions", @@ -94,12 +102,12 @@ pub fn request_permission() -> crate::Result { crate::api::dialog::AskResponse::Yes => { settings.allow_notification = Some(true); crate::settings::write_settings(settings)?; - Ok(granted) + Ok(PERMISSION_GRANTED.to_string()) } crate::api::dialog::AskResponse::No => { settings.allow_notification = Some(false); crate::settings::write_settings(settings)?; - Ok(denied) + Ok(PERMISSION_DENIED.to_string()) } } }