2023-10-12 12:01:15 -04:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:recon/config.dart';
|
2023-10-03 13:00:59 -04:00
|
|
|
import 'package:recon/crypto_helper.dart';
|
|
|
|
import 'package:recon/models/session.dart';
|
|
|
|
import 'package:recon/models/session_metadata.dart';
|
|
|
|
import 'package:recon/models/users/online_status.dart';
|
2023-10-03 12:20:02 -04:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
enum UserSessionType
|
|
|
|
{
|
|
|
|
unknown,
|
|
|
|
graphicalClient,
|
|
|
|
chatClient,
|
|
|
|
headless,
|
2023-10-12 12:01:15 -04:00
|
|
|
bot;
|
2023-10-03 12:20:02 -04:00
|
|
|
|
|
|
|
factory UserSessionType.fromString(String? text) {
|
|
|
|
return UserSessionType.values.firstWhere((element) => element.name.toLowerCase() == text?.toLowerCase(),
|
|
|
|
orElse: () => UserSessionType.unknown,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 14:16:23 -04:00
|
|
|
|
|
|
|
class UserStatus {
|
2023-10-12 12:01:15 -04:00
|
|
|
final String userId;
|
2023-05-29 14:16:23 -04:00
|
|
|
final OnlineStatus onlineStatus;
|
|
|
|
final DateTime lastStatusChange;
|
2023-10-03 12:20:02 -04:00
|
|
|
final DateTime lastPresenceTimestamp;
|
|
|
|
final String userSessionId;
|
2023-10-01 15:14:38 -04:00
|
|
|
final int currentSessionIndex;
|
|
|
|
final List<SessionMetadata> sessions;
|
2023-09-29 09:33:43 -04:00
|
|
|
final String appVersion;
|
2023-05-29 14:16:23 -04:00
|
|
|
final String outputDevice;
|
|
|
|
final bool isMobile;
|
2023-10-03 12:20:02 -04:00
|
|
|
final bool isPresent;
|
2023-05-29 14:16:23 -04:00
|
|
|
final String compatibilityHash;
|
2023-10-01 15:14:38 -04:00
|
|
|
final String hashSalt;
|
2023-10-03 12:20:02 -04:00
|
|
|
final UserSessionType sessionType;
|
|
|
|
final List<Session> decodedSessions;
|
2023-05-29 14:16:23 -04:00
|
|
|
|
2023-09-29 09:33:43 -04:00
|
|
|
const UserStatus({
|
2023-10-12 12:01:15 -04:00
|
|
|
required this.userId,
|
2023-09-29 09:33:43 -04:00
|
|
|
required this.onlineStatus,
|
|
|
|
required this.lastStatusChange,
|
2023-10-03 12:20:02 -04:00
|
|
|
required this.lastPresenceTimestamp,
|
|
|
|
required this.userSessionId,
|
2023-10-01 15:14:38 -04:00
|
|
|
required this.currentSessionIndex,
|
|
|
|
required this.sessions,
|
2023-09-29 09:33:43 -04:00
|
|
|
required this.appVersion,
|
|
|
|
required this.outputDevice,
|
|
|
|
required this.isMobile,
|
2023-10-03 12:20:02 -04:00
|
|
|
required this.isPresent,
|
2023-09-29 09:33:43 -04:00
|
|
|
required this.compatibilityHash,
|
2023-10-01 15:14:38 -04:00
|
|
|
required this.hashSalt,
|
2023-10-03 12:20:02 -04:00
|
|
|
required this.sessionType,
|
|
|
|
this.decodedSessions = const []
|
2023-09-29 09:33:43 -04:00
|
|
|
});
|
2023-05-29 14:16:23 -04:00
|
|
|
|
2023-10-03 12:20:02 -04:00
|
|
|
factory UserStatus.initial() =>
|
|
|
|
UserStatus.empty().copyWith(
|
2023-10-12 12:01:15 -04:00
|
|
|
compatibilityHash: Config.latestCompatHash,
|
2023-10-01 15:14:38 -04:00
|
|
|
onlineStatus: OnlineStatus.online,
|
|
|
|
hashSalt: CryptoHelper.cryptoToken(),
|
2023-10-12 12:01:15 -04:00
|
|
|
outputDevice: "Unknown",
|
2023-10-03 12:20:02 -04:00
|
|
|
userSessionId: const Uuid().v4().toString(),
|
|
|
|
sessionType: UserSessionType.chatClient,
|
2023-10-12 12:01:15 -04:00
|
|
|
isPresent: true,
|
2023-10-01 15:14:38 -04:00
|
|
|
);
|
|
|
|
|
2023-10-03 12:20:02 -04:00
|
|
|
factory UserStatus.empty() =>
|
|
|
|
UserStatus(
|
2023-10-12 12:01:15 -04:00
|
|
|
userId: "",
|
2023-05-29 14:16:23 -04:00
|
|
|
onlineStatus: OnlineStatus.offline,
|
|
|
|
lastStatusChange: DateTime.now(),
|
2023-10-03 12:20:02 -04:00
|
|
|
lastPresenceTimestamp: DateTime.now(),
|
|
|
|
userSessionId: "",
|
2023-10-01 15:14:38 -04:00
|
|
|
currentSessionIndex: -1,
|
|
|
|
sessions: [],
|
2023-09-29 09:33:43 -04:00
|
|
|
appVersion: "",
|
2023-05-29 14:16:23 -04:00
|
|
|
outputDevice: "Unknown",
|
|
|
|
isMobile: false,
|
2023-10-03 12:20:02 -04:00
|
|
|
isPresent: false,
|
2023-05-29 14:16:23 -04:00
|
|
|
compatibilityHash: "",
|
2023-10-01 15:14:38 -04:00
|
|
|
hashSalt: "",
|
2023-10-03 12:20:02 -04:00
|
|
|
sessionType: UserSessionType.unknown
|
2023-05-29 14:16:23 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
factory UserStatus.fromMap(Map map) {
|
2023-09-29 09:33:43 -04:00
|
|
|
final statusString = map["onlineStatus"].toString();
|
2023-05-29 14:16:23 -04:00
|
|
|
final status = OnlineStatus.fromString(statusString);
|
|
|
|
return UserStatus(
|
2023-10-12 12:01:15 -04:00
|
|
|
userId: map["userId"] ?? "",
|
2023-10-01 15:14:38 -04:00
|
|
|
onlineStatus: status,
|
2023-10-03 12:20:02 -04:00
|
|
|
lastStatusChange: DateTime.tryParse(map["lastStatusChange"] ?? "") ?? DateTime.now(),
|
|
|
|
lastPresenceTimestamp: DateTime.tryParse(map["lastPresenceTimestamp"] ?? "") ?? DateTime.now(),
|
|
|
|
userSessionId: map["userSessionId"] ?? "",
|
|
|
|
isPresent: map["isPresent"] ?? false,
|
2023-10-01 15:14:38 -04:00
|
|
|
currentSessionIndex: map["currentSessionIndex"] ?? -1,
|
|
|
|
sessions: (map["sessions"] as List? ?? []).map((e) => SessionMetadata.fromMap(e)).toList(),
|
|
|
|
appVersion: map["appVersion"] ?? "",
|
|
|
|
outputDevice: map["outputDevice"] ?? "Unknown",
|
|
|
|
isMobile: map["isMobile"] ?? false,
|
2023-10-12 12:01:15 -04:00
|
|
|
compatibilityHash: map["compatibilityHash"] ?? "",
|
2023-10-01 15:14:38 -04:00
|
|
|
hashSalt: map["hashSalt"] ?? "",
|
2023-10-03 12:20:02 -04:00
|
|
|
sessionType: UserSessionType.fromString(map["sessionType"])
|
2023-10-01 15:14:38 -04:00
|
|
|
);
|
2023-05-29 14:16:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap({bool shallow = false}) {
|
|
|
|
return {
|
2023-10-12 12:01:15 -04:00
|
|
|
"userId": userId,
|
2023-05-29 14:16:23 -04:00
|
|
|
"onlineStatus": onlineStatus.index,
|
|
|
|
"lastStatusChange": lastStatusChange.toIso8601String(),
|
2023-10-03 12:20:02 -04:00
|
|
|
"isPresent": isPresent,
|
|
|
|
"lastPresenceTimestamp": lastPresenceTimestamp.toIso8601String(),
|
|
|
|
"userSessionId": userSessionId,
|
2023-10-01 15:14:38 -04:00
|
|
|
"currentSessionIndex": currentSessionIndex,
|
|
|
|
"sessions": shallow
|
2023-09-29 09:33:43 -04:00
|
|
|
? []
|
2023-10-01 15:14:38 -04:00
|
|
|
: sessions
|
2023-10-03 12:20:02 -04:00
|
|
|
.map(
|
|
|
|
(e) => e.toMap(),
|
|
|
|
)
|
|
|
|
.toList(),
|
2023-09-30 06:22:32 -04:00
|
|
|
"appVersion": appVersion,
|
2023-05-29 14:16:23 -04:00
|
|
|
"outputDevice": outputDevice,
|
|
|
|
"isMobile": isMobile,
|
|
|
|
"compatibilityHash": compatibilityHash,
|
2023-10-12 12:01:15 -04:00
|
|
|
"sessionType": toBeginningOfSentenceCase(sessionType.name)
|
2023-05-29 14:16:23 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
UserStatus copyWith({
|
2023-10-12 12:01:15 -04:00
|
|
|
String? userId,
|
2023-05-29 14:16:23 -04:00
|
|
|
OnlineStatus? onlineStatus,
|
|
|
|
DateTime? lastStatusChange,
|
2023-10-03 12:20:02 -04:00
|
|
|
DateTime? lastPresenceTimestamp,
|
|
|
|
bool? isPresent,
|
|
|
|
String? userSessionId,
|
2023-10-01 15:14:38 -04:00
|
|
|
int? currentSessionIndex,
|
|
|
|
List<SessionMetadata>? sessions,
|
2023-09-30 08:20:08 -04:00
|
|
|
String? appVersion,
|
2023-05-29 14:16:23 -04:00
|
|
|
String? outputDevice,
|
|
|
|
bool? isMobile,
|
|
|
|
String? compatibilityHash,
|
2023-10-01 15:14:38 -04:00
|
|
|
String? hashSalt,
|
2023-10-03 12:20:02 -04:00
|
|
|
UserSessionType? sessionType,
|
2023-10-10 03:53:34 -04:00
|
|
|
List<Session>? decodedSessions,
|
2023-05-29 14:16:23 -04:00
|
|
|
}) =>
|
|
|
|
UserStatus(
|
2023-10-12 12:01:15 -04:00
|
|
|
userId: userId ?? this.userId,
|
2023-05-29 14:16:23 -04:00
|
|
|
onlineStatus: onlineStatus ?? this.onlineStatus,
|
|
|
|
lastStatusChange: lastStatusChange ?? this.lastStatusChange,
|
2023-10-03 12:20:02 -04:00
|
|
|
lastPresenceTimestamp: lastPresenceTimestamp ?? this.lastPresenceTimestamp,
|
|
|
|
isPresent: isPresent ?? this.isPresent,
|
|
|
|
userSessionId: userSessionId ?? this.userSessionId,
|
2023-10-01 15:14:38 -04:00
|
|
|
currentSessionIndex: currentSessionIndex ?? this.currentSessionIndex,
|
|
|
|
sessions: sessions ?? this.sessions,
|
2023-09-30 08:20:08 -04:00
|
|
|
appVersion: appVersion ?? this.appVersion,
|
2023-05-29 14:16:23 -04:00
|
|
|
outputDevice: outputDevice ?? this.outputDevice,
|
|
|
|
isMobile: isMobile ?? this.isMobile,
|
|
|
|
compatibilityHash: compatibilityHash ?? this.compatibilityHash,
|
2023-10-01 15:14:38 -04:00
|
|
|
hashSalt: hashSalt ?? this.hashSalt,
|
2023-10-03 12:20:02 -04:00
|
|
|
sessionType: sessionType ?? this.sessionType,
|
2023-10-10 03:53:34 -04:00
|
|
|
decodedSessions: decodedSessions ?? this.decodedSessions,
|
2023-05-29 14:16:23 -04:00
|
|
|
);
|
2023-09-29 09:33:43 -04:00
|
|
|
}
|