InteractiveMessage.fromMap constructor

InteractiveMessage.fromMap(
  1. dynamic map, {
  2. AppEntity? receiver,
})

Creates a new InteractiveMessage instance from a map.

Implementation

factory InteractiveMessage.fromMap(dynamic map, {AppEntity? receiver}) {
  bool? allowSenderInteraction = map["allowSenderInteraction"];
  Map<String, dynamic> _interactiveData;

  if (ioPlatform.Platform.isIOS) {
    _interactiveData = map['interactiveData']?.cast<String, dynamic>() ?? {};
  } else {
    _interactiveData = Map<String, dynamic>.from(
        json.decode(map['interactiveData'] ?? '{}'));
  }
  if (map == null) {
    throw ArgumentError('The type of custom message map is null');
  }
  final appEntity = (map['receiver'] == null)
      ? receiver
      : (map['receiverType'] == 'user')
          ? User.fromMap(map['receiver'])
          : Group.fromMap(map['receiver']);

  final conversationId = map['conversationId'].isEmpty
      ? map['receiverType'] == 'user'
          ? '${map['sender']['uid']}_user_${(appEntity as User).uid}'
          : 'group_${map['receiver']['guid']}'
      : map['conversationId'];

  return InteractiveMessage(
    id: map['id'],
    muid: map['muid'],
    sender: map['sender'] == null ? null : User.fromMap(map['sender']),
    receiver: appEntity,
    receiverUid: map['receiverUid'],
    type: map['type'],
    receiverType: map['receiverType'],
    sentAt: map['sentAt'] == 0 || map['sentAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['sentAt'] * 1000),
    deliveredAt: map['deliveredAt'] == 0 || map['deliveredAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredAt'] * 1000),
    readAt: map['readAt'] == 0 || map['readAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readAt'] * 1000),
    metadata: Map<String, dynamic>.from(json.decode(map['metadata'] ?? '{}')),
    readByMeAt: map['readByMeAt'] == 0 || map['readByMeAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readByMeAt'] * 1000),
    deliveredToMeAt: map['deliveredToMeAt'] == 0 ||
            map['deliveredToMeAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredToMeAt'] * 1000),
    deletedAt: map['deletedAt'] == 0 || map['deletedAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deletedAt'] * 1000),
    editedAt: map['editedAt'] == 0 || map['editedAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['editedAt'] * 1000),
    deletedBy: map['deletedBy'],
    editedBy: map['editedBy'],
    updatedAt: map['updatedAt'] == 0 || map['updatedAt'] == null
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] * 1000),
    conversationId: conversationId,
    parentMessageId: map['parentMessageId'],
    replyCount: map['replyCount'],
    tags: List<String>.from(map['tags'] ?? []),
    interactiveData: _interactiveData,
    interactionGoal: InteractionGoal.fromMap(map['interactionGoal']),
    interactions: map['interactions'] != null
        ? (map['interactions'])
            .map<Interaction>((optionMap) => Interaction.fromMap(optionMap))
            .toList()
        : null,
    allowSenderInteraction: allowSenderInteraction ?? false,
    unreadRepliesCount: map['unreadRepliesCount'],
  );
}