Skip to content

Commit

Permalink
refactor(nextcloud): Cleanup spreed helpers
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <jld3103yt@gmail.com>
  • Loading branch information
provokateurin committed Nov 5, 2023
1 parent b5f7efa commit e2ceb4e
Showing 1 changed file with 83 additions and 23 deletions.
106 changes: 83 additions & 23 deletions packages/nextcloud/lib/src/helpers/spreed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -45,12 +45,23 @@ enum RoomType {

/// Integer representation of the [ParticipantType].
int get value => index + 1;

/// Converts the integer 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 => [
RoomType.oneToOne,
RoomType.changelog,
RoomType.oneToOneFormer,
RoomType.noteToSelf,
].contains(this);
}

/// 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.
/// See https://github.com/nextcloud/spreed/blob/main/lib/Chat/ChatManager.php.
enum MessageType {
/// Message.
comment,
Expand All @@ -75,12 +86,15 @@ enum MessageType {
/// Deleted emoji reaction.
// ignore: constant_identifier_names
reaction_deleted;

/// Converts the string representation of a [MessageType] into a [MessageType].
static MessageType fromValue(final String value) => MessageType.values.firstWhere((final v) => v.name == value);
}

/// 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.
/// See https://github.com/nextcloud/spreed/blob/main/lib/Model/Attendee.php.
enum ActorType {
/// Logged-in users.
users,
Expand Down Expand Up @@ -109,12 +123,15 @@ enum ActorType {

/// Users from SIP.
phones;

/// Converts the string representation of a [ActorType] into a [ActorType].
static ActorType fromValue(final String value) => ActorType.values.firstWhere((final v) => v.name == value);
}

/// 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,
Expand All @@ -136,6 +153,9 @@ enum ParticipantType {

/// Integer representation of the [ParticipantType].
int get value => index + 1;

/// Converts the integer representation of a [ParticipantType] into a [ParticipantType].
static ParticipantType fromValue(final int value) => ParticipantType.values[value - 1];
}

/// Attendee permissions.
Expand All @@ -144,7 +164,7 @@ enum ParticipantType {
/// Use [value] to get the integer representation that is used in the API.
/// Use [ParticipantPermissionsValue.value] to convert multiple [ParticipantPermission] into the integer 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,
Expand Down Expand Up @@ -174,30 +194,70 @@ enum ParticipantPermission {
canSendMessageAndShareAndReact;

/// Integer representation of the [ParticipantPermission].
int get value => index == 0 ? 0 : 1 << (index - 1);
int get value => _bitValueFromEnumIndex(index);

/// Converts the integer representation of multiple [ParticipantPermission]s to the corresponding [ParticipantPermission]s.
static Set<ParticipantPermission> fromValue(final int value) {
final permissions = <ParticipantPermission>{};

var v = value;
for (var i = 1; i <= ParticipantPermission.values.length - 1; i++) {
if (v.isOdd) {
permissions.add(ParticipantPermission.values[i]);
}
v = v >> 1;
}

if (permissions.isEmpty) {
permissions.add(ParticipantPermission.$default);
}

return permissions;
}
static Set<ParticipantPermission> fromValue(final int value) => _enumFromBitValue(values, value);
}

/// 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);
}

/// Participant in-call flags.
///
/// 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 [ParticipantInCallFlagsValue.value] to convert multiple [ParticipantInCallFlag] into the integer 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,

/// Connected to the call with audio.
providesAudio,

/// Connected to the call with video.
providesVideo,

/// Connected to the call using SIP dial-in.
sipDialIn;

/// Integer representation of the [ParticipantInCallFlag].
int get value => _bitValueFromEnumIndex(index);

/// Converts the integer representation of multiple [ParticipantInCallFlag]s to the corresponding [ParticipantInCallFlag]s.
static Set<ParticipantInCallFlag> fromValue(final int value) => _enumFromBitValue(values, value);
}

/// Extension for the integer representation of multiple [ParticipantInCallFlag]s.
extension ParticipantInCallFlagsValue on Set<ParticipantInCallFlag> {
/// Gets the integer representation of multiple [ParticipantInCallFlag]s.
int get value => map((final p) => p.value).reduce((final a, final b) => a | b);
}

int _bitValueFromEnumIndex(final int index) => index == 0 ? 0 : 1 << (index - 1);

Set<T> _enumFromBitValue<T extends Enum>(final List<T> values, final int value) {
final result = <T>{};

var v = value;
for (var i = 1; i <= values.length - 1; i++) {
if (v.isOdd) {
result.add(values[i]);
}
v = v >> 1;
}

if (result.isEmpty) {
result.add(values.first);
}

return result;
}

0 comments on commit e2ceb4e

Please sign in to comment.