Merge dev; fix share-location and discovered-contact import/export; update docs
Merge dev (#588 share location, #436 discovered-contact import/export) into fix/review-fixes-and-issue-batch. Conflicts: keep the node-scan button alongside dev's import/export menu; take dev's marker label prompt. Share location (#588): - Never share a missing or 0,0 radio position; hide the chat action and show "Your radio has no location set" - Sanitise marker labels ('|' -> '/', newlines -> spaces) on every path - One shared MarkerLabelDialog owning its controller/focus node, with keyboard submit, replacing three copies - Map pins default to "Point of interest" again; "Share my location" uses the node name - Location shares honour the send cooldown and handle send errors Discovered-contact import/export (#436): - Per-entry decoding with imported/skipped/invalid counts; reject keys that are not 32 bytes; skip in-file duplicates and the radio's own key - Strip routing paths on import and stop exporting them; clamp future timestamps; enforce the discovered-contact cap; require a connection; 2 MB file limit - Export via in-memory XFile (fixes web); pass sharePositionOrigin for iPad (also GPX export and image save); macOS user-selected read-write entitlement for save dialogs Docs: cover share location, import/export, node scan, TCP reconnect, channel auto-resend options, arrival ordering, badge, password length and image limits. Translations for all new strings in every locale.
This commit is contained in:
commit
5fb67abdde
66 files changed
+3118
-168
No files matched your search
@@ -53,6 +53,98 @@ class ContactDiscoveryStore {
|
||||
await prefs.setString(keyFor, jsonEncode(jsonList));
|
||||
}
|
||||
|
||||
/// Exports [contacts] for sharing. Routing paths are left out: a path is
|
||||
/// the route from this radio and is meaningless on another one.
|
||||
String exportContactsJson(List<Contact> contacts) {
|
||||
final jsonList = contacts.map((contact) {
|
||||
final json = _toJson(contact);
|
||||
for (final key in _pathKeys) {
|
||||
json.remove(key);
|
||||
}
|
||||
return json;
|
||||
}).toList();
|
||||
return jsonEncode(jsonList);
|
||||
}
|
||||
|
||||
static const List<String> _pathKeys = [
|
||||
'pathLength',
|
||||
'path',
|
||||
'pathHashWidth',
|
||||
'pathOverride',
|
||||
'pathOverrideBytes',
|
||||
];
|
||||
|
||||
/// Merges an export file into [existingContacts], each entry on its own:
|
||||
/// a malformed entry is counted as invalid instead of failing the file.
|
||||
///
|
||||
/// Skips radio contacts ([knownContactKeys]), discovered contacts already
|
||||
/// present (they are fresher), duplicates within the file, and the
|
||||
/// connected radio's own key. Imported paths are dropped (they belong to the
|
||||
/// exporting radio) and timestamps are clamped to [now].
|
||||
///
|
||||
/// Throws [FormatException] when the file is not a JSON list.
|
||||
DiscoveredContactsImportResult importContactsJson({
|
||||
required String json,
|
||||
required List<Contact> existingContacts,
|
||||
required Set<String> knownContactKeys,
|
||||
String? selfPublicKeyHex,
|
||||
DateTime? now,
|
||||
}) {
|
||||
final Object? decoded;
|
||||
try {
|
||||
decoded = jsonDecode(json);
|
||||
} on FormatException {
|
||||
rethrow;
|
||||
} catch (e) {
|
||||
throw FormatException('Not valid JSON: $e');
|
||||
}
|
||||
if (decoded is! List) {
|
||||
throw const FormatException('Expected a list of contacts');
|
||||
}
|
||||
|
||||
final clampTo = now ?? DateTime.now();
|
||||
final seenKeys = {for (final c in existingContacts) c.publicKeyHex};
|
||||
var imported = 0;
|
||||
var skipped = 0;
|
||||
var invalid = 0;
|
||||
|
||||
for (final entry in decoded) {
|
||||
final Contact contact;
|
||||
try {
|
||||
if (entry is! Map<String, dynamic>) {
|
||||
throw const FormatException('Entry is not an object');
|
||||
}
|
||||
contact = _fromJson(entry, importing: true, clampTo: clampTo);
|
||||
if (contact.publicKey.length != _pubKeySize) {
|
||||
throw const FormatException('Public key must be 32 bytes');
|
||||
}
|
||||
} catch (e) {
|
||||
appLogger.warn('Skipping invalid imported discovered contact: $e');
|
||||
invalid++;
|
||||
continue;
|
||||
}
|
||||
|
||||
final keyHex = contact.publicKeyHex;
|
||||
if (keyHex == selfPublicKeyHex ||
|
||||
knownContactKeys.contains(keyHex) ||
|
||||
!seenKeys.add(keyHex)) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
existingContacts.add(contact);
|
||||
imported++;
|
||||
}
|
||||
|
||||
return DiscoveredContactsImportResult(
|
||||
total: decoded.length,
|
||||
imported: imported,
|
||||
skipped: skipped,
|
||||
invalid: invalid,
|
||||
);
|
||||
}
|
||||
|
||||
static const int _pubKeySize = 32;
|
||||
|
||||
Map<String, dynamic> _toJson(Contact contact) {
|
||||
return {
|
||||
'publicKey': base64Encode(contact.publicKey),
|
||||
@@ -71,25 +163,39 @@ class ContactDiscoveryStore {
|
||||
'lastSeen': contact.lastSeen.millisecondsSinceEpoch,
|
||||
'lastModified': contact.lastModified?.millisecondsSinceEpoch,
|
||||
'lastMessageAt': contact.lastMessageAt.millisecondsSinceEpoch,
|
||||
'isActive': contact.isActive,
|
||||
'rawPacket': contact.rawPacket != null
|
||||
? base64Encode(contact.rawPacket!)
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
Contact _fromJson(Map<String, dynamic> json) {
|
||||
final lastSeenMs = json['lastSeen'] as int? ?? 0;
|
||||
final lastMessageMs = json['lastMessageAt'] as int?;
|
||||
final lastModifiedMs = json['lastModified'] as int?;
|
||||
Contact _fromJson(
|
||||
Map<String, dynamic> json, {
|
||||
bool importing = false,
|
||||
DateTime? clampTo,
|
||||
}) {
|
||||
int? clampMs(int? ms) {
|
||||
if (ms == null || clampTo == null) return ms;
|
||||
final limit = clampTo.millisecondsSinceEpoch;
|
||||
return ms > limit ? limit : ms;
|
||||
}
|
||||
|
||||
final rawPathLength = json['pathLength'] as int? ?? -1;
|
||||
final rawPath = json['path'] != null
|
||||
final lastSeenMs = clampMs((json['lastSeen'] as num?)?.toInt()) ?? 0;
|
||||
final lastMessageMs = clampMs((json['lastMessageAt'] as num?)?.toInt());
|
||||
final lastModifiedMs = clampMs((json['lastModified'] as num?)?.toInt());
|
||||
|
||||
// An imported path is the exporting radio's route: never reuse it.
|
||||
final rawPathLength = importing ? -1 : json['pathLength'] as int? ?? -1;
|
||||
final rawPath = !importing && json['path'] != null
|
||||
? Uint8List.fromList(base64Decode(json['path'] as String))
|
||||
: Uint8List(0);
|
||||
|
||||
int decodedPathLength = rawPathLength;
|
||||
Uint8List decodedPath = rawPath;
|
||||
int? decodedPathHashWidth = json['pathHashWidth'] as int?;
|
||||
int? decodedPathHashWidth = importing
|
||||
? null
|
||||
: json['pathHashWidth'] as int?;
|
||||
|
||||
if (rawPathLength == 0xFF || rawPathLength < 0) {
|
||||
decodedPathLength = -1;
|
||||
@@ -120,8 +226,8 @@ class ContactDiscoveryStore {
|
||||
pathHashWidth:
|
||||
decodedPathHashWidth ??
|
||||
Contact.inferPathHashWidth(decodedPathLength, decodedPath.length),
|
||||
pathOverride: json['pathOverride'] as int?,
|
||||
pathOverrideBytes: json['pathOverrideBytes'] != null
|
||||
pathOverride: importing ? null : json['pathOverride'] as int?,
|
||||
pathOverrideBytes: !importing && json['pathOverrideBytes'] != null
|
||||
? Uint8List.fromList(
|
||||
base64Decode(json['pathOverrideBytes'] as String),
|
||||
)
|
||||
@@ -142,3 +248,32 @@ class ContactDiscoveryStore {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Outcome of [ContactDiscoveryStore.importContactsJson].
|
||||
class DiscoveredContactsImportResult {
|
||||
const DiscoveredContactsImportResult({
|
||||
required this.total,
|
||||
required this.imported,
|
||||
required this.skipped,
|
||||
required this.invalid,
|
||||
});
|
||||
|
||||
static const empty = DiscoveredContactsImportResult(
|
||||
total: 0,
|
||||
imported: 0,
|
||||
skipped: 0,
|
||||
invalid: 0,
|
||||
);
|
||||
|
||||
/// Entries in the file.
|
||||
final int total;
|
||||
|
||||
/// Entries added as new discovered contacts.
|
||||
final int imported;
|
||||
|
||||
/// Valid entries already known (contact, discovered, duplicate, or self).
|
||||
final int skipped;
|
||||
|
||||
/// Entries that could not be read.
|
||||
final int invalid;
|
||||
}
|
||||
Reference in new issue
Block a user