OpenContacts/lib/models/session.dart

139 lines
4 KiB
Dart
Raw Normal View History

2023-05-12 12:31:05 -04:00
import 'package:contacts_plus_plus/string_formatter.dart';
2023-05-01 16:00:56 -04:00
class Session {
final String id;
final String name;
2023-05-12 12:31:05 -04:00
final FormatNode formattedName;
2023-05-01 16:00:56 -04:00
final List<SessionUser> sessionUsers;
final String thumbnail;
final int maxUsers;
final bool hasEnded;
final bool isValid;
final String description;
2023-05-12 12:31:05 -04:00
final FormatNode formattedDescription;
2023-05-01 16:00:56 -04:00
final List<String> tags;
final bool headlessHost;
final String hostUserId;
2023-05-02 16:50:35 -04:00
final String hostUsername;
final SessionAccessLevel accessLevel;
2023-05-01 16:00:56 -04:00
Session({required this.id, required this.name, required this.sessionUsers, required this.thumbnail,
required this.maxUsers, required this.hasEnded, required this.isValid, required this.description,
required this.tags, required this.headlessHost, required this.hostUserId, required this.hostUsername,
required this.accessLevel,
2023-05-12 12:31:05 -04:00
}) : formattedName = FormatNode.fromText(name), formattedDescription = FormatNode.fromText(description);
2023-05-01 16:00:56 -04:00
factory Session.none() {
return Session(
id: "",
name: "",
sessionUsers: const [],
thumbnail: "",
maxUsers: 0,
hasEnded: true,
isValid: false,
description: "",
tags: const [],
headlessHost: false,
hostUserId: "",
hostUsername: "",
accessLevel: SessionAccessLevel.unknown
);
}
bool get isNone => id.isEmpty && isValid == false;
factory Session.fromMap(Map? map) {
if (map == null) return Session.none();
2023-05-01 16:00:56 -04:00
return Session(
id: map["sessionId"],
name: map["name"],
sessionUsers: (map["sessionUsers"] as List? ?? []).map((entry) => SessionUser.fromMap(entry)).toList(),
thumbnail: map["thumbnail"] ?? "",
2023-05-02 16:50:35 -04:00
maxUsers: map["maxUsers"] ?? 0,
hasEnded: map["hasEnded"] ?? false,
isValid: map["isValid"] ?? true,
2023-05-01 16:00:56 -04:00
description: map["description"] ?? "",
tags: ((map["tags"] as List?) ?? []).map((e) => e.toString()).toList(),
2023-05-02 16:50:35 -04:00
headlessHost: map["headlessHost"] ?? false,
hostUserId: map["hostUserId"] ?? "",
2023-05-02 16:50:35 -04:00
hostUsername: map["hostUsername"] ?? "",
accessLevel: SessionAccessLevel.fromName(map["accessLevel"]),
2023-05-01 16:00:56 -04:00
);
}
Map toMap({bool shallow=false}) {
return {
"sessionId": id,
"name": name,
2023-05-15 05:47:57 -04:00
"sessionUsers": shallow ? [] : sessionUsers.map((e) => e.toMap()).toList(),
"thumbnail": thumbnail,
"maxUsers": maxUsers,
"hasEnded": hasEnded,
"isValid": isValid,
"description": description,
2023-05-15 05:47:57 -04:00
"tags": shallow ? [] : tags,
"headlessHost": headlessHost,
"hostUserId": hostUserId,
"hostUsername": hostUsername,
"accessLevel": accessLevel.name, // This probably wont work, the API usually expects integers.
};
}
2023-05-15 05:47:57 -04:00
2023-05-01 16:00:56 -04:00
bool get isLive => !hasEnded && isValid;
}
enum SessionAccessLevel {
unknown,
private,
friends,
friendsOfFriends,
anyone;
static const _readableNamesMap = {
SessionAccessLevel.unknown: "Unknown",
SessionAccessLevel.private: "Private",
SessionAccessLevel.friends: "Contacts",
SessionAccessLevel.friendsOfFriends: "Contacts+",
SessionAccessLevel.anyone: "Anyone",
};
factory SessionAccessLevel.fromName(String? name) {
return SessionAccessLevel.values.firstWhere((element) => element.name.toLowerCase() == name?.toLowerCase(),
orElse: () => SessionAccessLevel.unknown,
);
}
String toReadableString() {
return SessionAccessLevel._readableNamesMap[this] ?? "Unknown";
}
}
2023-05-01 16:00:56 -04:00
class SessionUser {
final String id;
final String username;
final bool isPresent;
final int outputDevice;
SessionUser({required this.id, required this.username, required this.isPresent, required this.outputDevice});
factory SessionUser.fromMap(Map map) {
return SessionUser(
id: map["userID"] ?? "",
username: map["username"] ?? "Unknown",
isPresent: map["isPresent"] ?? false,
outputDevice: map["outputDevice"] ?? 0,
2023-05-01 16:00:56 -04:00
);
}
2023-05-15 05:47:57 -04:00
Map toMap() {
return {
"userID": id,
"username": username,
"isPresent": isPresent,
"outputDevice": outputDevice,
};
}
2023-05-01 16:00:56 -04:00
}