From 54c4f873fc530f5cc444ac05092638ff436f522d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 18 Dec 2025 14:51:15 +0100 Subject: [PATCH] [Flutter] Use deviceName instead of hostName for clients --- flutter/lib/main.dart | 40 ++++++++++++++++++++------ flutter/lib/main_utils.dart | 4 +-- flutter/lib/pages/account/account.dart | 2 +- flutter/lib/pages/account/login.dart | 2 +- flutter/lib/state/app_auth.dart | 8 ++++-- flutter/lib/state/globals.dart | 8 ++++++ 6 files changed, 49 insertions(+), 15 deletions(-) diff --git a/flutter/lib/main.dart b/flutter/lib/main.dart index f61ba1b..ed8888a 100644 --- a/flutter/lib/main.dart +++ b/flutter/lib/main.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:in_app_purchase/in_app_purchase.dart'; import 'package:provider/provider.dart'; +import 'package:simplecloudnotifier/api/api_client.dart'; import 'package:simplecloudnotifier/main_messaging.dart'; import 'package:simplecloudnotifier/main_utils.dart'; import 'package:simplecloudnotifier/components/layout/nav_layout.dart'; @@ -68,15 +69,17 @@ void main() async { print('[INIT] Request Notification permissions...'); await FirebaseMessaging.instance.requestPermission(provisional: true); - FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) { - try { - setFirebaseToken(fcmToken); - } catch (exc, trace) { - ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace); - } - }).onError((dynamic err) { - ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? '')); - }); + FirebaseMessaging.instance.onTokenRefresh + .listen((fcmToken) { + try { + setFirebaseToken(fcmToken); + } catch (exc, trace) { + ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace); + } + }) + .onError((dynamic err) { + ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? '')); + }); try { print('[INIT] Query firebase token...'); @@ -96,6 +99,25 @@ void main() async { await appAuth.tryMigrateFromV1(); + if (appAuth.isAuth()) { + print('[INIT] Load Client and potentially update...'); + + try { + var client = await appAuth.loadClient(onlyCached: true); + if (client != null) { + if (client.agentModel != Globals().deviceModel || client.name != Globals().nameForClient() || client.agentVersion != Globals().version) { + print('[INIT] Update Client info...'); + + final newClient = await APIClient.updateClient(appAuth, client.clientID, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version); + appAuth.setClientAndClientID(newClient); + await appAuth.save(); + } + } + } catch (exc, trace) { + ApplicationLog.error('Failed to get client (on init): ' + exc.toString(), trace: trace); + } + } + print('[INIT] Load Notifications...'); final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); diff --git a/flutter/lib/main_utils.dart b/flutter/lib/main_utils.dart index 72a3892..f0871a8 100644 --- a/flutter/lib/main_utils.dart +++ b/flutter/lib/main_utils.dart @@ -40,11 +40,11 @@ void setFirebaseToken(String fcmToken) async { if (client == null) { // should not really happen - perhaps someone externally deleted the client? - final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); + final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType); acc.setClientAndClientID(newClient); await acc.save(); } else { - final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().hostname, agentVersion: Globals().version); + final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken: fcmToken, agentModel: Globals().deviceModel, name: Globals().nameForClient(), agentVersion: Globals().version); acc.setClientAndClientID(newClient); await acc.save(); } diff --git a/flutter/lib/pages/account/account.dart b/flutter/lib/pages/account/account.dart index 583c5d3..515b483 100644 --- a/flutter/lib/pages/account/account.dart +++ b/flutter/lib/pages/account/account.dart @@ -527,7 +527,7 @@ class _AccountRootPageState extends State { await Globals().setPrefFCMToken(fcmToken); - final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().hostname, Globals().clientType); + final user = await APIClient.createUserWithClient(null, fcmToken, Globals().platform, Globals().version, Globals().nameForClient(), Globals().clientType); acc.set(user.user, user.clients[0], user.adminKey, user.sendKey); diff --git a/flutter/lib/pages/account/login.dart b/flutter/lib/pages/account/login.dart index 7b20334..5028269 100644 --- a/flutter/lib/pages/account/login.dart +++ b/flutter/lib/pages/account/login.dart @@ -156,7 +156,7 @@ class _AccountLoginPageState extends State { final user = await APIClient.getUser(DirectTokenSource(uid, atokv), uid); - final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); + final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType); acc.set(user, client, atokv, stokv); await acc.save(); diff --git a/flutter/lib/state/app_auth.dart b/flutter/lib/state/app_auth.dart index aed86f0..d2220eb 100644 --- a/flutter/lib/state/app_auth.dart +++ b/flutter/lib/state/app_auth.dart @@ -99,7 +99,7 @@ class AppAuth extends ChangeNotifier implements TokenSource { final user = await APIClient.getUser(DirectTokenSource(oldUserID, oldUserKey), oldUserID); - final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType); + final client = await APIClient.addClient(DirectTokenSource(oldUserID, oldUserKey), fcmToken, Globals().deviceModel, Globals().version, Globals().nameForClient(), Globals().clientType); set(user, client, oldUserKey, newTokenSend.token); @@ -232,7 +232,7 @@ class AppAuth extends ChangeNotifier implements TokenSource { return _user?.$1; } - Future loadClient({bool force = false, Duration? forceIfOlder = null}) async { + Future loadClient({bool force = false, Duration? forceIfOlder = null, bool onlyCached = false}) async { if (forceIfOlder != null && _client != null && _client!.$2.difference(DateTime.now()) > forceIfOlder) { force = true; } @@ -245,6 +245,10 @@ class AppAuth extends ChangeNotifier implements TokenSource { throw Exception('Not authenticated'); } + if (onlyCached) { + return null; + } + try { final client = await APIClient.getClient(this, _clientID!); diff --git a/flutter/lib/state/globals.dart b/flutter/lib/state/globals.dart index 9dbf446..3890bc5 100644 --- a/flutter/lib/state/globals.dart +++ b/flutter/lib/state/globals.dart @@ -92,4 +92,12 @@ class Globals { Future setPrefFCMToken(String value) { return sharedPrefs.setString("fcm.token", value); } + + String nameForClient() { + if (this.deviceName.isNotEmpty) { + return this.deviceName; + } else { + return this.hostname; + } + } }