Action.fromMap constructor

Action.fromMap(
  1. dynamic map
)

Creates a new instance of the Action class from a map.

Implementation

factory Action.fromMap(dynamic map) {
  if (map == null) {
    throw ArgumentError('The type of action map is null');
  }

  final appEntity = (map['receiverType'] == 'user')
      ? User.fromMap(map['receiver'])
      : Group.fromMap(map['receiver']);

  AppEntity? getAppEntity(dynamic map) {
    AppEntity? returningEntity;
    if (map == null) {
      return null;
    }
    if (map['uid'] != null) {
      returningEntity = User.fromMap(map);
    } else if (map['guid'] != null) {
      returningEntity = Group.fromMap(map);
    } else if (map['id'] != null) {
      return (BaseMessage.fromMap(map));
    }
    return returningEntity;
  }

  Action message = Action(
      message: map['message'] ?? '',
      rawData: map['rawData'] ?? '{}',
      action: map['action'].toString(),
      oldScope: map['oldScope'].toString(),
      newScope: map['newScope'].toString(),
      id: map['id'],
      muid: map['muid'],
      sender: User.fromMap(map['sender']),
      receiver: appEntity,
      receiverUid: map['receiverUid'],
      type: map['type'],
      receiverType: map['receiverType'],
      category: map['category'],
      sentAt: map['sentAt']>0? DateTime.fromMillisecondsSinceEpoch(map['sentAt'] * 1000):null,
      deliveredAt: map['deliveredAt']>0?
          DateTime.fromMillisecondsSinceEpoch(map['deliveredAt'] * 1000):null,
      readAt: map['readAt']>0? DateTime.fromMillisecondsSinceEpoch(map['readAt'] * 1000):null,
      metadata:
          Map<String, dynamic>.from(json.decode(map['metadata'] ?? '{}')),
      readByMeAt: map['readByMeAt']>0?
          DateTime.fromMillisecondsSinceEpoch(map['readByMeAt'] * 1000):null,
      deliveredToMeAt: map['deliveredToMeAt']>0?
          DateTime.fromMillisecondsSinceEpoch(map['deliveredToMeAt'] * 1000):null,
      deletedAt: map['deletedAt']>0? DateTime.fromMillisecondsSinceEpoch(map['deletedAt'] * 1000):null,
      editedAt: map['editedAt']>0? DateTime.fromMillisecondsSinceEpoch(map['editedAt'] * 1000):null,
      deletedBy: map['deletedBy'],
      editedBy: map['editedBy'],
      updatedAt: map['updatedAt']>0? DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] * 1000):null,
      conversationId: map['conversationId'],
      parentMessageId: map['parentMessageId'],
      replyCount: map['replyCount'],
      actionBy: getAppEntity(map['actionBy']),
      actionOn: getAppEntity(map['actionOn']),
      actionFor: getAppEntity(map['actionFor']),
      unreadRepliesCount: map['unreadRepliesCount'],
      mentionedUsers:
          map['mentionedUsers']?.map<User>((e) => User.fromMap(e)).toList() ??
              [],
      hasMentionedMe: map['hasMentionedMe'] ?? false);

  return message;
}