OpenContacts/lib/models/users/user_status.dart

168 lines
5.5 KiB
Dart
Raw Normal View History

import 'package:intl/intl.dart';
import 'package:recon/config.dart';
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,
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,
);
}
}
class UserStatus {
final String userId;
final OnlineStatus onlineStatus;
final DateTime lastStatusChange;
2023-10-03 12:20:02 -04:00
final DateTime lastPresenceTimestamp;
final String userSessionId;
final int currentSessionIndex;
final List<SessionMetadata> sessions;
2023-09-29 09:33:43 -04:00
final String appVersion;
final String outputDevice;
final bool isMobile;
2023-10-03 12:20:02 -04:00
final bool isPresent;
final String compatibilityHash;
final String hashSalt;
2023-10-03 12:20:02 -04:00
final UserSessionType sessionType;
final List<Session> decodedSessions;
2023-09-29 09:33:43 -04:00
const UserStatus({
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,
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,
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-10-03 12:20:02 -04:00
factory UserStatus.initial() =>
UserStatus.empty().copyWith(
compatibilityHash: Config.latestCompatHash,
onlineStatus: OnlineStatus.online,
hashSalt: CryptoHelper.cryptoToken(),
outputDevice: "Unknown",
2023-10-03 12:20:02 -04:00
userSessionId: const Uuid().v4().toString(),
sessionType: UserSessionType.chatClient,
isPresent: true,
);
2023-10-03 12:20:02 -04:00
factory UserStatus.empty() =>
UserStatus(
userId: "",
onlineStatus: OnlineStatus.offline,
lastStatusChange: DateTime.now(),
2023-10-03 12:20:02 -04:00
lastPresenceTimestamp: DateTime.now(),
userSessionId: "",
currentSessionIndex: -1,
sessions: [],
2023-09-29 09:33:43 -04:00
appVersion: "",
outputDevice: "Unknown",
isMobile: false,
2023-10-03 12:20:02 -04:00
isPresent: false,
compatibilityHash: "",
hashSalt: "",
2023-10-03 12:20:02 -04:00
sessionType: UserSessionType.unknown
);
factory UserStatus.fromMap(Map map) {
2023-09-29 09:33:43 -04:00
final statusString = map["onlineStatus"].toString();
final status = OnlineStatus.fromString(statusString);
return UserStatus(
userId: map["userId"] ?? "",
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,
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,
compatibilityHash: map["compatibilityHash"] ?? "",
hashSalt: map["hashSalt"] ?? "",
2023-10-03 12:20:02 -04:00
sessionType: UserSessionType.fromString(map["sessionType"])
);
}
Map toMap({bool shallow = false}) {
return {
"userId": userId,
"onlineStatus": onlineStatus.index,
"lastStatusChange": lastStatusChange.toIso8601String(),
2023-10-03 12:20:02 -04:00
"isPresent": isPresent,
"lastPresenceTimestamp": lastPresenceTimestamp.toIso8601String(),
"userSessionId": userSessionId,
"currentSessionIndex": currentSessionIndex,
"sessions": shallow
2023-09-29 09:33:43 -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,
"outputDevice": outputDevice,
"isMobile": isMobile,
"compatibilityHash": compatibilityHash,
"sessionType": toBeginningOfSentenceCase(sessionType.name)
};
}
UserStatus copyWith({
String? userId,
OnlineStatus? onlineStatus,
DateTime? lastStatusChange,
2023-10-03 12:20:02 -04:00
DateTime? lastPresenceTimestamp,
bool? isPresent,
String? userSessionId,
int? currentSessionIndex,
List<SessionMetadata>? sessions,
2023-09-30 08:20:08 -04:00
String? appVersion,
String? outputDevice,
bool? isMobile,
String? compatibilityHash,
String? hashSalt,
2023-10-03 12:20:02 -04:00
UserSessionType? sessionType,
2023-10-10 03:53:34 -04:00
List<Session>? decodedSessions,
}) =>
UserStatus(
userId: userId ?? this.userId,
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,
currentSessionIndex: currentSessionIndex ?? this.currentSessionIndex,
sessions: sessions ?? this.sessions,
2023-09-30 08:20:08 -04:00
appVersion: appVersion ?? this.appVersion,
outputDevice: outputDevice ?? this.outputDevice,
isMobile: isMobile ?? this.isMobile,
compatibilityHash: compatibilityHash ?? this.compatibilityHash,
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-09-29 09:33:43 -04:00
}