ente/lib/models/user_details.dart

218 lines
5.4 KiB
Dart
Raw Normal View History

2022-12-27 11:35:15 +00:00
import 'dart:convert';
import 'dart:math';
import 'package:collection/collection.dart';
2023-08-25 04:39:30 +00:00
import 'package:photos/models/file/file_type.dart';
2021-07-28 14:08:27 +00:00
import 'package:photos/models/subscription.dart';
2022-12-13 14:53:58 +00:00
class UserDetails {
2021-07-28 14:08:27 +00:00
final String email;
final int usage;
final int fileCount;
2023-03-09 10:27:43 +00:00
final int storageBonus;
2021-07-28 14:08:27 +00:00
final int sharedCollectionsCount;
final Subscription subscription;
final FamilyData? familyData;
2023-08-04 04:24:54 +00:00
final ProfileData? profileData;
2021-07-28 14:08:27 +00:00
const UserDetails(
2021-07-28 14:08:27 +00:00
this.email,
this.usage,
this.fileCount,
2023-03-09 10:27:43 +00:00
this.storageBonus,
2021-07-28 14:08:27 +00:00
this.sharedCollectionsCount,
this.subscription,
this.familyData,
2023-08-04 04:24:54 +00:00
this.profileData,
2021-07-28 14:08:27 +00:00
);
2022-04-20 20:14:44 +00:00
bool isPartOfFamily() {
return familyData?.members?.isNotEmpty ?? false;
}
bool isFamilyAdmin() {
assert(isPartOfFamily(), "verify user is part of family before calling");
final FamilyMember currentUserMember = familyData!.members!
.firstWhere((element) => element.email.trim() == email.trim());
2022-04-20 20:14:44 +00:00
return currentUserMember.isAdmin;
}
// getFamilyOrPersonalUsage will return total usage for family if user
// belong to family group. Otherwise, it will return storage consumed by
// current user
int getFamilyOrPersonalUsage() {
return isPartOfFamily() ? familyData!.getTotalUsage() : usage;
}
int getFreeStorage() {
2023-07-02 04:39:59 +00:00
return max(getTotalStorage() - getFamilyOrPersonalUsage(), 0);
}
2023-07-02 04:39:59 +00:00
// getTotalStorage will return total storage available including the
// storage bonus
2022-05-02 18:57:50 +00:00
int getTotalStorage() {
return (isPartOfFamily() ? familyData!.storage : subscription.storage) +
storageBonus;
2022-05-02 18:57:50 +00:00
}
2021-07-28 14:08:27 +00:00
factory UserDetails.fromMap(Map<String, dynamic> map) {
return UserDetails(
map['email'] as String,
map['usage'] as int,
(map['fileCount'] ?? 0) as int,
2023-03-09 10:27:43 +00:00
(map['storageBonus'] ?? 0) as int,
(map['sharedCollectionsCount'] ?? 0) as int,
2021-07-28 14:08:27 +00:00
Subscription.fromMap(map['subscription']),
FamilyData.fromMap(map['familyData']),
2023-08-04 04:24:54 +00:00
ProfileData.fromJson(map['profileData']),
2021-07-28 14:08:27 +00:00
);
}
2022-12-27 11:35:15 +00:00
Map<String, dynamic> toMap() {
return {
'email': email,
'usage': usage,
'fileCount': fileCount,
2023-03-09 10:27:43 +00:00
'storageBonus': storageBonus,
2022-12-27 11:35:15 +00:00
'sharedCollectionsCount': sharedCollectionsCount,
'subscription': subscription.toMap(),
'familyData': familyData?.toMap(),
2023-08-04 04:24:54 +00:00
'profileData': profileData?.toJson(),
2022-12-27 11:35:15 +00:00
};
}
String toJson() => json.encode(toMap());
factory UserDetails.fromJson(String source) =>
UserDetails.fromMap(json.decode(source));
}
class FamilyMember {
final String email;
final int usage;
final String id;
final bool isAdmin;
2022-12-27 11:35:15 +00:00
FamilyMember(
this.email,
this.usage,
this.id,
this.isAdmin,
);
factory FamilyMember.fromMap(Map<String, dynamic> map) {
return FamilyMember(
(map['email'] ?? '') as String,
map['usage'] as int,
map['id'] as String,
map['isAdmin'] as bool,
);
}
2022-12-27 11:35:15 +00:00
Map<String, dynamic> toMap() {
return {
'email': email,
'usage': usage,
'id': id,
'isAdmin': isAdmin,
};
}
String toJson() => json.encode(toMap());
factory FamilyMember.fromJson(String source) =>
FamilyMember.fromMap(json.decode(source));
}
2023-08-04 04:24:54 +00:00
class ProfileData {
bool canDisableEmailMFA;
bool isEmailMFAEnabled;
bool isTwoFactorEnabled;
// Constructor with default values
ProfileData({
this.canDisableEmailMFA = false,
this.isEmailMFAEnabled = false,
this.isTwoFactorEnabled = false,
});
// Factory method to create ProfileData instance from JSON
factory ProfileData.fromJson(Map<String, dynamic>? json) {
return ProfileData(
canDisableEmailMFA: json?['canDisableEmailMFA'] ?? false,
isEmailMFAEnabled: json?['isEmailMFAEnabled'] ?? false,
isTwoFactorEnabled: json?['isTwoFactorEnabled'] ?? false,
);
}
// Method to convert ProfileData instance to JSON
Map<String, dynamic> toJson() {
return {
'canDisableEmailMFA': canDisableEmailMFA,
'isEmailMFAEnabled': isEmailMFAEnabled,
'isTwoFactorEnabled': isTwoFactorEnabled,
};
}
String toJsonString() => json.encode(toJson());
}
class FamilyData {
final List<FamilyMember>? members;
// Storage available based on the family plan
final int storage;
final int expiryTime;
2022-12-27 11:35:15 +00:00
FamilyData(
this.members,
this.storage,
this.expiryTime,
);
int getTotalUsage() {
return members!.map((e) => e.usage).toList().sum;
}
static fromMap(Map<String, dynamic>? map) {
if (map == null) return null;
assert(map['members'] != null && map['members'].length >= 0);
final members = List<FamilyMember>.from(
2022-06-11 08:23:52 +00:00
map['members'].map((x) => FamilyMember.fromMap(x)),
);
return FamilyData(
members,
map['storage'] as int,
map['expiryTime'] as int,
);
}
2022-12-27 11:35:15 +00:00
Map<String, dynamic> toMap() {
return {
'members': members?.map((x) => x.toMap()).toList(),
'storage': storage,
'expiryTime': expiryTime,
};
}
String toJson() => json.encode(toMap());
factory FamilyData.fromJson(String source) =>
FamilyData.fromMap(json.decode(source));
2021-07-28 14:08:27 +00:00
}
2022-10-21 05:44:14 +00:00
class FilesCount {
final Map<FileType, int> filesCount;
2022-10-21 05:44:14 +00:00
FilesCount(this.filesCount);
int get total =>
images + videos + livePhotos + (filesCount[getInt(FileType.other)] ?? 0);
2022-10-21 05:44:14 +00:00
int get photos => images + livePhotos;
int get images => filesCount[FileType.image] ?? 0;
2022-10-21 05:44:14 +00:00
int get videos => filesCount[FileType.video] ?? 0;
2022-10-21 05:44:14 +00:00
int get livePhotos => filesCount[FileType.livePhoto] ?? 0;
2022-10-21 05:44:14 +00:00
}