Fix some issues with parsing user profiles

This commit is contained in:
Nutcake 2023-05-15 11:58:03 +02:00
parent b8b125f359
commit 78179f14b5
2 changed files with 9 additions and 9 deletions

View file

@ -22,23 +22,23 @@ class PersonalProfile {
}); });
factory PersonalProfile.fromMap(Map map) { factory PersonalProfile.fromMap(Map map) {
final banExp = map["publicBanExpiration"];
return PersonalProfile( return PersonalProfile(
id: map["id"], id: map["id"] ?? "",
username: map["username"], username: map["username"] ?? "",
email: map["email"], email: map["email"] ?? "",
publicBanExpiration: banExp == null ? null : DateTime.parse(banExp), publicBanExpiration: DateTime.tryParse(map["publicBanExpiration"] ?? ""),
publicBanType: map["publicBanType"], publicBanType: map["publicBanType"],
storageQuotas: (map["storageQuotas"] as List).map((e) => StorageQuotas.fromMap(e)).toList(), storageQuotas: (map["storageQuotas"] as List).map((e) => StorageQuotas.fromMap(e)).toList(),
quotaBytesSource: (map["quotaBytesSources"] as Map).map((key, value) => MapEntry(key, value as int)), quotaBytesSource: (map["quotaBytesSources"] as Map).map((key, value) => MapEntry(key, value as int)),
usedBytes: map["usedBytes"], usedBytes: map["usedBytes"] ?? 0,
twoFactor: map["2fa_login"] ?? false, twoFactor: map["2fa_login"] ?? false,
isPatreonSupporter: map["patreonData"]?["isPatreonSupporter"] ?? false, isPatreonSupporter: map["patreonData"]?["isPatreonSupporter"] ?? false,
userProfile: UserProfile.fromMap(map["profile"]), userProfile: UserProfile.fromMap(map["profile"]),
); );
} }
int get maxBytes => (quotaBytesSource.values.maxOrNull ?? 0) + storageQuotas.map((e) => e.bytes).reduce((value, element) => value + element); int get maxBytes => (quotaBytesSource.values.maxOrNull ?? 0)
+ (storageQuotas.isEmpty ? 0 : storageQuotas.map((e) => e.bytes).reduce((value, element) => value + element));
} }
class StorageQuotas { class StorageQuotas {

View file

@ -5,8 +5,8 @@ class UserProfile {
factory UserProfile.empty() => UserProfile(iconUrl: ""); factory UserProfile.empty() => UserProfile(iconUrl: "");
factory UserProfile.fromMap(Map map) { factory UserProfile.fromMap(Map? map) {
return UserProfile(iconUrl: map["iconUrl"] ?? ""); return UserProfile(iconUrl: map?["iconUrl"] ?? "");
} }
Map toMap() { Map toMap() {