refactor: Use enhanced enums for room enums

This commit is contained in:
Krille 2024-05-16 07:34:40 +02:00
parent f637bcd4d9
commit 491313ae7d
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
3 changed files with 54 additions and 45 deletions

View File

@ -62,6 +62,7 @@ export 'src/utils/matrix_file.dart';
export 'src/utils/matrix_id_string_extension.dart'; export 'src/utils/matrix_id_string_extension.dart';
export 'src/utils/matrix_localizations.dart'; export 'src/utils/matrix_localizations.dart';
export 'src/utils/native_implementations.dart'; export 'src/utils/native_implementations.dart';
export 'src/utils/room_enums.dart';
export 'src/utils/room_member_change_type.dart'; export 'src/utils/room_member_change_type.dart';
export 'src/utils/push_notification.dart'; export 'src/utils/push_notification.dart';
export 'src/utils/pushrule_evaluator.dart'; export 'src/utils/pushrule_evaluator.dart';

View File

@ -36,42 +36,12 @@ import 'package:matrix/src/utils/space_child.dart';
/// https://spec.matrix.org/v1.9/client-server-api/#size-limits /// https://spec.matrix.org/v1.9/client-server-api/#size-limits
const int maxPDUSize = 60000; const int maxPDUSize = 60000;
enum PushRuleState { notify, mentionsOnly, dontNotify }
enum JoinRules { public, knock, invite, private }
enum GuestAccess { canJoin, forbidden }
enum HistoryVisibility { invited, joined, shared, worldReadable }
const Map<GuestAccess, String> _guestAccessMap = {
GuestAccess.canJoin: 'can_join',
GuestAccess.forbidden: 'forbidden',
};
extension GuestAccessExtension on GuestAccess {
String get text => _guestAccessMap[this]!;
}
const Map<HistoryVisibility, String> _historyVisibilityMap = {
HistoryVisibility.invited: 'invited',
HistoryVisibility.joined: 'joined',
HistoryVisibility.shared: 'shared',
HistoryVisibility.worldReadable: 'world_readable',
};
extension HistoryVisibilityExtension on HistoryVisibility {
String get text => _historyVisibilityMap[this]!;
}
const String messageSendingStatusKey = const String messageSendingStatusKey =
'com.famedly.famedlysdk.message_sending_status'; 'com.famedly.famedlysdk.message_sending_status';
const String fileSendingStatusKey = const String fileSendingStatusKey =
'com.famedly.famedlysdk.file_sending_status'; 'com.famedly.famedlysdk.file_sending_status';
const String emptyRoomName = 'Empty chat';
/// Represents a Matrix room. /// Represents a Matrix room.
class Room { class Room {
/// The full qualified Matrix ID for the room in the format '!localid:server.abc'. /// The full qualified Matrix ID for the room in the format '!localid:server.abc'.
@ -2111,11 +2081,10 @@ class Room {
/// to the room from someone already inside of the room. Currently, knock and private are reserved /// to the room from someone already inside of the room. Currently, knock and private are reserved
/// keywords which are not implemented. /// keywords which are not implemented.
JoinRules? get joinRules { JoinRules? get joinRules {
final joinRule = getState(EventTypes.RoomJoinRules)?.content['join_rule']; final joinRulesString =
return joinRule != null getState(EventTypes.RoomJoinRules)?.content.tryGet<String>('join_rule');
? JoinRules.values.firstWhereOrNull( return JoinRules.values
(r) => r.toString().replaceAll('JoinRules.', '') == joinRule) .singleWhereOrNull((element) => element.text == joinRulesString);
: null;
} }
/// Changes the join rules. You should check first if the user is able to change it. /// Changes the join rules. You should check first if the user is able to change it.
@ -2137,11 +2106,12 @@ class Room {
/// This event controls whether guest users are allowed to join rooms. If this event /// This event controls whether guest users are allowed to join rooms. If this event
/// is absent, servers should act as if it is present and has the guest_access value "forbidden". /// is absent, servers should act as if it is present and has the guest_access value "forbidden".
GuestAccess get guestAccess { GuestAccess get guestAccess {
final ga = getState(EventTypes.GuestAccess)?.content['guest_access']; final guestAccessString = getState(EventTypes.GuestAccess)
return ga != null ?.content
? (_guestAccessMap.map((k, v) => MapEntry(v, k))[ga] ?? .tryGet<String>('guest_access');
GuestAccess.forbidden) return GuestAccess.values.singleWhereOrNull(
: GuestAccess.forbidden; (element) => element.text == guestAccessString) ??
GuestAccess.forbidden;
} }
/// Changes the guest access. You should check first if the user is able to change it. /// Changes the guest access. You should check first if the user is able to change it.
@ -2162,11 +2132,11 @@ class Room {
/// This event controls whether a user can see the events that happened in a room from before they joined. /// This event controls whether a user can see the events that happened in a room from before they joined.
HistoryVisibility? get historyVisibility { HistoryVisibility? get historyVisibility {
final hv = final historyVisibilityString = getState(EventTypes.HistoryVisibility)
getState(EventTypes.HistoryVisibility)?.content['history_visibility']; ?.content
return hv != null .tryGet<String>('history_visibility');
? _historyVisibilityMap.map((k, v) => MapEntry(v, k))[hv] return HistoryVisibility.values.singleWhereOrNull(
: null; (element) => element.text == historyVisibilityString);
} }
/// Changes the history visibility. You should check first if the user is able to change it. /// Changes the history visibility. You should check first if the user is able to change it.

View File

@ -0,0 +1,38 @@
enum PushRuleState {
notify,
mentionsOnly,
dontNotify,
}
enum JoinRules {
public('public'),
knock('knock'),
invite('invite'),
private('private'),
restricted('restricted'),
knockRestricted('knock_restricted');
const JoinRules(this.text);
final String text;
}
enum GuestAccess {
canJoin('can_join'),
forbidden('forbidden');
const GuestAccess(this.text);
final String text;
}
enum HistoryVisibility {
invited('invited'),
joined('joined'),
shared('shared'),
worldReadable('world_readable');
const HistoryVisibility(this.text);
final String text;
}