浏览代码

fix: remove notification permission prompt (#4302)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Didrik Nordström 3 年之前
父节点
当前提交
f482b09422

+ 5 - 0
.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 - 0
.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 - 0
.changes/remove-settings-module.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+**Breaking change:** Removed the `settings` module.

+ 0 - 1
core/tauri/Cargo.toml

@@ -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 }

+ 0 - 3
core/tauri/src/api/error.rs

@@ -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),

+ 13 - 73
core/tauri/src/endpoints/notification.rs

@@ -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";
@@ -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 {
@@ -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;
@@ -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,
     );
   }
 

+ 0 - 1
core/tauri/src/lib.rs

@@ -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")))]

+ 0 - 81
core/tauri/src/settings.rs

@@ -1,81 +0,0 @@
-// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-License-Identifier: MIT
-
-//! The Tauri-specific settings for your application.
-//!
-//! This only contains notification permission status for now, but is able to expand in the future.
-
-use crate::{
-  api::{
-    file::read_binary,
-    path::{resolve_path, BaseDirectory},
-  },
-  Config, Env, PackageInfo,
-};
-use serde::{Deserialize, Serialize};
-use std::{
-  fs::File,
-  io::Write,
-  path::{Path, PathBuf},
-};
-
-/// The Tauri Settings.
-#[derive(Debug, Clone, Default, Deserialize, Serialize)]
-#[non_exhaustive]
-pub struct Settings {
-  /// Whether the user allows notifications or not.
-  #[cfg(notification_all)]
-  pub allow_notification: Option<bool>,
-}
-
-/// Gets the path to the settings file.
-fn get_settings_path(
-  config: &Config,
-  package_info: &PackageInfo,
-  env: &Env,
-) -> crate::api::Result<PathBuf> {
-  resolve_path(
-    config,
-    package_info,
-    env,
-    ".tauri-settings",
-    Some(BaseDirectory::App),
-  )
-}
-
-/// Write the settings to the file system.
-#[allow(dead_code)]
-pub(crate) fn write_settings(
-  config: &Config,
-  package_info: &PackageInfo,
-  env: &Env,
-  settings: Settings,
-) -> crate::Result<()> {
-  let settings_path = get_settings_path(config, package_info, env)?;
-  let settings_folder = Path::new(&settings_path).parent().unwrap();
-  if !settings_folder.exists() {
-    std::fs::create_dir(settings_folder)?;
-  }
-  File::create(settings_path)
-    .map_err(Into::into)
-    .and_then(|mut f| {
-      f.write_all(&bincode::serialize(&settings).map_err(crate::api::Error::Bincode)?)
-        .map_err(Into::into)
-    })
-}
-
-/// Reads the settings from the file system.
-pub fn read_settings(config: &Config, package_info: &PackageInfo, env: &Env) -> Settings {
-  if let Ok(settings_path) = get_settings_path(config, package_info, env) {
-    if settings_path.exists() {
-      read_binary(settings_path)
-        .and_then(|settings| bincode::deserialize(&settings).map_err(Into::into))
-        .unwrap_or_default()
-    } else {
-      Settings::default()
-    }
-  } else {
-    Settings::default()
-  }
-}

+ 0 - 10
examples/api/src-tauri/Cargo.lock

@@ -172,15 +172,6 @@ version = "0.13.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
 
-[[package]]
-name = "bincode"
-version = "1.3.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
-dependencies = [
- "serde",
-]
-
 [[package]]
 name = "bitflags"
 version = "1.3.2"
@@ -3052,7 +3043,6 @@ dependencies = [
  "anyhow",
  "attohttpc",
  "base64",
- "bincode",
  "bytes",
  "clap",
  "cocoa",

+ 1 - 1
tooling/api/src/notification.ts

@@ -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')
   }