Use the TeamSpeak identity instead of the one Voelin made itself
The user wants to appear as their TeamSpeak identity. Until now the
official client's default became Voelin's only on a store without
identities, so anyone who had started Voelin once kept the identity the
app had created on its own, which nobody chose.
The store now records where each identity came from and which one is
the default (schema 4: identities.origin created/imported/user and
is_default; the migration marks the first identity named Default as
created, the rest as imported, the first as the default). When the
import on start finds the official client's default while Voelin's
default is still the created one, that identity becomes the default,
once: the created one is kept, and choosing it again later makes it the
user's own, which no import replaces. The status says so ("You now use
your TeamSpeak identity "X"; your previous one is kept").
The toast wrapped nothing and was cut at 560 pixels, wider than a phone;
it now wraps within the window. voelinctl identity list shows the origin
and the default. Tested with synthetic databases and in the app headless
with a fake home: created, then replaced once, then left alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
963441c6f5
commit
4ef5b422f0
9 files changed
+333
-65
No files matched your search
@@ -31,7 +31,7 @@ use std::{fs, str};
|
||||
use rusqlite::{Connection, OpenFlags, OptionalExtension};
|
||||
use tsclientlib::Identity;
|
||||
use voelin_gateway_proto::UniqueIds;
|
||||
use voelin_store::Store;
|
||||
use voelin_store::{IdentityOrigin, Store};
|
||||
|
||||
/// Name given to an identity whose source kept none.
|
||||
pub const DEFAULT_NICKNAME: &str = "imported";
|
||||
@@ -168,29 +168,65 @@ pub fn import(store: &Store, found: &[Found]) -> Result<Vec<Imported>, IdentityE
|
||||
if !known.insert(f.uid()) {
|
||||
return Ok(Imported::Duplicate);
|
||||
}
|
||||
Ok(Imported::Added(store.add_identity(&f.nickname, &f.identity)?))
|
||||
Ok(Imported::Added(store.add_identity(
|
||||
&f.nickname,
|
||||
&f.identity,
|
||||
IdentityOrigin::Imported,
|
||||
)?))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// What [`import_new`] did.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct LaunchImport {
|
||||
/// The identities added, the official clients' defaults first.
|
||||
pub added: Vec<Found>,
|
||||
/// The identity that became our default.
|
||||
pub default: Option<Found>,
|
||||
/// That took the place of the identity the app had created on its own
|
||||
/// ([`IdentityOrigin::Created`]), which is kept.
|
||||
pub replaced: bool,
|
||||
}
|
||||
|
||||
/// Store the identities in the official clients' files at `paths` (usually
|
||||
/// [`discover`]) that the store does not have yet, as the app does when it
|
||||
/// starts. Files that cannot be read or hold no identity are skipped; none is
|
||||
/// written. The identities the clients use by default go in first, so on a
|
||||
/// store without identities the official client's default becomes ours (the
|
||||
/// first identity) and servers see the same unique id. Returns what was
|
||||
/// added.
|
||||
pub fn import_new(store: &Store, paths: &[PathBuf]) -> Result<Vec<Found>, IdentityError> {
|
||||
/// written.
|
||||
///
|
||||
/// Servers should see the user as on the official client: while our
|
||||
/// default is an identity nobody chose, the one the app created on its own
|
||||
/// ([`IdentityOrigin::Created`]), or there is none, the identity the official
|
||||
/// client uses by default becomes ours (the TeamSpeak 6 client's before the
|
||||
/// TeamSpeak 3 client's), once: an identity the user picked as the default
|
||||
/// is [`IdentityOrigin::User`] and stays.
|
||||
pub fn import_new(store: &Store, paths: &[PathBuf]) -> Result<LaunchImport, IdentityError> {
|
||||
let before = store.default_identity()?;
|
||||
let mut found: Vec<Found> = paths.iter().filter_map(|path| read(path).ok()).flatten().collect();
|
||||
// Stable: TeamSpeak 6's default before TeamSpeak 3's, as `locations` lists them.
|
||||
found.sort_by_key(|f| !f.selected);
|
||||
let outcomes = import(store, &found)?;
|
||||
Ok(found
|
||||
if before.as_ref().is_none_or(|b| b.origin == IdentityOrigin::Created)
|
||||
&& let Some(official) = found.iter().find(|f| f.selected)
|
||||
{
|
||||
let uid = official.uid();
|
||||
if let Some(entry) = store.identities()?.into_iter().find(|e| e.uid == uid) {
|
||||
store.set_default_identity(entry.id, false)?;
|
||||
}
|
||||
}
|
||||
let after = store.default_identity()?;
|
||||
let mut report = LaunchImport::default();
|
||||
if let Some(after) = after.filter(|a| before.as_ref().is_none_or(|b| b.id != a.id)) {
|
||||
report.default = found.iter().find(|f| f.uid() == after.uid).cloned();
|
||||
report.replaced = before.is_some() && report.default.is_some();
|
||||
}
|
||||
report.added = found
|
||||
.into_iter()
|
||||
.zip(outcomes)
|
||||
.filter(|(_, outcome)| matches!(outcome, Imported::Added(_)))
|
||||
.map(|(f, _)| f)
|
||||
.collect())
|
||||
.collect();
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
/// The TeamSpeak 3 `.ini` export of an identity, which the official client
|
||||
@@ -636,6 +672,14 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn nicknames(found: &[Found]) -> Vec<&str> {
|
||||
found.iter().map(|f| f.nickname.as_str()).collect()
|
||||
}
|
||||
|
||||
fn default_uid(store: &Store) -> String {
|
||||
store.default_identity().unwrap().unwrap().uid
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn launch_import_makes_the_clients_default_ours_and_adds_only_new_ones() {
|
||||
let dir = temp_dir("launch");
|
||||
@@ -647,21 +691,71 @@ mod tests {
|
||||
let missing = dir.join("missing.db");
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
|
||||
let added = import_new(&store, &[ts6.clone(), missing.clone()]).unwrap();
|
||||
let nicknames: Vec<_> = added.iter().map(|f| f.nickname.as_str()).collect();
|
||||
assert_eq!(nicknames, ["Main", "Spare"]);
|
||||
// The first identity is the app's default: the client's default.
|
||||
assert_eq!(store.identities().unwrap()[0].uid, main.key().to_pub().get_uid());
|
||||
let report = import_new(&store, &[ts6.clone(), missing.clone()]).unwrap();
|
||||
assert_eq!(nicknames(&report.added), ["Main", "Spare"]);
|
||||
// Without an identity of its own the app takes the client's default.
|
||||
assert_eq!(report.default.unwrap().nickname, "Main");
|
||||
assert!(!report.replaced);
|
||||
assert_eq!(default_uid(&store), main.key().to_pub().get_uid());
|
||||
|
||||
// The next start finds only what is new.
|
||||
// The next start finds only what is new, and an imported default
|
||||
// stays even when another client has a different one.
|
||||
client_database(&ts3, &[(&old, "Old", true), (&main, "Main", true)]);
|
||||
let added = import_new(&store, &[ts6, ts3, missing]).unwrap();
|
||||
let nicknames: Vec<_> = added.iter().map(|f| f.nickname.as_str()).collect();
|
||||
assert_eq!(nicknames, ["Old"]);
|
||||
let stored = store.identities().unwrap();
|
||||
assert_eq!(stored.len(), 3);
|
||||
// A store that has identities keeps its default.
|
||||
assert_eq!(stored[0].uid, main.key().to_pub().get_uid());
|
||||
let report = import_new(&store, &[ts6, ts3, missing]).unwrap();
|
||||
assert_eq!(nicknames(&report.added), ["Old"]);
|
||||
assert!(report.default.is_none() && !report.replaced);
|
||||
assert_eq!(store.identities().unwrap().len(), 3);
|
||||
assert_eq!(default_uid(&store), main.key().to_pub().get_uid());
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// The identity the app made on its first start gives way to the
|
||||
/// official client's default once; the user's later choice stays.
|
||||
#[test]
|
||||
fn the_created_identity_gives_way_once() {
|
||||
let dir = temp_dir("replace");
|
||||
let ts6 = dir.join("ts6.db");
|
||||
let (made, main) = (synthetic(), synthetic());
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let made_id = store.add_identity("Default", &made, IdentityOrigin::Created).unwrap();
|
||||
// The official client is not there yet: nothing changes.
|
||||
let report = import_new(&store, std::slice::from_ref(&ts6)).unwrap();
|
||||
assert!(report.added.is_empty() && report.default.is_none());
|
||||
assert_eq!(default_uid(&store), made.key().to_pub().get_uid());
|
||||
|
||||
client_database(&ts6, &[(&main, "Main", true)]);
|
||||
let report = import_new(&store, std::slice::from_ref(&ts6)).unwrap();
|
||||
assert_eq!(report.default.unwrap().nickname, "Main");
|
||||
assert!(report.replaced);
|
||||
assert_eq!(default_uid(&store), main.key().to_pub().get_uid());
|
||||
// The one the app made is kept.
|
||||
assert_eq!(store.identities().unwrap().len(), 2);
|
||||
|
||||
// The user goes back to it: later starts leave that alone.
|
||||
store.set_default_identity(made_id, true).unwrap();
|
||||
for _ in 0..2 {
|
||||
let report = import_new(&store, std::slice::from_ref(&ts6)).unwrap();
|
||||
assert!(report.added.is_empty() && report.default.is_none() && !report.replaced);
|
||||
assert_eq!(default_uid(&store), made.key().to_pub().get_uid());
|
||||
}
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// An identity imported earlier (by `voelinctl identity import`, which
|
||||
/// does not choose a default) still takes the created one's place.
|
||||
#[test]
|
||||
fn an_earlier_import_still_replaces_the_created_identity() {
|
||||
let dir = temp_dir("earlier");
|
||||
let ts6 = dir.join("ts6.db");
|
||||
let (made, main) = (synthetic(), synthetic());
|
||||
client_database(&ts6, &[(&main, "Main", true)]);
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
store.add_identity("Default", &made, IdentityOrigin::Created).unwrap();
|
||||
import(&store, &read(&ts6).unwrap()).unwrap();
|
||||
assert_eq!(default_uid(&store), made.key().to_pub().get_uid());
|
||||
let report = import_new(&store, &[ts6]).unwrap();
|
||||
assert!(report.added.is_empty() && report.replaced);
|
||||
assert_eq!(default_uid(&store), main.key().to_pub().get_uid());
|
||||
fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
|
||||
@@ -274,8 +274,7 @@ mod tests {
|
||||
db.execute_batch("INSERT INTO settings (key, value) VALUES ('k', '1');").unwrap();
|
||||
}
|
||||
let store = Store::open(&path).unwrap();
|
||||
assert_eq!(store.schema_version().unwrap(), 3);
|
||||
assert_eq!(Store::SCHEMA_VERSION, 3);
|
||||
assert_eq!(store.schema_version().unwrap(), Store::SCHEMA_VERSION);
|
||||
assert_eq!(store.setting::<u32>("k").unwrap(), Some(1));
|
||||
store.put_contact(&friend("a=")).unwrap();
|
||||
drop(store);
|
||||
|
||||
@@ -21,7 +21,7 @@ pub use contacts::{Contact, ContactSeen, Relation};
|
||||
#[cfg(feature = "keyring")]
|
||||
pub use secrets::KeyringSecrets;
|
||||
pub use secrets::{MemorySecrets, Secrets};
|
||||
pub use store::{Bookmark, IdentityEntry, QueryConfig, QueryTransport, Store};
|
||||
pub use store::{Bookmark, IdentityEntry, IdentityOrigin, QueryConfig, QueryTransport, Store};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
|
||||
@@ -22,8 +22,22 @@ const MIGRATIONS: &[Migration] = &[
|
||||
Migration::Sql(SCHEMA_1),
|
||||
Migration::Code(crate::chat::migrate_2),
|
||||
Migration::Sql(crate::contacts::SCHEMA_3),
|
||||
Migration::Sql(SCHEMA_4),
|
||||
];
|
||||
|
||||
/// Version 4: where each identity came from ([`IdentityOrigin`]) and which
|
||||
/// one is the default. Until now the app created an identity named
|
||||
/// `Default` when it had none, and the only other way in was an import;
|
||||
/// the default was the first identity.
|
||||
const SCHEMA_4: &str = r#"
|
||||
ALTER TABLE identities ADD COLUMN origin TEXT NOT NULL DEFAULT 'user';
|
||||
ALTER TABLE identities ADD COLUMN is_default INTEGER NOT NULL DEFAULT 0;
|
||||
UPDATE identities SET origin = 'imported';
|
||||
UPDATE identities SET origin = 'created'
|
||||
WHERE id = (SELECT MIN(id) FROM identities) AND name = 'Default';
|
||||
UPDATE identities SET is_default = 1 WHERE id = (SELECT MIN(id) FROM identities);
|
||||
"#;
|
||||
|
||||
/// Version 1: identities, bookmarks, settings and a first chat cache.
|
||||
pub(crate) const SCHEMA_1: &str = r#"
|
||||
CREATE TABLE identities (
|
||||
@@ -62,6 +76,39 @@ pub struct IdentityEntry {
|
||||
pub name: String,
|
||||
pub uid: String,
|
||||
pub level: u8,
|
||||
pub origin: IdentityOrigin,
|
||||
/// The identity the app connects with.
|
||||
pub is_default: bool,
|
||||
}
|
||||
|
||||
/// Where an identity came from.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum IdentityOrigin {
|
||||
/// Made by the app on its own because it had none; nobody chose it, so
|
||||
/// the official client's identity may take its place as the default.
|
||||
Created,
|
||||
/// From another client (`voelin_core::identity`).
|
||||
Imported,
|
||||
/// Made or chosen as the default by the user.
|
||||
User,
|
||||
}
|
||||
|
||||
impl IdentityOrigin {
|
||||
fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
Self::Created => "created",
|
||||
Self::Imported => "imported",
|
||||
Self::User => "user",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(text: &str) -> Self {
|
||||
match text {
|
||||
"created" => Self::Created,
|
||||
"imported" => Self::Imported,
|
||||
_ => Self::User,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How to reach a server's ServerQuery for invisible presence and relay chat.
|
||||
@@ -179,28 +226,71 @@ impl Store {
|
||||
|
||||
// Identities
|
||||
|
||||
pub fn add_identity(&self, name: &str, identity: &Identity) -> Result<i64> {
|
||||
/// Store an identity; it is the default when no other one is.
|
||||
pub fn add_identity(
|
||||
&self,
|
||||
name: &str,
|
||||
identity: &Identity,
|
||||
origin: IdentityOrigin,
|
||||
) -> Result<i64> {
|
||||
let uid = identity.key().to_pub().get_uid();
|
||||
self.db.execute(
|
||||
"INSERT INTO identities (name, uid, data) VALUES (?1, ?2, ?3)",
|
||||
params![name, uid, serde_json::to_string(identity)?],
|
||||
"INSERT INTO identities (name, uid, data, origin, is_default)
|
||||
VALUES (?1, ?2, ?3, ?4, NOT EXISTS (SELECT 1 FROM identities WHERE is_default))",
|
||||
params![name, uid, serde_json::to_string(identity)?, origin.as_str()],
|
||||
)?;
|
||||
Ok(self.db.last_insert_rowid())
|
||||
}
|
||||
|
||||
pub fn identities(&self) -> Result<Vec<IdentityEntry>> {
|
||||
let mut stmt = self.db.prepare("SELECT id, name, uid, data FROM identities ORDER BY id")?;
|
||||
let mut stmt = self.db.prepare(
|
||||
"SELECT id, name, uid, data, origin, is_default FROM identities ORDER BY id",
|
||||
)?;
|
||||
let rows = stmt.query_map([], |r| {
|
||||
Ok((r.get::<_, i64>(0)?, r.get(1)?, r.get(2)?, r.get::<_, String>(3)?))
|
||||
Ok((
|
||||
r.get::<_, i64>(0)?,
|
||||
r.get(1)?,
|
||||
r.get(2)?,
|
||||
r.get::<_, String>(3)?,
|
||||
r.get::<_, String>(4)?,
|
||||
r.get(5)?,
|
||||
))
|
||||
})?;
|
||||
rows.map(|row| {
|
||||
let (id, name, uid, data) = row?;
|
||||
let (id, name, uid, data, origin, is_default) = row?;
|
||||
let identity: Identity = serde_json::from_str(&data)?;
|
||||
Ok(IdentityEntry { id, name, uid, level: identity.level() })
|
||||
let origin = IdentityOrigin::parse(&origin);
|
||||
Ok(IdentityEntry { id, name, uid, level: identity.level(), origin, is_default })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The identity the app connects with: the one marked as the default,
|
||||
/// else the first.
|
||||
pub fn default_identity(&self) -> Result<Option<IdentityEntry>> {
|
||||
let identities = self.identities()?;
|
||||
Ok(identities.iter().find(|i| i.is_default).or(identities.first()).cloned())
|
||||
}
|
||||
|
||||
/// Make `id` the default identity. `by_user`: the user chose it, so one
|
||||
/// the app created on its own becomes theirs ([`IdentityOrigin::User`])
|
||||
/// and is never replaced by an import again.
|
||||
pub fn set_default_identity(&self, id: i64, by_user: bool) -> Result<()> {
|
||||
let tx = self.db.unchecked_transaction()?;
|
||||
if tx.execute("UPDATE identities SET is_default = 1 WHERE id = ?1", [id])? == 0 {
|
||||
return Err(Error::NotFound("identity", id));
|
||||
}
|
||||
tx.execute("UPDATE identities SET is_default = 0 WHERE id <> ?1", [id])?;
|
||||
if by_user {
|
||||
tx.execute(
|
||||
"UPDATE identities SET origin = 'user' WHERE id = ?1 AND origin = 'created'",
|
||||
[id],
|
||||
)?;
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn identity(&self, id: i64) -> Result<Identity> {
|
||||
let data: Option<String> = self
|
||||
.db
|
||||
@@ -337,22 +427,86 @@ mod tests {
|
||||
fn identities() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
let identity = Identity::create();
|
||||
let id = store.add_identity("main", &identity).unwrap();
|
||||
let id = store.add_identity("main", &identity, IdentityOrigin::Imported).unwrap();
|
||||
let list = store.identities().unwrap();
|
||||
assert_eq!(list.len(), 1);
|
||||
assert_eq!(list[0].name, "main");
|
||||
assert_eq!(list[0].uid, identity.key().to_pub().get_uid());
|
||||
assert!(list[0].level >= 8);
|
||||
assert_eq!(list[0].origin, IdentityOrigin::Imported);
|
||||
let loaded = store.identity(id).unwrap();
|
||||
assert_eq!(loaded.counter(), identity.counter());
|
||||
// The same key twice is rejected (uid is unique).
|
||||
assert!(store.add_identity("dup", &identity).is_err());
|
||||
assert!(store.add_identity("dup", &identity, IdentityOrigin::User).is_err());
|
||||
store.rename_identity(id, "renamed").unwrap();
|
||||
assert_eq!(store.identities().unwrap()[0].name, "renamed");
|
||||
store.delete_identity(id).unwrap();
|
||||
assert!(matches!(store.identity(id), Err(Error::NotFound(..))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_default_identity() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
assert_eq!(store.default_identity().unwrap(), None);
|
||||
let made = store.add_identity("Default", &Identity::create(), IdentityOrigin::Created);
|
||||
let made = made.unwrap();
|
||||
let other = store.add_identity("Other", &Identity::create(), IdentityOrigin::Imported);
|
||||
let other = other.unwrap();
|
||||
// The first one is the default.
|
||||
let default = store.default_identity().unwrap().unwrap();
|
||||
assert_eq!((default.id, default.origin), (made, IdentityOrigin::Created));
|
||||
// An import moves the default without making the old one the user's.
|
||||
store.set_default_identity(other, false).unwrap();
|
||||
assert_eq!(store.default_identity().unwrap().unwrap().id, other);
|
||||
assert_eq!(store.identities().unwrap()[0].origin, IdentityOrigin::Created);
|
||||
// The user's choice makes the created one theirs.
|
||||
store.set_default_identity(made, true).unwrap();
|
||||
let list = store.identities().unwrap();
|
||||
assert_eq!((list[0].is_default, list[0].origin), (true, IdentityOrigin::User));
|
||||
assert_eq!((list[1].is_default, list[1].origin), (false, IdentityOrigin::Imported));
|
||||
assert!(matches!(store.set_default_identity(99, true), Err(Error::NotFound(..))));
|
||||
assert_eq!(store.default_identity().unwrap().unwrap().id, made);
|
||||
}
|
||||
|
||||
/// A database of version 3 learns where its identities came from: the
|
||||
/// first one, named `Default`, is the one the app made; the others were
|
||||
/// imported; the first stays the default.
|
||||
#[test]
|
||||
fn migrates_version_3_identities() {
|
||||
let dir = std::env::temp_dir().join(format!("voelin-store-v3-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("client.db");
|
||||
let (made, imported) = (Identity::create(), Identity::create());
|
||||
{
|
||||
let mut db = rusqlite::Connection::open(&path).unwrap();
|
||||
db.execute_batch(SCHEMA_1).unwrap();
|
||||
let tx = db.transaction().unwrap();
|
||||
crate::chat::migrate_2(&tx).unwrap();
|
||||
tx.commit().unwrap();
|
||||
db.execute_batch(crate::contacts::SCHEMA_3).unwrap();
|
||||
db.pragma_update(None, "user_version", 3).unwrap();
|
||||
for (name, identity) in [("Default", &made), ("Main", &imported)] {
|
||||
let uid = identity.key().to_pub().get_uid();
|
||||
let data = serde_json::to_string(identity).unwrap();
|
||||
db.execute(
|
||||
"INSERT INTO identities (name, uid, data) VALUES (?1, ?2, ?3)",
|
||||
params![name, uid, data],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
let store = Store::open(&path).unwrap();
|
||||
assert_eq!(store.schema_version().unwrap(), Store::SCHEMA_VERSION);
|
||||
let list = store.identities().unwrap();
|
||||
assert_eq!((list[0].origin, list[0].is_default), (IdentityOrigin::Created, true));
|
||||
assert_eq!((list[1].origin, list[1].is_default), (IdentityOrigin::Imported, false));
|
||||
// The keys are untouched.
|
||||
assert_eq!(store.identity(list[1].id).unwrap().counter(), imported.counter());
|
||||
drop(store);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bookmarks_keep_order_and_fields() {
|
||||
let store = Store::open_in_memory().unwrap();
|
||||
|
||||
@@ -534,24 +534,24 @@ pub fn run(options: RunOptions) -> Result<()> {
|
||||
let prefs = open_settings(&dir, &options.setting_overrides);
|
||||
let switches = crate::dev::Switches::from_env();
|
||||
// New identities of the official TeamSpeak clients, on every start (not
|
||||
// with the sample data of VOELIN_DEMO_UI); with none of our own yet, the
|
||||
// client's default identity becomes ours.
|
||||
let had_identity = !store.identities()?.is_empty();
|
||||
// with the sample data of VOELIN_DEMO_UI); the client's default
|
||||
// identity becomes ours while we have none the user chose.
|
||||
let imported = if prefs.get(&IDENTITY_IMPORT) && !switches.demo_ui {
|
||||
voelin_core::identity::import_new(&store, &voelin_core::identity::discover())
|
||||
.unwrap_or_else(|e| {
|
||||
warn!(%e, "could not import TeamSpeak identities");
|
||||
Vec::new()
|
||||
Default::default()
|
||||
})
|
||||
} else {
|
||||
Vec::new()
|
||||
Default::default()
|
||||
};
|
||||
let identity = match store.identities()?.first() {
|
||||
let identity = match store.default_identity()? {
|
||||
Some(entry) => store.identity(entry.id)?,
|
||||
None => {
|
||||
// Creating an identity takes a moment (security level 8).
|
||||
let identity = tsclientlib::Identity::create();
|
||||
store.add_identity(crate::servers::CREATED_IDENTITY, &identity)?;
|
||||
let origin = voelin_store::IdentityOrigin::Created;
|
||||
store.add_identity(crate::servers::CREATED_IDENTITY, &identity, origin)?;
|
||||
identity
|
||||
}
|
||||
};
|
||||
@@ -636,7 +636,7 @@ pub fn run(options: RunOptions) -> Result<()> {
|
||||
if app.demo {
|
||||
app.start_demo();
|
||||
}
|
||||
if let Some(text) = crate::servers::imported_status(&imported, had_identity) {
|
||||
if let Some(text) = crate::servers::imported_status(&imported) {
|
||||
app.set_status(text);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use slint::{ComponentHandle, SharedString};
|
||||
use tracing::warn;
|
||||
use voelin_core::identity::Found;
|
||||
use voelin_core::identity::LaunchImport;
|
||||
use voelin_core::{Command, ObserveState, Source, VoiceOptions, VoiceState};
|
||||
use voelin_store::{Bookmark, QueryConfig, QueryTransport};
|
||||
|
||||
@@ -14,20 +14,20 @@ use crate::vm;
|
||||
pub(crate) const CREATED_IDENTITY: &str = "Default";
|
||||
|
||||
/// What to tell the user about the identities imported from the official
|
||||
/// clients at start ([`voelin_core::identity::import_new`]), if any.
|
||||
/// `had_identity`: we had one before, so it stays the default.
|
||||
pub(crate) fn imported_status(imported: &[Found], had_identity: bool) -> Option<String> {
|
||||
let first = imported.first()?;
|
||||
let n = imported.len();
|
||||
let names = imported.iter().map(|f| f.nickname.as_str()).collect::<Vec<_>>().join(", ");
|
||||
Some(match (had_identity, n) {
|
||||
(true, 1) => format!("Imported the identity \u{201c}{names}\u{201d} from TeamSpeak"),
|
||||
(true, _) => format!("Imported {n} identities from TeamSpeak: {names}"),
|
||||
(false, 1) => format!("Using your TeamSpeak identity \u{201c}{names}\u{201d}"),
|
||||
(false, _) => format!(
|
||||
"Using your TeamSpeak identity \u{201c}{}\u{201d} ({n} imported: {names})",
|
||||
first.nickname
|
||||
/// clients at start ([`voelin_core::identity::import_new`]), if anything.
|
||||
pub(crate) fn imported_status(report: &LaunchImport) -> Option<String> {
|
||||
let n = report.added.len();
|
||||
let names = report.added.iter().map(|f| f.nickname.as_str()).collect::<Vec<_>>().join(", ");
|
||||
let more = if n > 1 { format!(" ({n} imported: {names})") } else { String::new() };
|
||||
Some(match &report.default {
|
||||
Some(f) if report.replaced => format!(
|
||||
"You now use your TeamSpeak identity \u{201c}{}\u{201d}; your previous one is kept{more}",
|
||||
f.nickname
|
||||
),
|
||||
Some(f) => format!("Using your TeamSpeak identity \u{201c}{}\u{201d}{more}", f.nickname),
|
||||
None if n == 1 => format!("Imported the identity \u{201c}{names}\u{201d} from TeamSpeak"),
|
||||
None if n > 1 => format!("Imported {n} identities from TeamSpeak: {names}"),
|
||||
None => return None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -104,9 +104,9 @@ impl App {
|
||||
/// from TeamSpeak keeps the nickname used there), else the system user's.
|
||||
pub(crate) fn default_nickname(&self) -> String {
|
||||
self.store
|
||||
.identities()
|
||||
.default_identity()
|
||||
.ok()
|
||||
.and_then(|identities| identities.into_iter().next())
|
||||
.flatten()
|
||||
.map(|identity| identity.name)
|
||||
.filter(|name| {
|
||||
![CREATED_IDENTITY, voelin_core::identity::DEFAULT_NICKNAME]
|
||||
@@ -470,6 +470,8 @@ impl App {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use voelin_core::identity::Found;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -536,14 +538,24 @@ mod tests {
|
||||
source: "settings.db".into(),
|
||||
selected: false,
|
||||
};
|
||||
assert_eq!(imported_status(&[], false), None);
|
||||
let report = |added: &[&str], default: Option<&str>, replaced| LaunchImport {
|
||||
added: added.iter().map(|n| found(n)).collect(),
|
||||
default: default.map(found),
|
||||
replaced,
|
||||
};
|
||||
assert_eq!(imported_status(&report(&[], None, false)), None);
|
||||
assert_eq!(
|
||||
imported_status(&[found("Main")], false).unwrap(),
|
||||
imported_status(&report(&["Main"], Some("Main"), false)).unwrap(),
|
||||
"Using your TeamSpeak identity \u{201c}Main\u{201d}"
|
||||
);
|
||||
assert_eq!(
|
||||
imported_status(&[found("Main"), found("Alt")], true).unwrap(),
|
||||
imported_status(&report(&["Main", "Alt"], None, false)).unwrap(),
|
||||
"Imported 2 identities from TeamSpeak: Main, Alt"
|
||||
);
|
||||
// Imported earlier, now ours instead of the one the app made.
|
||||
assert_eq!(
|
||||
imported_status(&report(&[], Some("Main"), true)).unwrap(),
|
||||
"You now use your TeamSpeak identity \u{201c}Main\u{201d}; your previous one is kept"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -212,6 +212,7 @@ export component MainWindow inherits Window {
|
||||
x: (parent.width - self.width) / 2;
|
||||
y: parent.height - self.height - (Nav.narrow ? 140px : 96px);
|
||||
text: Bridge.status-text;
|
||||
widest: min(560px, parent.width - 32px);
|
||||
}
|
||||
|
||||
// The member card of the members panel, over everything.
|
||||
|
||||
@@ -234,8 +234,10 @@ export component Toast inherits Rectangle {
|
||||
in property <image> icon: Icons.info;
|
||||
in property <duration> timeout: 5s;
|
||||
in-out property <bool> shown;
|
||||
// The widest it gets (e.g. the window less a margin); longer text wraps.
|
||||
in property <length> widest: 560px;
|
||||
height: shown ? row.preferred-height + 16px : 0px;
|
||||
width: min(row.preferred-width + 24px, 560px);
|
||||
width: min(row.preferred-width + 24px, widest);
|
||||
visible: shown;
|
||||
border-radius: Theme.radius-lg;
|
||||
background: Theme.surface-3;
|
||||
@@ -266,7 +268,7 @@ export component Toast inherits Rectangle {
|
||||
text: root.text;
|
||||
color: Theme.text;
|
||||
vertical-alignment: center;
|
||||
overflow: elide;
|
||||
wrap: word-wrap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,14 @@ fn open_store(path: Option<PathBuf>) -> Result<Store> {
|
||||
|
||||
/// Identities in the client database, with the id `export` takes.
|
||||
pub fn list(store: Option<PathBuf>) -> Result<()> {
|
||||
for entry in open_store(store)?.identities()? {
|
||||
println!("{:>3} {:<20} {} level {}", entry.id, entry.name, entry.uid, entry.level);
|
||||
let store = open_store(store)?;
|
||||
let default = store.default_identity()?.map(|entry| entry.id);
|
||||
for entry in store.identities()? {
|
||||
let mark = if Some(entry.id) == default { " default" } else { "" };
|
||||
println!(
|
||||
"{:>3} {:<20} {} level {:<3} {:?}{mark}",
|
||||
entry.id, entry.name, entry.uid, entry.level, entry.origin
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user