mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
new: use generated ids for servers (#765)
* new: use generated ids for servers Fixes #743 * fix: deps. * fix: migrate related settings * fix: restore servers from json
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
|
||||
import 'package:server_box/data/model/server/server_private_info.dart';
|
||||
import 'package:server_box/data/store/container.dart';
|
||||
import 'package:server_box/data/store/setting.dart';
|
||||
import 'package:server_box/data/store/snippet.dart';
|
||||
|
||||
class ServerStore extends HiveStore {
|
||||
ServerStore._() : super('server');
|
||||
@@ -8,15 +11,12 @@ class ServerStore extends HiveStore {
|
||||
static final instance = ServerStore._();
|
||||
|
||||
void put(Spi info) {
|
||||
// box.put(info.id, info);
|
||||
// box.updateLastModified();
|
||||
set(info.id, info);
|
||||
}
|
||||
|
||||
List<Spi> fetch() {
|
||||
final ids = box.keys;
|
||||
final List<Spi> ss = [];
|
||||
for (final id in ids) {
|
||||
for (final id in keys()) {
|
||||
final s = box.get(id);
|
||||
if (s != null && s is Spi) {
|
||||
ss.add(s);
|
||||
@@ -29,10 +29,6 @@ class ServerStore extends HiveStore {
|
||||
remove(id);
|
||||
}
|
||||
|
||||
void deleteAll() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void update(Spi old, Spi newInfo) {
|
||||
if (!have(old)) {
|
||||
throw Exception('Old spi: $old not found');
|
||||
@@ -41,5 +37,74 @@ class ServerStore extends HiveStore {
|
||||
put(newInfo);
|
||||
}
|
||||
|
||||
bool have(Spi s) => box.get(s.id) != null;
|
||||
bool have(Spi s) => get(s.id) != null;
|
||||
|
||||
void migrateIds() {
|
||||
final ss = fetch();
|
||||
final idMap = <String, String>{};
|
||||
|
||||
// Collect all old to new ID mappings
|
||||
for (final s in ss) {
|
||||
final newId = s.migrateId();
|
||||
if (newId == null) continue;
|
||||
// Use s.oldId as the key, because s.id would be empty for a server being migrated.
|
||||
// s.oldId represents the identifier used before migration.
|
||||
idMap[s.oldId] = newId;
|
||||
}
|
||||
|
||||
final srvOrder = SettingStore.instance.serverOrder.fetch();
|
||||
final snippets = SnippetStore.instance.fetch();
|
||||
final container = ContainerStore.instance;
|
||||
|
||||
bool srvOrderChanged = false;
|
||||
// Update all references to the servers
|
||||
for (final e in idMap.entries) {
|
||||
final oldId = e.key;
|
||||
final newId = e.value;
|
||||
|
||||
// Replace ids in ordering settings.
|
||||
final srvIdx = srvOrder.indexOf(oldId);
|
||||
if (srvIdx != -1) {
|
||||
srvOrder[srvIdx] = newId;
|
||||
srvOrderChanged = true;
|
||||
}
|
||||
|
||||
// Replace ids in jump server settings.
|
||||
final spi = get<Spi>(newId);
|
||||
if (spi != null) {
|
||||
final jumpId = spi.jumpId; // This could be an oldId.
|
||||
// Check if this jumpId corresponds to a server that was also migrated.
|
||||
if (jumpId != null && idMap.containsKey(jumpId)) {
|
||||
final newJumpId = idMap[jumpId];
|
||||
if (spi.jumpId != newJumpId) {
|
||||
final newSpi = spi.copyWith(jumpId: newJumpId);
|
||||
update(spi, newSpi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace ids in [Snippet]
|
||||
for (final snippet in snippets) {
|
||||
final autoRunsOn = snippet.autoRunOn;
|
||||
final idx = autoRunsOn?.indexOf(oldId);
|
||||
if (idx != null && idx != -1) {
|
||||
final newAutoRunsOn = List<String>.from(autoRunsOn ?? []);
|
||||
newAutoRunsOn[idx] = newId;
|
||||
final newSnippet = snippet.copyWith(autoRunOn: newAutoRunsOn);
|
||||
SnippetStore.instance.update(snippet, newSnippet);
|
||||
}
|
||||
}
|
||||
|
||||
// Replace ids in [Container]
|
||||
final dockerHost = container.fetch(oldId);
|
||||
if (dockerHost != null) {
|
||||
container.remove(oldId);
|
||||
container.set(newId, dockerHost);
|
||||
}
|
||||
}
|
||||
|
||||
if (srvOrderChanged) {
|
||||
SettingStore.instance.serverOrder.put(srvOrder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ class SnippetStore extends HiveStore {
|
||||
static final instance = SnippetStore._();
|
||||
|
||||
void put(Snippet snippet) {
|
||||
// box.put(snippet.name, snippet);
|
||||
// box.updateLastModified();
|
||||
set(snippet.name, snippet);
|
||||
}
|
||||
|
||||
@@ -25,8 +23,16 @@ class SnippetStore extends HiveStore {
|
||||
}
|
||||
|
||||
void delete(Snippet s) {
|
||||
// box.delete(s.name);
|
||||
// box.updateLastModified();
|
||||
remove(s.name);
|
||||
}
|
||||
|
||||
void update(Snippet old, Snippet newInfo) {
|
||||
if (!have(old)) {
|
||||
throw Exception('Old snippet: $old not found');
|
||||
}
|
||||
delete(old);
|
||||
put(newInfo);
|
||||
}
|
||||
|
||||
bool have(Snippet s) => get(s.name) != null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user