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:
@@ -10,6 +10,7 @@ import '../../transport/pipelines_client.dart';
|
||||
import '../../../features/settings/server_connections.dart';
|
||||
import '../machine_access.dart';
|
||||
import '../server_catalog.dart';
|
||||
import '../server_naming.dart';
|
||||
import 'browser_peer.dart';
|
||||
import 'chat_server_gateway.dart';
|
||||
import 'federated_chat_gateway.dart';
|
||||
@@ -27,6 +28,9 @@ class ChatMachineStatus {
|
||||
|
||||
/// Why the machine is not connected, once classified; null while unknown.
|
||||
MachineAccessResult? access;
|
||||
|
||||
/// The machine reported its hostname to this pool; asked once per pool.
|
||||
bool hostnameKnown = false;
|
||||
}
|
||||
|
||||
/// Probes one saved machine with its own kept sign-in (see
|
||||
@@ -180,6 +184,7 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
|
||||
if (item.connected && !was) {
|
||||
revision++;
|
||||
unawaited(ackSync.connected());
|
||||
unawaited(_learnHostname(item, generation));
|
||||
}
|
||||
if (!item.connected && !item.connecting && item.access == null) {
|
||||
unawaited(_classify(item, generation));
|
||||
@@ -189,6 +194,7 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
|
||||
revision++;
|
||||
if (item.connected) {
|
||||
unawaited(ackSync.connected());
|
||||
unawaited(_learnHostname(item, generation));
|
||||
} else {
|
||||
unawaited(_classify(item, generation));
|
||||
}
|
||||
@@ -207,6 +213,21 @@ class ChatServerPool extends ChangeNotifier implements MachineAuthenticator {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
/// Names a machine the app named by its address after the hostname it
|
||||
/// reports (see [ServerConnections.learnHostname]); typed names are kept.
|
||||
Future<void> _learnHostname(ChatMachineStatus item, int generation) async {
|
||||
final adapter = item.adapter;
|
||||
if (item.hostnameKnown || adapter == null) return;
|
||||
item.hostnameKnown = true;
|
||||
final hostname = await fetchServerHostname(adapter.gateway);
|
||||
if (_disposed || generation != item.generation) return;
|
||||
if (hostname.isEmpty) {
|
||||
item.hostnameKnown = false;
|
||||
return;
|
||||
}
|
||||
await servers.learnHostname(item.server.id, hostname);
|
||||
}
|
||||
|
||||
/// A native socket that did not open says nothing about why. Ask the web
|
||||
/// login with this machine's own kept sign-in: a form means it wants its
|
||||
/// password, a redirect means the sign-in is fine and the socket failed.
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'dart:math';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'server_catalog.dart';
|
||||
import 'server_naming.dart';
|
||||
import '../transport/server_url.dart';
|
||||
|
||||
const String serverCatalogKey = 'pipelines.next.serverCatalog.v1';
|
||||
@@ -27,7 +28,8 @@ ServerCatalog _migrate(String legacyEndpoint, [List<String>? binding]) {
|
||||
servers: [
|
||||
SavedServer.validated(
|
||||
id: id,
|
||||
name: 'My server',
|
||||
// Its address names it until the machine reports its hostname.
|
||||
name: temporaryServerName(legacyEndpoint),
|
||||
endpoints: [legacyEndpoint],
|
||||
legacyEndpoint: canMigrate ? legacyEndpoint : null,
|
||||
),
|
||||
|
||||
@@ -15,7 +15,10 @@ class ServerContext extends InheritedNotifier<ServerConnections> {
|
||||
static ServerContext? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<ServerContext>();
|
||||
|
||||
/// The machine's name; a browser's unsaved page origin is named by the
|
||||
/// hostname it reported.
|
||||
String? get name {
|
||||
if (serverId == null) return notifier!.originHostname;
|
||||
for (final server in notifier!.catalog.servers) {
|
||||
if (server.id == serverId) return server.name;
|
||||
}
|
||||
|
||||
@@ -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