Room.parse constructor

Room.parse(
  1. Object? source, {
  2. RoomExtra? extra,
})

Implementation

factory Room.parse(Object? source, {RoomExtra? extra}) {
  if (source is Room) return source;
  if (source is! Map) return const Room.empty();

  final keys = RoomKeys.i;

  final isBot = source[keys.isBot];
  final isGroup = source[keys.isGroup];
  final archives = source[keys.archives];
  final blocks = source[keys.blocks];
  final createdAt = source[keys.createdAt];
  final createdBy = source[keys.createdBy];
  final ex = source[keys.extra];
  final id = source[keys.id];
  final isDeleted = source[keys.isDeleted];
  final isVerified = source[keys.isVerified];
  final lastMessage = source[keys.lastMessage];
  final lastMessageDeleted = source[keys.lastMessageDeleted];
  final lastMessageId = source[keys.lastMessageId];
  final lastMessageSenderId = source[keys.lastMessageSenderId];
  final lastMessageStatuses = source[keys.lastMessageStatuses];
  final leaves = source[keys.leaves];
  final mutes = source[keys.mutes];
  final participants = source[keys.participants];
  final pins = source[keys.pins];
  final removes = source[keys.removes];
  final restricts = source[keys.restricts];
  final unseenCounts = source[keys.unseenCount];
  final updatedAt = source[keys.updatedAt];

  final mIsGroup = isGroup is bool ? isGroup : false;

  final room = Room(
    isLocal: false,
    isBot: isBot is bool ? isBot : false,
    isGroup: mIsGroup,
    archives: archives is Iterable ? archives.parsedStrings.toSet() : {},
    blocks: blocks is Iterable ? blocks.parsedStrings.toSet() : {},
    createdAt: ChatValueTimestamp.parse(createdAt),
    createdBy: createdBy is String && createdBy.isNotEmpty ? createdBy : '',
    extra: extra ?? (ex is Map ? ex.parse() : {}),
    id: id is String && id.isNotEmpty ? id : '',
    isDeleted: isDeleted is bool ? isDeleted : false,
    isVerified: isVerified is bool ? isVerified : false,
    lastMessage:
        lastMessage is String && lastMessage.isNotEmpty ? lastMessage : null,
    lastMessageDeleted:
        lastMessageDeleted is bool ? lastMessageDeleted : false,
    lastMessageId: lastMessageId is String && lastMessageId.isNotEmpty
        ? lastMessageId
        : '',
    lastMessageSenderId:
        lastMessageSenderId is String ? lastMessageSenderId : '',
    lastMessageStatuses: lastMessageStatuses is Map
        ? MessageStatus.values.references(lastMessageStatuses)
        : {},
    leaves: leaves is Iterable ? leaves.parsedStrings.toSet() : {},
    mutes: mutes is Iterable ? mutes.parsedStrings.toSet() : {},
    participants:
        participants is Iterable ? participants.parsedStrings.toSet() : {},
    pins: pins is Iterable ? pins.parsedStrings.toSet() : {},
    removes: removes is Map ? removes.parse() : {},
    restricts: restricts is Iterable ? restricts.parsedStrings.toSet() : {},
    unseenCounts: unseenCounts is Map ? unseenCounts.parse() : {},
    updatedAt: ChatValueTimestamp.parse(updatedAt),
  );

  if (mIsGroup) {
    final name = source[keys.name];
    final photo = source[keys.photo];
    return GroupRoom.from(
      room,
      name: name is String && name.isNotEmpty ? name : null,
      photo: photo is String && photo.isNotEmpty ? photo : null,
    );
  }

  return DirectRoom.from(room);
}