mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-12 17:00:28 +00:00
feat: Implement room login state management and enhance SAR message handling
This commit is contained in:
@@ -8,7 +8,8 @@ enum ContactType {
|
||||
none(0),
|
||||
chat(1),
|
||||
repeater(2),
|
||||
room(3);
|
||||
room(3),
|
||||
channel(99); // Virtual type for public channel (not from protocol)
|
||||
|
||||
const ContactType(this.value);
|
||||
final int value;
|
||||
@@ -28,6 +29,8 @@ enum ContactType {
|
||||
return 'Repeater';
|
||||
case ContactType.room:
|
||||
return 'Room';
|
||||
case ContactType.channel:
|
||||
return 'Channel';
|
||||
default:
|
||||
return 'Unknown';
|
||||
}
|
||||
@@ -75,6 +78,12 @@ class Contact {
|
||||
return publicKey.map((b) => b.toRadixString(16).padLeft(2, '0')).join('');
|
||||
}
|
||||
|
||||
/// Get public key prefix (first 6 bytes) for room login matching
|
||||
Uint8List get publicKeyPrefix {
|
||||
if (publicKey.length < 6) return publicKey;
|
||||
return publicKey.sublist(0, 6);
|
||||
}
|
||||
|
||||
/// Convert advLat/advLon to LatLng
|
||||
LatLng? get advertLocation {
|
||||
if (advLat == 0 && advLon == 0) return null;
|
||||
@@ -103,9 +112,12 @@ class Contact {
|
||||
/// Check if contact is a repeater
|
||||
bool get isRepeater => type == ContactType.repeater;
|
||||
|
||||
/// Check if contact is a room/channel
|
||||
/// Check if contact is a room (persistent storage)
|
||||
bool get isRoom => type == ContactType.room;
|
||||
|
||||
/// Check if contact is a channel (ephemeral broadcast)
|
||||
bool get isChannel => type == ContactType.channel;
|
||||
|
||||
/// Get last seen time
|
||||
DateTime get lastSeenTime {
|
||||
return DateTime.fromMillisecondsSinceEpoch(lastAdvert * 1000);
|
||||
|
||||
111
lib/models/room_login_state.dart
Normal file
111
lib/models/room_login_state.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
/// Represents the login state for a room
|
||||
class RoomLoginState {
|
||||
final Uint8List publicKeyPrefix;
|
||||
final bool isLoggedIn;
|
||||
final bool isAdmin;
|
||||
final int permissions;
|
||||
final int? tag;
|
||||
final DateTime? loginTime;
|
||||
final bool hasPassword; // Whether we have a saved password
|
||||
|
||||
const RoomLoginState({
|
||||
required this.publicKeyPrefix,
|
||||
this.isLoggedIn = false,
|
||||
this.isAdmin = false,
|
||||
this.permissions = 0,
|
||||
this.tag,
|
||||
this.loginTime,
|
||||
this.hasPassword = false,
|
||||
});
|
||||
|
||||
/// Create a logged-in state
|
||||
factory RoomLoginState.loggedIn({
|
||||
required Uint8List publicKeyPrefix,
|
||||
required int permissions,
|
||||
required bool isAdmin,
|
||||
required int tag,
|
||||
required bool hasPassword,
|
||||
}) {
|
||||
return RoomLoginState(
|
||||
publicKeyPrefix: publicKeyPrefix,
|
||||
isLoggedIn: true,
|
||||
isAdmin: isAdmin,
|
||||
permissions: permissions,
|
||||
tag: tag,
|
||||
loginTime: DateTime.now(),
|
||||
hasPassword: hasPassword,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create a logged-out state
|
||||
factory RoomLoginState.loggedOut({
|
||||
required Uint8List publicKeyPrefix,
|
||||
bool hasPassword = false,
|
||||
}) {
|
||||
return RoomLoginState(
|
||||
publicKeyPrefix: publicKeyPrefix,
|
||||
isLoggedIn: false,
|
||||
hasPassword: hasPassword,
|
||||
);
|
||||
}
|
||||
|
||||
/// Copy with modified fields
|
||||
RoomLoginState copyWith({
|
||||
Uint8List? publicKeyPrefix,
|
||||
bool? isLoggedIn,
|
||||
bool? isAdmin,
|
||||
int? permissions,
|
||||
int? tag,
|
||||
DateTime? loginTime,
|
||||
bool? hasPassword,
|
||||
}) {
|
||||
return RoomLoginState(
|
||||
publicKeyPrefix: publicKeyPrefix ?? this.publicKeyPrefix,
|
||||
isLoggedIn: isLoggedIn ?? this.isLoggedIn,
|
||||
isAdmin: isAdmin ?? this.isAdmin,
|
||||
permissions: permissions ?? this.permissions,
|
||||
tag: tag ?? this.tag,
|
||||
loginTime: loginTime ?? this.loginTime,
|
||||
hasPassword: hasPassword ?? this.hasPassword,
|
||||
);
|
||||
}
|
||||
|
||||
/// Get formatted public key prefix (e.g., "15:59:89:54:b4:d4")
|
||||
String get publicKeyPrefixHex {
|
||||
return publicKeyPrefix
|
||||
.map((b) => b.toRadixString(16).padLeft(2, '0'))
|
||||
.join(':');
|
||||
}
|
||||
|
||||
/// Get login duration if logged in
|
||||
Duration? get loginDuration {
|
||||
if (!isLoggedIn || loginTime == null) return null;
|
||||
return DateTime.now().difference(loginTime!);
|
||||
}
|
||||
|
||||
/// Get formatted login duration (e.g., "2h 15m ago")
|
||||
String? get loginDurationFormatted {
|
||||
final duration = loginDuration;
|
||||
if (duration == null) return null;
|
||||
|
||||
if (duration.inMinutes < 1) {
|
||||
return 'just now';
|
||||
} else if (duration.inMinutes < 60) {
|
||||
return '${duration.inMinutes}m ago';
|
||||
} else if (duration.inHours < 24) {
|
||||
final hours = duration.inHours;
|
||||
final minutes = duration.inMinutes % 60;
|
||||
return minutes > 0 ? '${hours}h ${minutes}m ago' : '${hours}h ago';
|
||||
} else {
|
||||
final days = duration.inDays;
|
||||
return '${days}d ago';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'RoomLoginState(prefix: $publicKeyPrefixHex, loggedIn: $isLoggedIn, admin: $isAdmin, hasPassword: $hasPassword)';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user