AIAssistantMessage.fromMap constructor

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

Creates an AIAssistantMessage instance from a JSON map.

Implementation

factory AIAssistantMessage.fromMap(dynamic map, {AppEntity? receiver}) {
  if (map == null) {
    throw ArgumentError('The AI Assistant 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'] == null ||
          (map['conversationId'] as String).isEmpty)
      ? map['receiverType'] == CometChatReceiverType.user
          ? '${map['sender']?['uid']}_user_${(appEntity as User).uid}'
          : 'group_${map['receiver']['guid']}'
      : map['conversationId'];

  return AIAssistantMessage(
    runId: map['runId'],
    threadId: map['threadId'],
    text: map['text'],
    tags: List<String>.from(map['tags'] ?? []),
    id: map['id'] ?? 0,
    muid: map['muid'] ?? '',
    sender: map['sender'] == null ? null : User.fromMap(map['sender']),
    receiver: appEntity,
    receiverUid: map['receiverUid'] ?? '',
    type: map['type'] ?? CometChatMessageType.assistant,
    receiverType: map['receiverType'] ?? '',
    category: map['category'] ?? CometChatMessageCategory.categoryAgentic,
    sentAt: map['sentAt'] == null || map['sentAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['sentAt'] * 1000),
    deliveredAt: map['deliveredAt'] == null || map['deliveredAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredAt'] * 1000),
    readAt: map['readAt'] == null || map['readAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readAt'] * 1000),
    metadata: map['metadata'] == null
        ? {}
        : Map<String, dynamic>.from(map['metadata'] is String
            ? json.decode(map['metadata'])
            : map['metadata']),
    readByMeAt: map['readByMeAt'] == null || map['readByMeAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['readByMeAt'] * 1000),
    deliveredToMeAt: map['deliveredToMeAt'] == null ||
            map['deliveredToMeAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deliveredToMeAt'] * 1000),
    deletedAt: map['deletedAt'] == null || map['deletedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['deletedAt'] * 1000),
    editedAt: map['editedAt'] == null || map['editedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['editedAt'] * 1000),
    deletedBy: map['deletedBy'],
    editedBy: map['editedBy'],
    updatedAt: map['updatedAt'] == null || map['updatedAt'] == 0
        ? null
        : DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] * 1000),
    conversationId: conversationId,
    parentMessageId: map['parentMessageId'] ?? 0,
    replyCount: map['replyCount'] ?? 0,
    unreadRepliesCount: map['unreadRepliesCount'] ?? 0,
    mentionedUsers:
        map['mentionedUsers']?.map<User>((e) => User.fromMap(e)).toList() ??
            [],
    hasMentionedMe: map['hasMentionedMe'] ?? false,
    reactions: map['reactions']
            ?.map<ReactionCount>((e) => ReactionCount.fromMap(e))
            .toList() ??
        [],
  );
}