Extend user and session models

This commit is contained in:
Nutcake 2023-05-15 11:47:57 +02:00
parent 5e267b7ff0
commit b8b125f359
3 changed files with 58 additions and 7 deletions

View file

@ -8,4 +8,6 @@ class Config {
static const String neosHubUrl = "$apiBaseUrl/hub";
static const int messageCacheValiditySeconds = 90;
static const String latestCompatHash = "jnnkdwkBqGv5+jlf1u/k7A==";
}

View file

@ -118,22 +118,36 @@ enum OnlineStatus {
class UserStatus {
final OnlineStatus onlineStatus;
final DateTime lastStatusChange;
final int currentSessionAccessLevel;
final bool currentSessionHidden;
final bool currentHosting;
final Session currentSession;
final List<Session> activeSessions;
final String neosVersion;
final String outputDevice;
final bool isMobile;
final String compatibilityHash;
const UserStatus(
{required this.onlineStatus, required this.lastStatusChange, required this.currentSession, required this.activeSessions,
required this.neosVersion,
{required this.onlineStatus, required this.lastStatusChange, required this.currentSession,
required this.currentSessionAccessLevel, required this.currentSessionHidden, required this.currentHosting,
required this.activeSessions, required this.neosVersion, required this.outputDevice, required this.isMobile,
required this.compatibilityHash,
});
factory UserStatus.empty() =>
UserStatus(
onlineStatus: OnlineStatus.offline,
lastStatusChange: DateTime.now(),
activeSessions: [],
currentSessionAccessLevel: 0,
currentSessionHidden: false,
currentHosting: false,
currentSession: Session.none(),
activeSessions: [],
neosVersion: "",
outputDevice: "Unknown",
isMobile: false,
compatibilityHash: "",
);
factory UserStatus.fromMap(Map map) {
@ -142,9 +156,15 @@ class UserStatus {
return UserStatus(
onlineStatus: status,
lastStatusChange: DateTime.parse(map["lastStatusChange"]),
currentSessionAccessLevel: map["currentSessionAccessLevel"] ?? 0,
currentSessionHidden: map["currentSessionHidden"] ?? false,
currentHosting: map["currentHosting"] ?? false,
currentSession: Session.fromMap(map["currentSession"]),
activeSessions: (map["activeSessions"] as List? ?? []).map((e) => Session.fromMap(e)).toList(),
neosVersion: map["neosVersion"] ?? "",
outputDevice: map["outputDevice"] ?? "Unknown",
isMobile: map["isMobile"] ?? false,
compatibilityHash: map["compatabilityHash"] ?? ""
);
}
@ -152,24 +172,42 @@ class UserStatus {
return {
"onlineStatus": onlineStatus.index,
"lastStatusChange": lastStatusChange.toIso8601String(),
"currentSessionAccessLevel": currentSessionAccessLevel,
"currentSessionHidden": currentSessionHidden,
"currentHosting": currentHosting,
"currentSession": currentSession.isNone || shallow ? null : currentSession.toMap(),
"activeSessions": shallow ? [] : activeSessions.map((e) => e.toMap(),),
"activeSessions": shallow ? [] : activeSessions.map((e) => e.toMap(),).toList(),
"neosVersion": neosVersion,
"outputDevice": outputDevice,
"isMobile": isMobile,
"compatibilityHash": compatibilityHash,
};
}
UserStatus copyWith({
OnlineStatus? onlineStatus,
DateTime? lastStatusChange,
int? currentSessionAccessLevel,
bool? currentSessionHidden,
bool? currentHosting,
Session? currentSession,
List<Session>? activeSessions,
String? neosVersion
String? neosVersion,
String? outputDevice,
bool? isMobile,
String? compatibilityHash,
}) =>
UserStatus(
onlineStatus: onlineStatus ?? this.onlineStatus,
lastStatusChange: lastStatusChange ?? this.lastStatusChange,
currentSessionAccessLevel: currentSessionAccessLevel ?? this.currentSessionAccessLevel,
currentSessionHidden: currentSessionHidden ?? this.currentSessionHidden,
currentHosting: currentHosting ?? this.currentHosting,
currentSession: currentSession ?? this.currentSession,
activeSessions: activeSessions ?? this.activeSessions,
neosVersion: neosVersion ?? this.neosVersion,
outputDevice: outputDevice ?? this.outputDevice,
isMobile: isMobile ?? this.isMobile,
compatibilityHash: compatibilityHash ?? this.compatibilityHash,
);
}

View file

@ -66,13 +66,13 @@ class Session {
return {
"sessionId": id,
"name": name,
"sessionUsers": shallow ? [] : throw UnimplementedError(),
"sessionUsers": shallow ? [] : sessionUsers.map((e) => e.toMap()).toList(),
"thumbnail": thumbnail,
"maxUsers": maxUsers,
"hasEnded": hasEnded,
"isValid": isValid,
"description": description,
"tags": shallow ? [] : throw UnimplementedError(),
"tags": shallow ? [] : tags,
"headlessHost": headlessHost,
"hostUserId": hostUserId,
"hostUsername": hostUsername,
@ -80,6 +80,8 @@ class Session {
};
}
bool get isLive => !hasEnded && isValid;
}
@ -125,4 +127,13 @@ class SessionUser {
outputDevice: map["outputDevice"] ?? 0,
);
}
Map toMap() {
return {
"userID": id,
"username": username,
"isPresent": isPresent,
"outputDevice": outputDevice,
};
}
}