TextMessage.fromMap constructor

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

Implementation

factory TextMessage.fromMap(dynamic map, {AppEntity? receiver}) {
  if (map == null) throw ArgumentError('The type of text 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'];

  TextMessage message = TextMessage(
    text: map['text'] ?? '',
    tags: List<String>.from(map['tags'] ?? []),
    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'],
    unreadRepliesCount: map['unreadRepliesCount'],
  );
  //Mentions
  message._mentionedUsers = map[MessageConstants.mentionedUsers]?.map<User>((e) => User.fromMap(e)).toList() ?? [];
  message._hasMentionedMe = map[MessageConstants.hasMentionedMe] ?? false;
  //Reactions
  message._reactions = map[MessageConstants.reactions]?.map<ReactionCount>((e) => ReactionCount.fromMap(e)).toList() ?? [];
  return message;
}