Skip to content

Commit

Permalink
feat: block win sound and CSP (tari-project#1497)
Browse files Browse the repository at this point in the history
Description
---
Fix bug introduced from merging tari-project#372 
Adds CSP for inline styling to allow rendering tower animation.
Also changes loading audio file to bundle it with FE assets from
public/assets instead of bundling it from BE and exposing for FE to
fetch it.

Some explanations regarding CSP:
- `"dangerousDisableAssetCspModification": ["style-src"],` and `
"style-src": "'self' 'unsafe-inline'",` are required to use CSS-in-JS
styled components to update components styling at runtime
- `"script-src": "'wasm-unsafe-eval'", and the `http://ipc.localhost
https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/dotlottie-player.wasm
https://unpkg.com/@lottiefiles/[email protected]/dist/dotlottie-player.wasm`
inside ` "connect-src"` are required by animation libraries
- `"default-src": "'self' asset: tauri: URL: blob: data:",` is the
fallback for non-defined entries and it's just allows to access local
assets, read them as a file and `"connect-src": "tauri: ipc:
http://ipc.localhost ..."` allows to use commands
- `"https:"` in `"default-src"` and `"connect-src"` is required by
Windows to load assets (looks like Windows doesn't support custom
protocols like `asset:` or `tuari:`)


```js
"security": {
    "dangerousDisableAssetCspModification": ["style-src"],
    "csp": {
        "default-src": "'self' asset: tauri: URL: blob: data: https:",
        "script-src": "'wasm-unsafe-eval'",
        "style-src": "'self' 'unsafe-inline'",
        "connect-src": "https: tauri: ipc: http://ipc.localhost https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/dotlottie-player.wasm https://unpkg.com/@lottiefiles/[email protected]/dist/dotlottie-player.wasm"
    },
},
```


Motivation and Context
---
Fix P0 bug and simplify handling of sound effect.

How Has This Been Tested?
---

What process can a PR reviewer use to test or verify this change?
---
Few things to test:

 - sound effect when winning block mine
 - sound effect when replay won block
 - disabling and enabling audio from general settings
- right click and toggle 'inspect' and check for any warning or error
logs coming from lack of any CSP.

All above should be done on release mode since previously this issues
didn't occur on dev build.


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced audio playback functionality with interactive feedback on
key events.
  - Added an audio toggle in settings for enabling or disabling sound.
- Expanded multi-language support with a new “audio enabled” label
across supported locales.

- **Chores**
  - Updated Linux build processes to include essential audio libraries.
- Improved configuration and security settings to enhance overall
performance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Brian Pearce <[email protected]>
  • Loading branch information
MCozhusheck and brianp authored Feb 11, 2025
1 parent d0ba475 commit da9a9ee
Show file tree
Hide file tree
Showing 28 changed files with 207 additions and 27 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
AIRDROP_BASE_URL: http://localhost:4000
AIRDROP_API_BASE_URL: http://localhost:3004
AIRDROP_TWITTER_AUTH_URL: http://localhost:3004/auth/twitter
TELEMETRY_API_URL: http://localhost:3004
TELEMETRY_API_URL: http://localhost:3004
run: |
cargo install cargo-lints
cargo lints clippy --all-targets --all-features
Expand Down Expand Up @@ -111,6 +111,13 @@ jobs:
opencl-headers \
ocl-icd-opencl-dev
- name: Install Dependencies - Linux/GStreamer (for audio playback)
run: |
sudo apt-get install --no-install-recommends --assume-yes \
libunwind-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 \
gstreamer1.0-qt5 gstreamer1.0-pulseaudio gstreamer1.0-fdkaac patchelf
- uses: actions/setup-node@v4
with:
node-version: lts/*
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ jobs:
opencl-headers \
ocl-icd-opencl-dev
- name: Install Dependencies - Linux/GStreamer (for audio playback)
if: startsWith(runner.os,'Linux')
run: |
sudo apt-get install --no-install-recommends --assume-yes \
libunwind-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good \
gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 \
gstreamer1.0-qt5 gstreamer1.0-pulseaudio gstreamer1.0-fdkaac patchelf
- name: Install Dependencies - macOS
if: startsWith(runner.os,'macOS')
run: |
Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@
<body>
<main id="main">
<div id="root"></div>

<audio id="player" preload="auto">
<source src="/assets/block_win.mp3" type="audio/mpeg" />
</audio>
<canvas id="canvas" style="opacity: 0"></canvas>
<script type="module" src="src/main.tsx"></script>
<script>
Expand Down
Binary file added public/assets/block_win.mp3
Binary file not shown.
1 change: 1 addition & 0 deletions public/locales/af/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "App herbegin benodig",
"application-info": "Aansoek Inligting",
"applyInviteCode": "Pas Uitnodigingskode Toe",
"audio-enabled": "Audio enabled",
"cancel": "Cancel",
"change-language": "Verander taal",
"confirm": "Bevestig",
Expand Down
1 change: 1 addition & 0 deletions public/locales/cn/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "需要重启应用",
"application-info": "应用信息",
"applyInviteCode": "应用邀请代码",
"audio-enabled": "Audio enabled",
"cancel": "取消",
"change-language": "更改语言",
"confirm": "确认",
Expand Down
1 change: 1 addition & 0 deletions public/locales/de/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "App-Neustart erforderlich",
"application-info": "Anwendungsinformationen",
"applyInviteCode": "Einladungscode anwenden",
"audio-enabled": "Audio enabled",
"cancel": "Abbrechen",
"change-language": "Sprache",
"confirm": "Bestätigen",
Expand Down
1 change: 1 addition & 0 deletions public/locales/en/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "App restart required",
"application-info": "Application Information",
"applyInviteCode": "Apply Invite Code",
"audio-enabled": "Audio enabled",
"cancel": "Cancel",
"change-language": "Language",
"confirm": "Confirm",
Expand Down
1 change: 1 addition & 0 deletions public/locales/fr/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "Redémarrage de l\"application requis",
"application-info": "Informations sur l\"application",
"applyInviteCode": "Appliquer le code d\"invitation",
"audio-enabled": "Audio enabled",
"cancel": "Cancel",
"change-language": "Langue",
"confirm": "Confirmer",
Expand Down
1 change: 1 addition & 0 deletions public/locales/hi/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "ऐप पुनः प्रारंभ आवश्यक है",
"application-info": "एप्लिकेशन जानकारी",
"applyInviteCode": "आमंत्रण कोड लागू करें",
"audio-enabled": "Audio enabled",
"cancel": "रद्द करें",
"change-language": "भाषा",
"confirm": "पुष्टि करें",
Expand Down
1 change: 1 addition & 0 deletions public/locales/id/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "Diperlukan restart aplikasi",
"application-info": "Informasi Aplikasi",
"applyInviteCode": "Terapkan Kode Undangan",
"audio-enabled": "Audio enabled",
"cancel": "Batal",
"change-language": "Bahasa",
"confirm": "Konfirmasi",
Expand Down
1 change: 1 addition & 0 deletions public/locales/ja/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "アプリの再起動が必要です",
"application-info": "アプリケーション情報",
"applyInviteCode": "招待コードを適用",
"audio-enabled": "Audio enabled",
"cancel": "キャンセル",
"change-language": "言語",
"confirm": "確認",
Expand Down
1 change: 1 addition & 0 deletions public/locales/ko/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "앱 재시작 필요",
"application-info": "애플리케이션 정보",
"applyInviteCode": "초대 코드 적용",
"audio-enabled": "Audio enabled",
"cancel": "취소",
"change-language": "언어",
"confirm": "확인",
Expand Down
1 change: 1 addition & 0 deletions public/locales/pl/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "Wymagany restart aplikacji",
"application-info": "Informacje o aplikacji",
"applyInviteCode": "Zastosuj Kod Zaproszenia",
"audio-enabled": "Audio enabled",
"cancel": "Anuluj",
"change-language": "Zmień język",
"confirm": "Potwierdź",
Expand Down
1 change: 1 addition & 0 deletions public/locales/ru/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "Требуется перезапуск приложения",
"application-info": "Информация о приложении",
"applyInviteCode": "Применить код приглашения",
"audio-enabled": "Audio enabled",
"cancel": "Отмена",
"change-language": "Язык",
"confirm": "Подтвердить",
Expand Down
1 change: 1 addition & 0 deletions public/locales/tr/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"app-restart-required": "Uygulama yeniden başlatılması gerekiyor",
"application-info": "Uygulama Bilgileri",
"applyInviteCode": "Davet Kodunu Uygula",
"audio-enabled": "Audio enabled",
"cancel": "İptal",
"change-language": "Dil Değiştir",
"confirm": "Onayla",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "default",
"windows": ["main", "splashscreen"],
"windows": ["main"],
"permissions": [
"core:path:default",
"core:event:default",
Expand Down
37 changes: 23 additions & 14 deletions src-tauri/capabilities/desktop.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{
"identifier": "desktop-capability",
"platforms": [
"macOS",
"windows",
"linux"
],
"windows": [
"main",
"splashscreen"
],
"permissions": [
"updater:default"
]
}
"identifier": "desktop-capability",
"platforms": ["macOS", "windows", "linux"],
"windows": ["main"],
"permissions": [
"updater:default",
"core:path:default",
"core:event:default",
"core:window:default",
"core:app:default",
"core:resources:default",
"core:menu:default",
"core:tray:default",
"core:window:allow-close",
"core:window:allow-destroy",
"core:window:allow-minimize",
"core:window:allow-unminimize",
"shell:allow-open",
"sentry:default",
"process:allow-restart",
"process:default",
"clipboard-manager:allow-write-text"
]
}
17 changes: 17 additions & 0 deletions src-tauri/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub struct AppConfigFromFile {
last_changelog_version: String,
#[serde(default)]
airdrop_tokens: Option<AirdropTokens>,
#[serde(default = "default_true")]
audio_enabled: bool,
}

impl Default for AppConfigFromFile {
Expand Down Expand Up @@ -161,6 +163,7 @@ impl Default for AppConfigFromFile {
pre_release: false,
last_changelog_version: default_changelog_version(),
airdrop_tokens: None,
audio_enabled: true,
}
}
}
Expand Down Expand Up @@ -281,6 +284,7 @@ pub(crate) struct AppConfig {
pre_release: bool,
last_changelog_version: String,
airdrop_tokens: Option<AirdropTokens>,
audio_enabled: bool,
}

impl AppConfig {
Expand Down Expand Up @@ -327,6 +331,7 @@ impl AppConfig {
pre_release: false,
last_changelog_version: default_changelog_version(),
airdrop_tokens: None,
audio_enabled: true,
}
}

Expand Down Expand Up @@ -406,6 +411,7 @@ impl AppConfig {
self.pre_release = config.pre_release;
self.last_changelog_version = config.last_changelog_version;
self.airdrop_tokens = config.airdrop_tokens;
self.audio_enabled = config.audio_enabled;

KEYRING_ACCESSED.store(
config.keyring_accessed,
Expand Down Expand Up @@ -542,6 +548,16 @@ impl AppConfig {
Ok(())
}

pub fn audio_enabled(&self) -> bool {
self.audio_enabled
}

pub async fn set_audio_enabled(&mut self, audio_enabled: bool) -> Result<(), anyhow::Error> {
self.audio_enabled = audio_enabled;
self.update_config_file().await?;
Ok(())
}

pub async fn set_max_gpu_usage(
&mut self,
custom_max_gpu_usage: Vec<GpuThreads>,
Expand Down Expand Up @@ -833,6 +849,7 @@ impl AppConfig {
pre_release: self.pre_release,
last_changelog_version: self.last_changelog_version.clone(),
airdrop_tokens: self.airdrop_tokens.clone(),
audio_enabled: self.audio_enabled,
};
let config = serde_json::to_string(config)?;
debug!(target: LOG_TARGET, "Updating config file: {:?} {:?}", file, self.clone());
Expand Down
34 changes: 33 additions & 1 deletion src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use log::{debug, error, info, warn};
use monero_address_creator::Seed as MoneroSeed;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{json, Value};
use std::fmt::Debug;
use std::fs::{read_dir, remove_dir_all, remove_file, File};
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -1381,6 +1381,38 @@ pub async fn set_airdrop_tokens<'r>(
Ok(())
}

#[tauri::command]
pub async fn get_audio_enabled(state: tauri::State<'_, UniverseAppState>) -> Result<bool, String> {
let enabled = state.config.read().await.audio_enabled();
Ok(enabled)
}

#[tauri::command]
pub async fn set_audio_enabled(
audio_enabled: bool,
state: tauri::State<'_, UniverseAppState>,
) -> Result<(), String> {
state
.config
.write()
.await
.set_audio_enabled(audio_enabled)
.await
.map_err(|e| e.to_string())?;
let telemetry_service = state.telemetry_service.read().await;
telemetry_service
.send("audio-enabled".to_string(), json!(audio_enabled))
.await
.inspect_err(|e| {
error!(
"error at sending telemetry data for get_audio_enabled {:?}",
e
)
})
.map_err(|e| e.to_string())?;
Ok(())
}

#[allow(clippy::too_many_lines)]
#[tauri::command]
pub async fn start_mining<'r>(
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,8 @@ fn main() {
commands::sign_ws_data,
commands::set_airdrop_tokens,
commands::get_airdrop_tokens,
commands::get_audio_enabled,
commands::set_audio_enabled,
commands::frontend_ready
])
.build(tauri::generate_context!())
Expand Down
25 changes: 15 additions & 10 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"bundle": {
"active": true,
"targets": "all",
"linux": {
"appimage": {
"bundleMediaFramework": true
}
},
"macOS": {
"providerShortName": "Tari Labs, LLC"
},
Expand All @@ -35,12 +40,14 @@
},
"app": {
"security": {
"capabilities": [
"desktop-capability",
"default",
"migrated"
],
"csp": null,
"capabilities": ["desktop-capability", "default", "migrated"],
"dangerousDisableAssetCspModification": ["style-src"],
"csp": {
"default-src": "'self' asset: tauri: URL: blob: data: https:",
"script-src": "'wasm-unsafe-eval'",
"style-src": "'self' 'unsafe-inline'",
"connect-src": "https: tauri: ipc: http://ipc.localhost https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/dotlottie-player.wasm https://unpkg.com/@lottiefiles/[email protected]/dist/dotlottie-player.wasm"
},
"pattern": {
"use": "isolation",
"options": {
Expand Down Expand Up @@ -75,9 +82,7 @@
"plugins": {
"updater": {
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEYxNUJBOEFEQkQ4RjJBMjYKUldRbUtvKzlyYWhiOFJIUmFFditENVV3d3hRbjNlZm1DMi9aMjluRUpVdHhQTytadTV3ODN3bUMK",
"endpoints": [
"https://raw.githubusercontent.com/tari-project/universe/main/.updater/alpha-latest.json"
],
"endpoints": ["https://raw.githubusercontent.com/tari-project/universe/main/.updater/alpha-latest.json"],
"windows": {
"installMode": "passive"
}
Expand All @@ -92,4 +97,4 @@
]
}
}
}
}
Loading

0 comments on commit da9a9ee

Please sign in to comment.