Browse Source

refactor(nextcloud): Cleanup spreed helpers

Signed-off-by: jld3103 <jld3103yt@gmail.com>
pull/1113/head
jld3103 1 year ago
parent
commit
1f897fc46e
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 101
      packages/nextcloud/lib/src/helpers/spreed.dart
  2. 22
      packages/nextcloud/test/spreed_test.dart

101
packages/nextcloud/lib/src/helpers/spreed.dart

@ -23,7 +23,7 @@ extension SpreedVersionSupported on spreed.Client {
/// Conversation types.
///
/// Use [value] to get the integer representation that is used in the API.
/// See https://github.com/nextcloud/spreed/blob/master/lib/Room.php.
/// See https://github.com/nextcloud/spreed/blob/main/lib/Room.php.
enum RoomType {
/// Room between two participants.
oneToOne,
@ -45,12 +45,22 @@ enum RoomType {
/// Integer representation of the [ParticipantType].
int get value => index + 1;
/// Converts the integer [value] representation of a [RoomType] into a [RoomType].
static RoomType fromValue(final int value) => RoomType.values[value - 1];
/// Whether the room is only with one other user.
bool get isSingleUser => switch (this) {
RoomType.oneToOne || RoomType.changelog || RoomType.oneToOneFormer || RoomType.noteToSelf => true,
_ => false,
};
}
/// Types of chat messages.
///
/// Use `name` to get the string representation that is used in the API.
/// See https://github.com/nextcloud/spreed/blob/master/lib/Chat/ChatManager.php.
/// Use [EnumName.name] to get the string representation that is used in the API.
/// Use [EnumByName.byName] to convert the string representation into a [MessageType].
/// See https://github.com/nextcloud/spreed/blob/main/lib/Chat/ChatManager.php.
enum MessageType {
/// Message.
comment,
@ -79,8 +89,9 @@ enum MessageType {
/// Actor types of chat messages.
///
/// Use `name` to get the string representation that is used in the API.
/// See https://github.com/nextcloud/spreed/blob/master/lib/Model/Attendee.php.
/// Use [EnumName.name] to get the string representation that is used in the API.
/// Use [EnumByName.byName] to convert the string representation into a [ActorType].
/// See https://github.com/nextcloud/spreed/blob/main/lib/Model/Attendee.php.
enum ActorType {
/// Logged-in users.
users,
@ -114,7 +125,7 @@ enum ActorType {
/// Participant types.
///
/// Use [value] to get the integer representation that is used in the API.
/// See https://github.com/nextcloud/spreed/blob/master/lib/Participant.php.
/// See https://github.com/nextcloud/spreed/blob/main/lib/Participant.php.
enum ParticipantType {
/// Owner.
owner,
@ -136,15 +147,18 @@ enum ParticipantType {
/// Integer representation of the [ParticipantType].
int get value => index + 1;
/// Converts the integer [value] representation of a [ParticipantType] into a [ParticipantType].
static ParticipantType fromValue(final int value) => ParticipantType.values[value - 1];
}
/// Attendee permissions.
///
/// Use [fromValue] to convert the integer representation into this enum representation.
/// Use [value] to get the integer representation that is used in the API.
/// Use [ParticipantPermissionsValue.value] to convert multiple [ParticipantPermission] into the integer representation.
/// Use [EnumByBinary.byBinary] to convert the binary representation into multiple [ParticipantPermission]s.
/// Use [EnumBinary.binary] to get the binary representation that is used in the API.
/// Use [EnumCollectionBinary.binary] to convert multiple [ParticipantPermission]s into the binary representation.
///
/// See https://github.com/nextcloud/spreed/blob/master/lib/Model/Attendee.php.
/// See https://github.com/nextcloud/spreed/blob/main/lib/Model/Attendee.php.
enum ParticipantPermission {
/// Default permissions.
$default,
@ -172,32 +186,71 @@ enum ParticipantPermission {
/// Can post chat message, share items and do reactions.
canSendMessageAndShareAndReact;
}
/// Participant in-call flags.
///
/// Use [EnumByBinary.byBinary] to convert the binary representation into multiple [ParticipantInCallFlag]s.
/// Use [EnumBinary.binary] to get the binary representation that is used in the API.
/// Use [EnumCollectionBinary.binary] to convert multiple [ParticipantInCallFlag]s into the binary representation.
///
/// See https://github.com/nextcloud/spreed/blob/main/lib/Participant.php.
enum ParticipantInCallFlag {
/// Not connected to the call.
disconnected,
/// Connected to the call.
inCall,
/// Integer representation of the [ParticipantPermission].
int get value => index == 0 ? 0 : 1 << (index - 1);
/// Connected to the call with audio.
providesAudio,
/// Converts the integer representation of multiple [ParticipantPermission]s to the corresponding [ParticipantPermission]s.
static Set<ParticipantPermission> fromValue(final int value) {
final permissions = <ParticipantPermission>{};
/// Connected to the call with video.
providesVideo,
/// Connected to the call using SIP dial-in.
sipDialIn;
}
/// An extension to convert the binary representation of an enum into enum values.
///
/// See [EnumBinary.binary] for getting the binary representation of a single enum value.
/// See [EnumCollectionBinary.binary] for getting the binary representation of multiple enum values.
extension EnumByBinary<T extends Enum> on List<T> {
/// Converts the binary representation of an enum into enum values.
Set<T> byBinary(final int value) {
final result = <T>{};
var v = value;
for (var i = 1; i <= ParticipantPermission.values.length - 1; i++) {
for (var i = 1; i <= length - 1; i++) {
if (v.isOdd) {
permissions.add(ParticipantPermission.values[i]);
result.add(this[i]);
}
v = v >> 1;
}
if (permissions.isEmpty) {
permissions.add(ParticipantPermission.$default);
if (result.isEmpty) {
result.add(first);
}
return permissions;
return result;
}
}
/// Extension for the integer representation of multiple [ParticipantPermission]s.
extension ParticipantPermissionsValue on Set<ParticipantPermission> {
/// Gets the integer representation of multiple [ParticipantPermission]s.
int get value => map((final p) => p.value).reduce((final a, final b) => a | b);
/// An extension to get the binary representation of an enum value based on its index.
extension EnumBinary on Enum {
/// Gets the binary representation of the [index].
///
/// See [EnumCollectionBinary.binary] for getting the binary representation of multiple enum values.
/// See [EnumByBinary.byBinary] for converting the binary representation into the enum values.
int get binary => index == 0 ? 0 : 1 << (index - 1);
}
/// An extension to get the binary representation of enum values based on their indexes.
extension EnumCollectionBinary<T extends Enum> on Set<T> {
/// Gets the binary representation of the indexes.
///
/// See [EnumBinary.binary] for getting the binary representation of a single enum value.
/// See [EnumByBinary.byBinary] for converting the binary representation into enum values.
int get binary => map((final p) => p.binary).reduce((final a, final b) => a | b);
}

22
packages/nextcloud/test/spreed_test.dart

@ -39,13 +39,13 @@ void main() {
});
test('Participant permissions', () async {
expect(spreed.ParticipantPermission.$default.value, 0);
expect(spreed.ParticipantPermission.fromValue(0), {spreed.ParticipantPermission.$default});
expect({spreed.ParticipantPermission.$default}.value, 0);
expect(spreed.ParticipantPermission.$default.binary, 0);
expect(spreed.ParticipantPermission.values.byBinary(0), {spreed.ParticipantPermission.$default});
expect({spreed.ParticipantPermission.$default}.binary, 0);
expect(spreed.ParticipantPermission.custom.value, 1);
expect(spreed.ParticipantPermission.canSendMessageAndShareAndReact.value, 128);
expect(spreed.ParticipantPermission.fromValue(129), {
expect(spreed.ParticipantPermission.custom.binary, 1);
expect(spreed.ParticipantPermission.canSendMessageAndShareAndReact.binary, 128);
expect(spreed.ParticipantPermission.values.byBinary(129), {
spreed.ParticipantPermission.custom,
spreed.ParticipantPermission.canSendMessageAndShareAndReact,
});
@ -53,7 +53,7 @@ void main() {
{
spreed.ParticipantPermission.custom,
spreed.ParticipantPermission.canSendMessageAndShareAndReact,
}.value,
}.binary,
129,
);
});
@ -69,7 +69,7 @@ void main() {
expect(response.body.ocs.data[0].name, 'user1');
expect(response.body.ocs.data[0].displayName, 'Talk updates ✅');
expect(response.body.ocs.data[0].participantType, spreed.ParticipantType.user.value);
expect(spreed.ParticipantPermission.fromValue(response.body.ocs.data[0].permissions), {
expect(spreed.ParticipantPermission.values.byBinary(response.body.ocs.data[0].permissions), {
spreed.ParticipantPermission.startCall,
spreed.ParticipantPermission.joinCall,
spreed.ParticipantPermission.canPublishAudio,
@ -107,7 +107,7 @@ void main() {
expect(response.body.ocs.data.name, 'user2');
expect(response.body.ocs.data.displayName, 'User Two');
expect(response.body.ocs.data.participantType, spreed.ParticipantType.owner.value);
expect(spreed.ParticipantPermission.fromValue(response.body.ocs.data.permissions), {
expect(spreed.ParticipantPermission.values.byBinary(response.body.ocs.data.permissions), {
spreed.ParticipantPermission.startCall,
spreed.ParticipantPermission.joinCall,
spreed.ParticipantPermission.canIgnoreLobby,
@ -129,7 +129,7 @@ void main() {
expect(response.body.ocs.data.name, 'admin');
expect(response.body.ocs.data.displayName, 'admin');
expect(response.body.ocs.data.participantType, spreed.ParticipantType.owner.value);
expect(spreed.ParticipantPermission.fromValue(response.body.ocs.data.permissions), {
expect(spreed.ParticipantPermission.values.byBinary(response.body.ocs.data.permissions), {
spreed.ParticipantPermission.startCall,
spreed.ParticipantPermission.joinCall,
spreed.ParticipantPermission.canIgnoreLobby,
@ -151,7 +151,7 @@ void main() {
expect(response.body.ocs.data.name, 'abc');
expect(response.body.ocs.data.displayName, 'abc');
expect(response.body.ocs.data.participantType, spreed.ParticipantType.owner.value);
expect(spreed.ParticipantPermission.fromValue(response.body.ocs.data.permissions), {
expect(spreed.ParticipantPermission.values.byBinary(response.body.ocs.data.permissions), {
spreed.ParticipantPermission.startCall,
spreed.ParticipantPermission.joinCall,
spreed.ParticipantPermission.canIgnoreLobby,

Loading…
Cancel
Save