ui: TeamSpeak links open the server dialog, or move you in voice
Links to ts3server://, teamspeak:// and tmspk.gg in chat were only copied. voelin_model::Link reads them, and invite_link now writes through ServerLink::to_url, so a copied invite reads back the same (a '/' in a channel's name stays %2F in the link, \/ in the path). App::open_link (links.rs) finds the saved server with that address (vm::servers::same_address; of several, the one in voice, else the one shown: link_server). In voice on it, it moves into the link's channel (channel_by_path) through join_channel, so a locked channel asks for its password when the link's is missing or wrong. Otherwise the server dialog opens filled in by link_form, over the saved server's form when there is one: its nickname stays. Nothing connects before Connect. Invite codes need a TeamSpeak service; a toast says so. VOELIN_OPEN=link:<url> goes through inbox::Request::OpenLink, where the platform's links will come in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AVRENKftQqGoEeswiR9TDQ
This commit is contained in:
1 parent
7965187d2c
commit
65ff5f6693
15 files changed
+843
-96
No files matched your search
@@ -62,7 +62,7 @@ pub(super) fn wire(ui: &MainWindow) {
|
||||
with_app(|app| app.create_topic(key, title.to_string()));
|
||||
});
|
||||
bridge.on_open_link(|link, masked| {
|
||||
with_app(|app| app.open_link_text(&link, masked)).unwrap_or_default().into()
|
||||
with_app(|app| app.open_link_text(&link, masked));
|
||||
});
|
||||
bridge.on_quote_text(|author, text| crate::vm::chat::quote(&author, &text).into());
|
||||
bridge.on_open_member(|id| {
|
||||
|
||||
@@ -624,9 +624,8 @@ impl App {
|
||||
/// A link in a message was clicked (`masked`: its text shows something
|
||||
/// else, [`vm::bbcode::is_masked`]). A page on the web opens in the
|
||||
/// browser, a masked one once the user saw where it goes (Nav.link-open);
|
||||
/// a TeamSpeak link is copied until Voelin opens them. Returns what to
|
||||
/// copy ("": nothing).
|
||||
pub(crate) fn open_link_text(&mut self, link: &str, masked: bool) -> String {
|
||||
/// a TeamSpeak link in Voelin ([`App::open_link`]).
|
||||
pub(crate) fn open_link_text(&mut self, link: &str, masked: bool) {
|
||||
match vm::bbcode::classify_link(link) {
|
||||
LinkKind::Web(url) if masked => {
|
||||
if let Some(ui) = self.ui.upgrade() {
|
||||
@@ -635,20 +634,15 @@ impl App {
|
||||
nav.set_link_url(url.into());
|
||||
nav.set_link_open(true);
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
LinkKind::Web(url) => {
|
||||
if let Err(e) = self.open_url(&url) {
|
||||
self.set_status(format!("Cannot open the link: {e}"));
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
LinkKind::Server(url) => {
|
||||
self.copy_note = Some("TeamSpeak links open in Voelin soon; copied".to_owned());
|
||||
url
|
||||
}
|
||||
LinkKind::Server(url) => self.open_link(&url),
|
||||
// A file opens from its card.
|
||||
LinkKind::File | LinkKind::Refused => String::new(),
|
||||
LinkKind::File | LinkKind::Refused => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@
|
||||
//! the "New" divider, scrolled up to; `unread:end` ten messages before,
|
||||
//! at the list's end, under the bar that counts the new messages),
|
||||
//! `link-confirm[:<url>]` (the question before a masked link opens, by
|
||||
//! default the sample's raid board), `watch` (the first stream; with
|
||||
//! default the sample's raid board), `link:<url>` (a TeamSpeak link
|
||||
//! opened: the server dialog filled in from it, or in voice on its
|
||||
//! server a move into its channel), `watch` (the first stream; with
|
||||
//! sample data the local test pattern in its place), `popout` (the same,
|
||||
//! popped out),
|
||||
//! `tab:<home|servers|chat|activity|you>` (phone layout); `friends[:<uid>]`,
|
||||
@@ -307,6 +309,8 @@ pub(crate) fn start(ui: &MainWindow, switches: &Switches) -> Running {
|
||||
let url = if arg.is_empty() { DEMO_RAID_BOARD } else { arg };
|
||||
with_app(|app| app.open_link_text(url, true));
|
||||
}
|
||||
// As a link from the platform comes.
|
||||
"link" => crate::inbox::request(crate::inbox::Request::OpenLink(arg.to_owned())),
|
||||
"watch" | "popout" => {
|
||||
with_app(|app| {
|
||||
if switches.demo_ui { app.demo_watch() } else { app.watch_first_stream() }
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Requests from the platform around the window: on Android a tapped
|
||||
//! notification or something shared to the app ("Share to Voelin"). They
|
||||
//! can come from any thread and before the window runs; [`request`] keeps
|
||||
//! them until the window takes them on its thread.
|
||||
//! notification or something shared to the app ("Share to Voelin"), and a
|
||||
//! TeamSpeak link to open. They can come from any thread and before the
|
||||
//! window runs; [`request`] keeps them until the window takes them on its
|
||||
//! thread.
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
@@ -21,6 +22,9 @@ pub enum Request {
|
||||
/// should not post into a chat), the files are uploaded to the current
|
||||
/// channel and linked there, as the composer's attach button does.
|
||||
Share { text: Option<String>, files: Vec<PathBuf> },
|
||||
/// Open a TeamSpeak link (`ts3server://`, `teamspeak://`, `tmspk.gg`):
|
||||
/// the server dialog filled in from it, or a move in voice.
|
||||
OpenLink(String),
|
||||
}
|
||||
|
||||
static PENDING: Mutex<Vec<Request>> = Mutex::new(Vec::new());
|
||||
@@ -52,6 +56,9 @@ pub(crate) fn take() {
|
||||
nav.invoke_show(Page::Server);
|
||||
with_app(|app| app.share(&nav, text, files));
|
||||
}
|
||||
Request::OpenLink(url) => {
|
||||
with_app(|app| app.open_link(&url));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ mod hotkey;
|
||||
mod identities;
|
||||
mod images;
|
||||
mod inbox;
|
||||
mod links;
|
||||
mod members;
|
||||
mod messages;
|
||||
#[cfg(not(target_os = "android"))]
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
//! TeamSpeak links opened in the app (`ts3server://`, `teamspeak://` and
|
||||
//! `tmspk.gg`), from a message or the platform.
|
||||
|
||||
use slint::ComponentHandle;
|
||||
use voelin_core::VoiceState;
|
||||
use voelin_model::{Link, ServerLink};
|
||||
|
||||
use crate::app::{App, Nav};
|
||||
use crate::vm;
|
||||
|
||||
impl App {
|
||||
/// Open a TeamSpeak link. In voice on its server: show it and move into
|
||||
/// the link's channel (a locked one asks for its password if the link
|
||||
/// has none or a wrong one). Otherwise the server dialog, filled in from
|
||||
/// the link (the saved server's when there is one): nothing connects
|
||||
/// before its Connect.
|
||||
pub(crate) fn open_link(&mut self, url: &str) {
|
||||
let link = match Link::parse(url) {
|
||||
Some(Link::Server(link)) => link,
|
||||
Some(Link::Invite(_)) => {
|
||||
self.set_status(
|
||||
"TeamSpeak invite codes (tmspk.gg/…) can't be opened yet; ask for the server address",
|
||||
);
|
||||
return;
|
||||
}
|
||||
None => {
|
||||
self.set_status("Not a TeamSpeak server link");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let in_voice = |id: i64| {
|
||||
self.sessions.get(&id).is_some_and(|v| v.state.voice == VoiceState::Connected)
|
||||
};
|
||||
match vm::servers::link_server(&self.bookmarks, &link.address(), self.current, in_voice) {
|
||||
Some((id, true)) => self.move_by_link(id, &link),
|
||||
saved => self.open_link_form(saved.map(|(id, _)| id), &link),
|
||||
}
|
||||
}
|
||||
|
||||
/// In voice on the link's server: show it, and move into its channel.
|
||||
fn move_by_link(&mut self, session: i64, link: &ServerLink) {
|
||||
self.show_server(session);
|
||||
let Some(path) = &link.channel else { return };
|
||||
let Some(view) = self.sessions.get_mut(&session) else { return };
|
||||
let Some(channel) = vm::servers::channel_by_path(&view.presence, path) else {
|
||||
let server = self.bookmark(session).map(|b| b.name.clone()).unwrap_or_default();
|
||||
self.set_status(format!("{server} has no channel \u{201c}{path}\u{201d}"));
|
||||
return;
|
||||
};
|
||||
if view.state.own_channel == Some(channel) {
|
||||
return;
|
||||
}
|
||||
// As if typed into the password dialog: a wrong one asks again.
|
||||
if let Some(password) = &link.channel_password {
|
||||
view.channel_passwords.insert(channel, password.clone());
|
||||
}
|
||||
self.join_channel(session, channel);
|
||||
}
|
||||
|
||||
/// The server dialog, filled in from the link over the saved server's
|
||||
/// form, or a new server's.
|
||||
fn open_link_form(&mut self, saved: Option<i64>, link: &ServerLink) {
|
||||
let form = self.bookmark_form(saved.unwrap_or(-1));
|
||||
let form = vm::servers::link_form(link, form, &self.default_nickname());
|
||||
let Some(ui) = self.ui.upgrade() else { return };
|
||||
let nav = ui.global::<Nav>();
|
||||
nav.set_bookmark_form(form);
|
||||
nav.set_bookmark_open(true);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use slint::SharedString;
|
||||
use voelin_core::{ObserveState, SessionState, VoiceState};
|
||||
use voelin_model::{ChannelId, ChannelInfo};
|
||||
use voelin_model::{ChannelId, ChannelInfo, Presence, ServerLink};
|
||||
use voelin_store::Bookmark;
|
||||
|
||||
use crate::app::{BookmarkForm, ServerItem};
|
||||
@@ -107,43 +107,88 @@ pub fn connect_to(form: &BookmarkForm, saved: Option<&Bookmark>) -> Option<Conne
|
||||
/// the top) on the server at `address` (`host`, `host:port` or
|
||||
/// `[v6]:port`), as TeamSpeak clients open them.
|
||||
pub fn invite_link(address: &str, path: &[&str]) -> String {
|
||||
fn encode(text: &str) -> String {
|
||||
text.bytes()
|
||||
.map(|b| match b {
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
|
||||
char::from(b).to_string()
|
||||
}
|
||||
_ => format!("%{b:02X}"),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
let address = address.trim();
|
||||
// A port follows the last colon, unless that colon is inside an IPv6
|
||||
// address without brackets.
|
||||
let (host, port) = match address.rsplit_once(':') {
|
||||
Some((host, port))
|
||||
if !port.is_empty()
|
||||
&& port.bytes().all(|b| b.is_ascii_digit())
|
||||
&& (!host.contains(':') || host.ends_with(']')) =>
|
||||
{
|
||||
(host, Some(port))
|
||||
}
|
||||
_ => (address, None),
|
||||
let mut link = ServerLink::new(address);
|
||||
link.channel = (!path.is_empty()).then(|| voelin_model::join_channel_path(path));
|
||||
link.to_url()
|
||||
}
|
||||
|
||||
/// TeamSpeak's voice port, of an address without one.
|
||||
const DEFAULT_PORT: u16 = 9987;
|
||||
|
||||
/// Whether two addresses name the same server: the host in any case, an
|
||||
/// IPv6 address with or without its brackets, no port as 9987 (a port a
|
||||
/// DNS record gives is not looked up).
|
||||
pub fn same_address(a: &str, b: &str) -> bool {
|
||||
let key = |address: &str| {
|
||||
let link = ServerLink::new(address);
|
||||
(link.host.trim_end_matches('.').to_lowercase(), link.port.unwrap_or(DEFAULT_PORT))
|
||||
};
|
||||
let mut link = format!("ts3server://{host}");
|
||||
let mut query = Vec::new();
|
||||
if let Some(port) = port {
|
||||
query.push(format!("port={port}"));
|
||||
let a = key(a);
|
||||
!a.0.is_empty() && a == key(b)
|
||||
}
|
||||
|
||||
/// The saved server a link to `address` opens, and whether it has voice
|
||||
/// (`in_voice`): of those at that address ([`same_address`]; two can be,
|
||||
/// with other identities), the one in voice, else the one shown
|
||||
/// (`current`), else the first.
|
||||
pub fn link_server(
|
||||
bookmarks: &[Bookmark],
|
||||
address: &str,
|
||||
current: Option<i64>,
|
||||
in_voice: impl Fn(i64) -> bool,
|
||||
) -> Option<(i64, bool)> {
|
||||
let saved: Vec<i64> =
|
||||
bookmarks.iter().filter(|b| same_address(&b.address, address)).map(|b| b.id).collect();
|
||||
if let Some(id) = saved.iter().copied().find(|id| in_voice(*id)) {
|
||||
return Some((id, true));
|
||||
}
|
||||
if !path.is_empty() {
|
||||
let names: Vec<String> = path.iter().map(|n| encode(n)).collect();
|
||||
query.push(format!("channel={}", names.join("/")));
|
||||
let shown = saved.iter().copied().find(|id| Some(*id) == current);
|
||||
shown.or_else(|| saved.first().copied()).map(|id| (id, false))
|
||||
}
|
||||
|
||||
/// The channel a link's path names (`/<id>`, or names from the top as the
|
||||
/// server has them, [`voelin_model::split_channel_path`]). Of siblings with
|
||||
/// the same name, the first in the tree's order that has the rest of the
|
||||
/// path.
|
||||
pub fn channel_by_path(presence: &Presence, path: &str) -> Option<ChannelId> {
|
||||
fn below(presence: &Presence, parent: ChannelId, names: &[String]) -> Option<ChannelId> {
|
||||
let (name, rest) = names.split_first()?;
|
||||
let siblings = presence.channels.values().filter(|c| c.parent == parent).collect();
|
||||
voelin_model::order_siblings(siblings)
|
||||
.into_iter()
|
||||
.filter(|c| c.name == *name)
|
||||
.find_map(|c| if rest.is_empty() { Some(c.id) } else { below(presence, c.id, rest) })
|
||||
}
|
||||
if !query.is_empty() {
|
||||
link.push('?');
|
||||
link.push_str(&query.join("&"));
|
||||
match voelin_model::channel_path_id(path) {
|
||||
Some(id) => presence.channels.contains_key(&id).then_some(id),
|
||||
None => below(presence, 0, &voelin_model::split_channel_path(path)),
|
||||
}
|
||||
link
|
||||
}
|
||||
|
||||
/// The server dialog's form for a link, over `form`: a saved server's
|
||||
/// keeps its address, name and nickname (a link never changes it), and its
|
||||
/// stored password unless the link has one; a new server's takes the
|
||||
/// link's address, nickname (else `default_nickname`) and password. The
|
||||
/// link's channel, its password and privilege key come along, for Connect.
|
||||
pub fn link_form(
|
||||
link: &ServerLink,
|
||||
mut form: BookmarkForm,
|
||||
default_nickname: &str,
|
||||
) -> BookmarkForm {
|
||||
let text = |value: &Option<String>| SharedString::from(value.as_deref().unwrap_or_default());
|
||||
if form.id < 0 {
|
||||
form.address = link.address().into();
|
||||
}
|
||||
if form.id < 0 || form.nickname.is_empty() {
|
||||
form.nickname = link.nickname.as_deref().unwrap_or(default_nickname).into();
|
||||
}
|
||||
if let Some(password) = &link.password {
|
||||
form.server_password = password.into();
|
||||
}
|
||||
form.channel = text(&link.channel);
|
||||
form.channel_password = text(&link.channel_password);
|
||||
form.token = text(&link.token);
|
||||
form
|
||||
}
|
||||
|
||||
/// What joining a channel takes ([`join_password`]).
|
||||
@@ -311,6 +356,148 @@ mod tests {
|
||||
// A bare IPv6 address has no port.
|
||||
assert_eq!(invite_link("2001:db8::1", &[]), "ts3server://2001:db8::1");
|
||||
assert_eq!(invite_link("h:x", &["Ä/b"]), "ts3server://h:x?channel=%C3%84%2Fb");
|
||||
// Read back, they name the same server and channel.
|
||||
let Some(voelin_model::Link::Server(link)) =
|
||||
voelin_model::Link::parse(&invite_link("[2001:db8::1]:9987", &["Games", "Ä/b"]))
|
||||
else {
|
||||
panic!("not a server link");
|
||||
};
|
||||
assert!(same_address(&link.address(), "[2001:db8::1]:9987"));
|
||||
assert_eq!(voelin_model::split_channel_path(&link.channel.unwrap()), ["Games", "Ä/b"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn same_addresses() {
|
||||
for (a, b) in [
|
||||
("ts.example.org", "TS.Example.org"),
|
||||
(" ts.example.org ", "ts.example.org:9987"),
|
||||
("ts.example.org.", "ts.example.org"),
|
||||
("[2001:db8::1]:9987", "2001:db8::1"),
|
||||
("[2001:db8::1]", "[2001:DB8::1]:9987"),
|
||||
("127.0.0.1:9988", "127.0.0.1:9988"),
|
||||
] {
|
||||
assert!(same_address(a, b), "{a} = {b}");
|
||||
}
|
||||
for (a, b) in [
|
||||
("ts.example.org", "ts.example.org:9988"),
|
||||
("ts.example.org", "example.org"),
|
||||
("[2001:db8::1]:9988", "2001:db8::1"),
|
||||
("", ""),
|
||||
] {
|
||||
assert!(!same_address(a, b), "{a} != {b}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn servers_of_links() {
|
||||
let saved =
|
||||
|id, address: &str| Bookmark { id, address: address.into(), ..Default::default() };
|
||||
let bookmarks = [
|
||||
saved(1, "other.example"),
|
||||
saved(2, "ts.example.org"),
|
||||
saved(3, "TS.example.org:9987"),
|
||||
saved(4, "ts.example.org:9988"),
|
||||
];
|
||||
let voice = |ids: &'static [i64]| move |id| ids.contains(&id);
|
||||
// Not in voice: the one shown, else the first.
|
||||
assert_eq!(link_server(&bookmarks, "ts.example.org", None, voice(&[])), Some((2, false)));
|
||||
assert_eq!(
|
||||
link_server(&bookmarks, "ts.example.org", Some(3), voice(&[])),
|
||||
Some((3, false))
|
||||
);
|
||||
// In voice on one of them, also when another is shown.
|
||||
assert_eq!(
|
||||
link_server(&bookmarks, "ts.example.org:9987", Some(2), voice(&[1, 3])),
|
||||
Some((3, true))
|
||||
);
|
||||
// Voice on the server at another port does not count.
|
||||
assert_eq!(
|
||||
link_server(&bookmarks, "ts.example.org:9988", None, voice(&[2])),
|
||||
Some((4, false))
|
||||
);
|
||||
assert_eq!(link_server(&bookmarks, "new.example", Some(1), voice(&[1])), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channels_by_path() {
|
||||
let channel = |id, parent, order, name: &str| ChannelInfo {
|
||||
id,
|
||||
parent,
|
||||
order,
|
||||
name: name.into(),
|
||||
..Default::default()
|
||||
};
|
||||
let mut p = Presence::default();
|
||||
for c in [
|
||||
channel(1, 0, 0, "Lobby"),
|
||||
// Two of the same name, the first in the tree with the higher id.
|
||||
channel(4, 0, 1, "Games"),
|
||||
channel(3, 4, 0, "Chess"),
|
||||
channel(7, 0, 4, "AC/DC"),
|
||||
channel(2, 0, 7, "Games"),
|
||||
channel(5, 2, 0, "Go"),
|
||||
channel(6, 2, 5, "Chess"),
|
||||
] {
|
||||
p.channels.insert(c.id, c);
|
||||
}
|
||||
assert_eq!(channel_by_path(&p, "Lobby"), Some(1), "at the top");
|
||||
assert_eq!(channel_by_path(&p, "Games/Chess"), Some(3), "nested");
|
||||
// The same name: the first, unless only another has the rest.
|
||||
assert_eq!(channel_by_path(&p, "Games"), Some(4));
|
||||
assert_eq!(channel_by_path(&p, "Games/Go"), Some(5));
|
||||
assert_eq!(channel_by_path(&p, "AC\\/DC"), Some(7), "a / in a name");
|
||||
assert_eq!(channel_by_path(&p, "/6"), Some(6), "by id");
|
||||
for missing in ["Chess", "Games/Poker", "Lobby/Games", "/99", "", "lobby"] {
|
||||
assert_eq!(channel_by_path(&p, missing), None, "{missing}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forms_from_links() {
|
||||
let link = ServerLink {
|
||||
host: "ts.example.org".into(),
|
||||
port: Some(9988),
|
||||
nickname: Some("Link".into()),
|
||||
password: Some("pw".into()),
|
||||
channel: Some("Gaming/Raid Night".into()),
|
||||
channel_password: Some("raid".into()),
|
||||
token: Some("key".into()),
|
||||
};
|
||||
let bare = ServerLink::new("ts.example.org");
|
||||
// A new server: the link's address, nickname and password.
|
||||
let new = BookmarkForm { id: -1, nickname: "Me".into(), ..Default::default() };
|
||||
let f = link_form(&link, new.clone(), "Me");
|
||||
assert_eq!((f.id, f.name.as_str(), f.address.as_str()), (-1, "", "ts.example.org:9988"));
|
||||
assert_eq!((f.nickname.as_str(), f.server_password.as_str()), ("Link", "pw"));
|
||||
assert_eq!(
|
||||
(f.channel.as_str(), f.channel_password.as_str(), f.token.as_str()),
|
||||
("Gaming/Raid Night", "raid", "key")
|
||||
);
|
||||
let f = link_form(&bare, new, "Me");
|
||||
assert_eq!((f.address.as_str(), f.nickname.as_str()), ("ts.example.org", "Me"));
|
||||
assert_eq!(
|
||||
(f.server_password.as_str(), f.channel.as_str(), f.token.as_str()),
|
||||
("", "", "")
|
||||
);
|
||||
// A saved server keeps its address, name, nickname and password...
|
||||
let saved = BookmarkForm {
|
||||
id: 3,
|
||||
name: "Nightfall".into(),
|
||||
address: "TS.example.org:9988".into(),
|
||||
nickname: "Nova".into(),
|
||||
server_password: "stored".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let f = link_form(&bare, saved.clone(), "Me");
|
||||
assert_eq!(
|
||||
(f.id, f.name.as_str(), f.address.as_str()),
|
||||
(3, "Nightfall", "TS.example.org:9988")
|
||||
);
|
||||
assert_eq!((f.nickname.as_str(), f.server_password.as_str()), ("Nova", "stored"));
|
||||
// ...the link's password in place of the stored one.
|
||||
let f = link_form(&link, saved, "Me");
|
||||
assert_eq!((f.nickname.as_str(), f.server_password.as_str()), ("Nova", "pw"));
|
||||
assert_eq!((f.channel.as_str(), f.token.as_str()), ("Gaming/Raid Night", "key"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user