Servers: name a machine after its hostname once it connects
A saved server still named by the app ('My server', 'This server',
'Original legacy server' or its address host/IP) takes the hostname the
machine reports in get_status the first time the app connects; typed
names are kept. New servers (legacy migration, Settings with a blank
name, onboarding) start with the address host. A browser on its page
origin with no saved profile shows the reported hostname too. No server
change: get_status already reports the hostname.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import '../api_gateway.dart';
|
||||
import 'server_catalog.dart';
|
||||
|
||||
/// Names the app itself gave a machine before it knew better. Older builds
|
||||
/// saved these; a machine named one of them takes its own hostname instead.
|
||||
const Set<String> placeholderServerNames = {
|
||||
'My server',
|
||||
'This server',
|
||||
'Original legacy server',
|
||||
};
|
||||
|
||||
/// The temporary name of a machine reached at [url]: its address's host.
|
||||
String temporaryServerName(String url) {
|
||||
final host = Uri.tryParse(url.trim())?.host ?? '';
|
||||
return host.isEmpty ? url.trim() : host;
|
||||
}
|
||||
|
||||
/// Whether [server] still carries a name the app chose: a placeholder, or
|
||||
/// exactly the host (or IP) of one of its addresses. A name the operator
|
||||
/// typed is anything else and is never replaced.
|
||||
bool hasPlaceholderName(SavedServer server) {
|
||||
final name = server.name.trim();
|
||||
if (placeholderServerNames.contains(name)) return true;
|
||||
final lower = name.toLowerCase();
|
||||
return server.endpoints.any(
|
||||
(url) => temporaryServerName(url).toLowerCase() == lower,
|
||||
);
|
||||
}
|
||||
|
||||
/// [server] renamed to the [hostname] its machine reports, or null when it
|
||||
/// keeps its name: the name is the operator's, or the hostname is empty or
|
||||
/// already the name.
|
||||
SavedServer? renamedToHostname(SavedServer server, String hostname) {
|
||||
final name = hostname.trim();
|
||||
if (name.isEmpty || name == server.name || !hasPlaceholderName(server)) {
|
||||
return null;
|
||||
}
|
||||
return SavedServer.validated(
|
||||
id: server.id,
|
||||
name: name,
|
||||
endpoints: server.endpoints,
|
||||
legacyEndpoint: server.legacyEndpoint,
|
||||
);
|
||||
}
|
||||
|
||||
/// The machine's own hostname from a `get_status` reply, or '' when absent.
|
||||
String hostnameFromStatus(JsonMap reply) {
|
||||
final status = reply['status'];
|
||||
if (status is! Map) return '';
|
||||
final hostname = status['hostname'];
|
||||
return hostname is String ? hostname.trim() : '';
|
||||
}
|
||||
|
||||
/// Asks the machine behind [gateway] for its hostname; '' when it cannot say
|
||||
/// (an older server, a refused request or a dropped connection).
|
||||
Future<String> fetchServerHostname(ApiGateway gateway) async {
|
||||
try {
|
||||
return hostnameFromStatus(await gateway.call('get_status'));
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user