leaveRoom method

Future<Result<bool, MUCError>> leaveRoom(
  1. JID roomJid
)

Leaves a Multi-User Chat room.

Leaves the specified MUC room by sending an 'unavailable' presence stanza. Removes the corresponding room entry from the cache. Returns a Result with a boolean value indicating success or failure, or an MUCError if applicable.

Implementation

Future<Result<bool, MUCError>> leaveRoom(
  JID roomJid,
) async {
  final nick = await _cacheLock.synchronized(() {
    final nick = _mucRoomCache[roomJid]?.nick;
    _mucRoomCache.remove(roomJid);
    return nick;
  });
  if (nick == null) {
    return Result(RoomNotJoinedError());
  }
  await getAttributes().sendStanza(
    StanzaDetails(
      Stanza.presence(
        to: roomJid.withResource(nick).toString(),
        type: 'unavailable',
      ),
      awaitable: false,
    ),
  );
  return const Result(true);
}