User requested a checkpoint and stopped all agents. Deployed artifacts remain the last verified releases; unfinished settings/categories/forums are preserved as WIP. See TODO/roadmap for verification and remaining release/updater work.
219 lines
6.5 KiB
Rust
219 lines
6.5 KiB
Rust
//! Maps application events to notification sounds and plays them.
|
|
use std::sync::OnceLock;
|
|
|
|
use vnox_client::audio::sounds::{self, SoundEvent, SoundPlayer};
|
|
use vnox_client::net::NetEvent;
|
|
|
|
use crate::AppState;
|
|
use crate::ui::state::{ConnState, UiState, VoiceUiState};
|
|
|
|
fn player() -> &'static SoundPlayer {
|
|
static PLAYER: OnceLock<SoundPlayer> = OnceLock::new();
|
|
PLAYER.get_or_init(SoundPlayer::default)
|
|
}
|
|
|
|
/// Plays `event` if the sound settings and the deafen state allow it.
|
|
pub fn play(state: &AppState, event: SoundEvent) {
|
|
if let Some(event) = sounds::select(event, &state.ui.sounds, state.ui.deafen) {
|
|
player().play(
|
|
event,
|
|
state.ui.sounds.gain(),
|
|
state.audio.as_ref(),
|
|
&state.ui.output_device_id,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Plays the test sound from the settings page, ignoring the category switches but not the
|
|
/// master switch or volume.
|
|
pub fn preview(state: &AppState) {
|
|
let mut settings = state.ui.sounds;
|
|
settings.voice_room = true;
|
|
if let Some(event) = sounds::select(SoundEvent::PeerJoined, &settings, false) {
|
|
player().play(
|
|
event,
|
|
settings.gain(),
|
|
state.audio.as_ref(),
|
|
&state.ui.output_device_id,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// True when the person is looking at the text channel `channel_id`.
|
|
fn viewing_channel(ui: &UiState, channel_id: &str) -> bool {
|
|
!ui.showing_voice
|
|
&& !ui.browsing_dms
|
|
&& ui.active_dm_id.is_none()
|
|
&& ui.active_channel.as_deref() == Some(channel_id)
|
|
}
|
|
|
|
/// The sound a network event should make, before settings are applied.
|
|
pub fn sound_for_event(
|
|
ui: &UiState,
|
|
own_id: &str,
|
|
own_nickname: &str,
|
|
event: &NetEvent,
|
|
) -> Option<SoundEvent> {
|
|
match event {
|
|
NetEvent::Connected { .. } => Some(SoundEvent::Connected),
|
|
// Only a connection that was established can be lost; failed attempts stay quiet.
|
|
NetEvent::Disconnected { .. } if matches!(ui.conn, ConnState::Connected { .. }) => {
|
|
Some(SoundEvent::ConnectionLost)
|
|
}
|
|
NetEvent::Error { .. } => Some(SoundEvent::Error),
|
|
NetEvent::UserJoin {
|
|
channel_id,
|
|
user_id,
|
|
..
|
|
} if user_id != own_id && in_current_voice_room(ui, channel_id) => {
|
|
Some(SoundEvent::PeerJoined)
|
|
}
|
|
NetEvent::UserLeave {
|
|
channel_id,
|
|
user_id,
|
|
} if user_id != own_id && in_current_voice_room(ui, channel_id) => {
|
|
Some(SoundEvent::PeerLeft)
|
|
}
|
|
NetEvent::ChatMessage {
|
|
channel_id,
|
|
sender_id,
|
|
content,
|
|
..
|
|
} => sounds::message_event(
|
|
sender_id == own_id,
|
|
sounds::is_mention(content, own_nickname),
|
|
viewing_channel(ui, channel_id),
|
|
),
|
|
NetEvent::DmMessage {
|
|
dm_id, sender_id, ..
|
|
} => sounds::message_event(
|
|
sender_id == own_id,
|
|
false,
|
|
!ui.showing_voice && ui.active_dm_id.as_deref() == Some(dm_id.as_str()),
|
|
),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn in_current_voice_room(ui: &UiState, channel_id: &str) -> bool {
|
|
matches!(&ui.voice, VoiceUiState::Active { channel } if channel == channel_id)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn connected_ui() -> UiState {
|
|
UiState {
|
|
conn: ConnState::Connected {
|
|
session_id: "s".into(),
|
|
node_name: "n".into(),
|
|
},
|
|
active_channel: Some("general".into()),
|
|
..UiState::default()
|
|
}
|
|
}
|
|
|
|
fn chat(channel: &str, sender: &str, content: &str) -> NetEvent {
|
|
NetEvent::ChatMessage {
|
|
message_id: "m".into(),
|
|
channel_id: channel.into(),
|
|
sender_id: sender.into(),
|
|
nickname: sender.into(),
|
|
content: content.into(),
|
|
timestamp: 0,
|
|
reply_to: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn messages_sound_only_outside_the_viewed_channel() {
|
|
let ui = connected_ui();
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &chat("general", "bob", "hi")),
|
|
None
|
|
);
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &chat("random", "bob", "hi")),
|
|
Some(SoundEvent::Message)
|
|
);
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &chat("random", "me", "hi")),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn viewing_the_voice_page_does_not_count_as_viewing_the_channel() {
|
|
let ui = UiState {
|
|
showing_voice: true,
|
|
..connected_ui()
|
|
};
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &chat("general", "bob", "hi")),
|
|
Some(SoundEvent::Message)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn mentions_use_the_mention_sound_even_in_the_viewed_channel() {
|
|
let ui = connected_ui();
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "Alice", &chat("general", "bob", "hey @alice")),
|
|
Some(SoundEvent::Mention)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn peers_are_announced_only_in_the_voice_room_you_are_in() {
|
|
let ui = UiState {
|
|
voice: VoiceUiState::Active {
|
|
channel: "lounge".into(),
|
|
},
|
|
..connected_ui()
|
|
};
|
|
let join = |channel: &str, user: &str| NetEvent::UserJoin {
|
|
account_id: String::new(),
|
|
avatar_url: String::new(),
|
|
channel_id: channel.into(),
|
|
user_id: user.into(),
|
|
nickname: user.into(),
|
|
};
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &join("lounge", "bob")),
|
|
Some(SoundEvent::PeerJoined)
|
|
);
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &join("lounge", "me")),
|
|
None
|
|
);
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &join("general", "bob")),
|
|
None
|
|
);
|
|
let leave = NetEvent::UserLeave {
|
|
channel_id: "lounge".into(),
|
|
user_id: "bob".into(),
|
|
};
|
|
assert_eq!(
|
|
sound_for_event(&ui, "me", "me", &leave),
|
|
Some(SoundEvent::PeerLeft)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn only_an_established_connection_can_be_lost() {
|
|
let lost = NetEvent::Disconnected {
|
|
reason: "x".into(),
|
|
will_retry: true,
|
|
};
|
|
assert_eq!(
|
|
sound_for_event(&connected_ui(), "me", "me", &lost),
|
|
Some(SoundEvent::ConnectionLost)
|
|
);
|
|
assert_eq!(
|
|
sound_for_event(&UiState::default(), "me", "me", &lost),
|
|
None
|
|
);
|
|
}
|
|
}
|