diff --git a/lib/matrix.dart b/lib/matrix.dart index 3e52aa84..f0ab4bbc 100644 --- a/lib/matrix.dart +++ b/lib/matrix.dart @@ -62,6 +62,7 @@ export 'src/utils/matrix_file.dart'; export 'src/utils/matrix_id_string_extension.dart'; export 'src/utils/matrix_localizations.dart'; export 'src/utils/native_implementations.dart'; +export 'src/utils/room_enums.dart'; export 'src/utils/room_member_change_type.dart'; export 'src/utils/push_notification.dart'; export 'src/utils/pushrule_evaluator.dart'; diff --git a/lib/src/room.dart b/lib/src/room.dart index 43bb4c0d..e26672fd 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -36,42 +36,12 @@ import 'package:matrix/src/utils/space_child.dart'; /// https://spec.matrix.org/v1.9/client-server-api/#size-limits 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 _guestAccessMap = { - GuestAccess.canJoin: 'can_join', - GuestAccess.forbidden: 'forbidden', -}; - -extension GuestAccessExtension on GuestAccess { - String get text => _guestAccessMap[this]!; -} - -const Map _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 = 'com.famedly.famedlysdk.message_sending_status'; const String fileSendingStatusKey = 'com.famedly.famedlysdk.file_sending_status'; -const String emptyRoomName = 'Empty chat'; - /// Represents a Matrix room. class Room { /// 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 /// keywords which are not implemented. JoinRules? get joinRules { - final joinRule = getState(EventTypes.RoomJoinRules)?.content['join_rule']; - return joinRule != null - ? JoinRules.values.firstWhereOrNull( - (r) => r.toString().replaceAll('JoinRules.', '') == joinRule) - : null; + final joinRulesString = + getState(EventTypes.RoomJoinRules)?.content.tryGet('join_rule'); + return JoinRules.values + .singleWhereOrNull((element) => element.text == joinRulesString); } /// 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 /// is absent, servers should act as if it is present and has the guest_access value "forbidden". GuestAccess get guestAccess { - final ga = getState(EventTypes.GuestAccess)?.content['guest_access']; - return ga != null - ? (_guestAccessMap.map((k, v) => MapEntry(v, k))[ga] ?? - GuestAccess.forbidden) - : GuestAccess.forbidden; + final guestAccessString = getState(EventTypes.GuestAccess) + ?.content + .tryGet('guest_access'); + return GuestAccess.values.singleWhereOrNull( + (element) => element.text == guestAccessString) ?? + GuestAccess.forbidden; } /// 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. HistoryVisibility? get historyVisibility { - final hv = - getState(EventTypes.HistoryVisibility)?.content['history_visibility']; - return hv != null - ? _historyVisibilityMap.map((k, v) => MapEntry(v, k))[hv] - : null; + final historyVisibilityString = getState(EventTypes.HistoryVisibility) + ?.content + .tryGet('history_visibility'); + return HistoryVisibility.values.singleWhereOrNull( + (element) => element.text == historyVisibilityString); } /// Changes the history visibility. You should check first if the user is able to change it. diff --git a/lib/src/utils/room_enums.dart b/lib/src/utils/room_enums.dart new file mode 100644 index 00000000..a38b8fad --- /dev/null +++ b/lib/src/utils/room_enums.dart @@ -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; +}