Skip to content

Commit

Permalink
fix(core): notification permission check when !allowlisted, closes #1666
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 2, 2021
1 parent 8b6f3de commit 8941790
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changes/notification-permission.md
@@ -0,0 +1,6 @@
---
"tauri": patch
---

`Notification.requestPermission()` now returns `"denied"` when not allowlisted.
`IsNotificationPermissionGranted` returns `false` when not allowlisted.
22 changes: 15 additions & 7 deletions core/tauri/src/endpoints/notification.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}
}
Expand Down Expand Up @@ -81,10 +87,12 @@ pub fn is_permission_granted() -> crate::Result<InvokeResponse> {
#[cfg(notification_all)]
pub fn request_permission() -> crate::Result<String> {
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",
Expand All @@ -94,12 +102,12 @@ pub fn request_permission() -> crate::Result<String> {
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())
}
}
}

0 comments on commit 8941790

Please sign in to comment.