updateParticipantPermission function

Future<void> updateParticipantPermission(
  1. UpdateParticipantPermissionOptions options
)

Updates a single participant's permission level. Only hosts (islevel === "2") can update permissions. Cannot change a host's permission level.

Example:

await updateParticipantPermission(UpdateParticipantPermissionOptions(
  socket: socket,
  participant: Participant(id: "123", name: "John", islevel: "0"),
  newLevel: "1",
  member: "currentUser",
  islevel: "2",
  roomName: "room123",
  showAlert: (alert) => print(alert.message),
));

Implementation

Future<void> updateParticipantPermission(
    UpdateParticipantPermissionOptions options) async {
  // Only hosts can update permissions
  if (options.islevel != "2") {
    options.showAlert?.call(
      message: "Only the host can update participant permissions",
      type: "danger",
      duration: 3000,
    );
    return;
  }

  // Cannot change host's permission
  if (options.participant.islevel == "2") {
    options.showAlert?.call(
      message: "Cannot change the host's permission level",
      type: "danger",
      duration: 3000,
    );
    return;
  }

  // Don't emit if level is the same
  if (options.participant.islevel == options.newLevel) {
    return;
  }

  final completer = Completer<void>();

  options.socket.emitWithAck("updateParticipantPermission", {
    'participantId': options.participant.id,
    'participantName': options.participant.name,
    'newLevel': options.newLevel,
    'roomName': options.roomName,
  }, ack: (response) {
    if (response == null || response['success'] != true) {
      final reason = response?['reason'] ?? 'Unknown error';
      debugPrint('updateParticipantPermission failed: $reason');
      options.showAlert?.call(
        message: reason,
        type: "danger",
        duration: 3000,
      );
    }
    completer.complete();
  });

  return completer.future;
}