CustomMessage.fromMap constructor

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

Creates a new CustomMessage instance from a map.

Implementation

factory CustomMessage.fromMap(dynamic map, {AppEntity? receiver}) {
  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'];

  CustomMessage message = CustomMessage(
      subType: map['subType'] ?? '',
      customData:
          Map<String, dynamic>.from(json.decode(map['customData'] ?? '{}')),
      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'],
      category: map['category'],
      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'] ?? []),
      unreadRepliesCount: map['unreadRepliesCount'],
      mentionedUsers:
          map['mentionedUsers']?.map<User>((e) => User.fromMap(e)).toList() ??
              [],
      hasMentionedMe: map['hasMentionedMe'] ?? false,
      reactions: map['reactions']
              ?.map<ReactionCount>((e) => ReactionCount.fromMap(e))
              .toList() ??
          [],
      conversationText: map['text'],
      updateConversation: map['updateConversation'],
      sendNotification: map['sendNotification']);

  return message;
}