toJson method

Map<String, dynamic> toJson()
override

Generates a map representing the InteractiveMessage.

Implementation

Map<String, dynamic> toJson() {
  User? userObj = sender;

  // Null-check BEFORE casting (see CustomMessage.toJson): an outgoing message
  // may carry only receiverUid, so `receiver as User` would throw
  // `type 'Null' is not a subtype of type 'User'` when serialized by the
  // durable outbox. Mirrors the guarded pattern in TextMessage.toJson.
  late Map receiverMap;
  if (receiverType == CometChatReceiverType.group) {
    if (receiver != null && receiver is Group) {
      receiverMap = (receiver as Group).toJson();
    } else {
      receiverMap = {};
    }
  } else {
    if (receiver != null && receiver is User) {
      receiverMap = (receiver as User).toJson();
    } else {
      receiverMap = {};
    }
  }

  final map = <String, dynamic>{};
  map['metadata'] = metadata;
  if (receiver != null) {
    map['receiver'] = receiverMap;
  }
  map['editedBy'] = editedBy;
  map['conversationId'] = conversationId;
  map['sentAt'] = sentAt == null ? 0 : sentAt?.millisecondsSinceEpoch;
  map['receiverUid'] = receiverUid;
  map['type'] = type;
  map['readAt'] = readAt == null ? 0 : readAt?.millisecondsSinceEpoch;
  map['deletedBy'] = deletedBy;
  map['deliveredAt'] = deliveredAt == null
      ? 0
      : deliveredAt?.millisecondsSinceEpoch;
  map['muid'] = muid;

  map['deletedAt'] = deletedAt == null
      ? 0
      : deletedAt?.millisecondsSinceEpoch;
  map['replyCount'] = replyCount;
  if (sender != null) {
    map['sender'] = userObj!.toJson();
  }
  map['receiverType'] = receiverType;
  map['editedAt'] = editedAt == null ? 0 : editedAt?.millisecondsSinceEpoch;
  map['parentMessageId'] = parentMessageId;
  map['readByMeAt'] = readByMeAt == null
      ? 0
      : readByMeAt?.millisecondsSinceEpoch;
  map['id'] = id;
  map['category'] = category;
  map['deliveredToMeAt'] = deliveredToMeAt == null
      ? 0
      : deliveredToMeAt?.millisecondsSinceEpoch;
  map['updatedAt'] = updatedAt == null
      ? 0
      : updatedAt?.millisecondsSinceEpoch;
  map['interactiveData'] = interactiveData;
  map['interactions'] = interactions?.map((e) => e.toJson()).toList();
  map['interactionGoal'] = interactionGoal?.toMap();
  map['tags'] = tags;
  map['allowSenderInteraction'] = allowSenderInteraction;
  map['unreadRepliesCount'] = unreadRepliesCount;
  map['quotedMessageId'] = quotedMessageId;
  if (quotedMessage != null) {
    map['quotedMessage'] = quotedMessage!.toJson();
  }
  return map;
}