Skip to content

Commit

Permalink
fix: remove notification permission prompt (#4302)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
betamos and lucasfernog committed Jun 9, 2022
1 parent a6f45d5 commit f482b09
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 170 deletions.
5 changes: 5 additions & 0 deletions .changes/permission-granted-return-val.md
@@ -0,0 +1,5 @@
---
"api": patch
---

The notification's `isPermissionGranted` function now returns `boolean` instead of `boolean | null`. The response is never `null` because we won't check the permission for now, always returning `true` instead.
5 changes: 5 additions & 0 deletions .changes/remove-notification-permission-prompt.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

No longer ask for permission to send notifications and always allow it.
5 changes: 5 additions & 0 deletions .changes/remove-settings-module.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

**Breaking change:** Removed the `settings` module.
1 change: 0 additions & 1 deletion core/tauri/Cargo.toml
Expand Up @@ -68,7 +68,6 @@ zip = { version = "0.6", default-features = false, optional = true }
ignore = "0.4"
flate2 = "1.0"
http = "0.2"
bincode = "1.3"
dirs-next = "2.0"
percent-encoding = "2.1"
base64 = { version = "0.13", optional = true }
Expand Down
3 changes: 0 additions & 3 deletions core/tauri/src/api/error.rs
Expand Up @@ -53,9 +53,6 @@ pub enum Error {
/// JSON error.
#[error(transparent)]
Json(#[from] serde_json::Error),
/// Bincode error.
#[error(transparent)]
Bincode(#[from] Box<bincode::ErrorKind>),
/// IO error.
#[error(transparent)]
Io(#[from] std::io::Error),
Expand Down
86 changes: 13 additions & 73 deletions core/tauri/src/endpoints/notification.rs
Expand Up @@ -13,7 +13,6 @@ use tauri_macros::{command_enum, module_command_handler, CommandModule};
use crate::{api::notification::Notification, Env, Manager};

// `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";
Expand Down Expand Up @@ -49,13 +48,6 @@ impl Cmd {
context: InvokeContext<R>,
options: NotificationOptions,
) -> super::Result<()> {
let allowed = match is_permission_granted(&context) {
Some(p) => p,
None => request_permission(&context),
};
if !allowed {
return Err(crate::Error::NotificationNotAllowed.into_anyhow());
}
let mut notification =
Notification::new(context.config.tauri.bundle.identifier.clone()).title(options.title);
if let Some(body) = options.body {
Expand All @@ -68,80 +60,23 @@ impl Cmd {
Ok(())
}

#[cfg(notification_all)]
fn request_notification_permission<R: Runtime>(
context: InvokeContext<R>,
) -> super::Result<&'static str> {
if request_permission(&context) {
Ok(PERMISSION_GRANTED)
} else {
Ok(PERMISSION_DENIED)
}
}

#[cfg(not(notification_all))]
fn request_notification_permission<R: Runtime>(
_context: InvokeContext<R>,
) -> super::Result<&'static str> {
Ok(PERMISSION_DENIED)
}

#[cfg(notification_all)]
fn is_notification_permission_granted<R: Runtime>(
context: InvokeContext<R>,
) -> super::Result<Option<bool>> {
if let Some(allow_notification) = is_permission_granted(&context) {
Ok(Some(allow_notification))
Ok(if cfg!(notification_all) {
PERMISSION_GRANTED
} else {
Ok(None)
}
PERMISSION_DENIED
})
}

#[cfg(not(notification_all))]
fn is_notification_permission_granted<R: Runtime>(
_context: InvokeContext<R>,
) -> super::Result<Option<bool>> {
Ok(Some(false))
) -> super::Result<bool> {
Ok(cfg!(notification_all))
}
}

#[cfg(notification_all)]
fn request_permission<R: Runtime>(context: &InvokeContext<R>) -> bool {
let mut settings = crate::settings::read_settings(
&context.config,
&context.package_info,
context.window.state::<Env>().inner(),
);
if let Some(allow_notification) = settings.allow_notification {
return allow_notification;
}
let answer = crate::api::dialog::blocking::ask(
Some(&context.window),
"Permissions",
"This app wants to show notifications. Do you allow?",
);

settings.allow_notification = Some(answer);
let _ = crate::settings::write_settings(
&context.config,
&context.package_info,
context.window.state::<Env>().inner(),
settings,
);

answer
}

#[cfg(notification_all)]
fn is_permission_granted<R: Runtime>(context: &InvokeContext<R>) -> Option<bool> {
crate::settings::read_settings(
&context.config,
&context.package_info,
context.window.state::<Env>().inner(),
)
.allow_notification
}

#[cfg(test)]
mod tests {
use super::NotificationOptions;
Expand All @@ -163,16 +98,21 @@ mod tests {
fn request_notification_permission() {
assert_eq!(
super::Cmd::request_notification_permission(crate::test::mock_invoke_context()).unwrap(),
super::PERMISSION_DENIED
if cfg!(notification_all) {
super::PERMISSION_GRANTED
} else {
super::PERMISSION_DENIED
}
)
}

#[cfg(not(notification_all))]
#[test]
fn is_notification_permission_granted() {
let expected = cfg!(notification_all);
assert_eq!(
super::Cmd::is_notification_permission_granted(crate::test::mock_invoke_context()).unwrap(),
Some(false)
expected,
);
}

Expand Down
1 change: 0 additions & 1 deletion core/tauri/src/lib.rs
Expand Up @@ -172,7 +172,6 @@ pub mod window;
use tauri_runtime as runtime;
/// The allowlist scopes.
pub mod scope;
pub mod settings;
mod state;
#[cfg(updater)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "updater")))]
Expand Down
81 changes: 0 additions & 81 deletions core/tauri/src/settings.rs

This file was deleted.

10 changes: 0 additions & 10 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tooling/api/src/notification.ts
Expand Up @@ -46,7 +46,7 @@ type Permission = 'granted' | 'denied' | 'default'
*
* @returns
*/
async function isPermissionGranted(): Promise<boolean | null> {
async function isPermissionGranted(): Promise<boolean> {
if (window.Notification.permission !== 'default') {
return Promise.resolve(window.Notification.permission === 'granted')
}
Expand Down

0 comments on commit f482b09

Please sign in to comment.