async function readCommandsFromAggregate()

in packages/chatdown/utils/index.js [208:328]


async function readCommandsFromAggregate(args, aggregate) {
    const newActivities = [];
    commandRegExp.lastIndex = 0;
    let result;
    let delay = messageTimeGap;
    let currentActivity = createActivity({ type: activitytypes.Message, from: args.from, recipient: args.recipient, conversationId: args.conversation.id });
    currentActivity.text = '';
    while ((result = commandRegExp.exec(aggregate))) {
        // typeOrField should always be listed first
        let match = result[1]; // result[] doesn't have [] on it
        let lines = match.split(NEWLINE);
        let split = lines[0].indexOf('=');
        let typeOrField = split > 0 ? lines[0].substring(0, split).trim() : lines[0].trim();
        let rest = (split > 0) ? lines[0].substring(split + 1).trim() : undefined;
        if (lines.length > 1)
            rest = match.substr(match.indexOf(NEWLINE) + NEWLINE.length);
        const type = activitytypes[typeOrField.toLowerCase()];
        const field = activityfield[typeOrField.toLowerCase()];
        const instruction = instructions[typeOrField.toLowerCase()];
        // This isn't an activity - bail
        if (!type && !field && !instruction) {
            // skip unknown tag
            let value = aggregate.substr(0, result.index + result[0].length);
            currentActivity.text += value;
            aggregate = aggregate.substring(value.length);
            continue;
        }

        // Indicates a new activity -
        // As more activity types are supported, this should
        // become a util or helper class.
        if (type) {
            let text = aggregate.substr(0, result.index).trim();
            if (text.length > 0) {
                currentActivity.text = text;
                currentActivity.timestamp = getIncrementedDate(delay);
                newActivities.push(currentActivity);
                // reset
                delay = messageTimeGap;
                currentActivity = createActivity({ type: activitytypes.Message, from: args.from, recipient: args.recipient, conversationId: args.conversation.id });
                currentActivity.text = '';
            }
            aggregate = aggregate.substr(result.index);

            switch (type) {
            case activitytypes.typing: {
                let newActivity = createActivity({ type, recipient: args.recipient, from: args.from, conversationId: args.conversation.id });
                newActivity.timestamp = getIncrementedDate(100);
                newActivities.push(newActivity);
                break;
            }
            case activitytypes.conversationupdate:
                processConversationUpdate(args, newActivities, rest);
                break;
            }
        }
        else if (instruction) {
            switch (instruction) {
            case instructions.delay:
                delay = parseInt(rest);
                break;
            }
        }
        else if (field) {
            // As more activity fields are supported,
            // this should become a util or helper class.
            switch (field) {
            case activityfield.attachment:
                await addAttachment(currentActivity, rest);
                break;
            case activityfield.attachmentlayout:
                addAttachmentLayout(currentActivity, rest);
                break;
            case activityfield.suggestions:
                addSuggestions(currentActivity, rest);
                break;
            case activityfield.basiccard:
            case activityfield.herocard:
                addCard(cardContentTypes.hero, currentActivity, rest);
                break;
            case activityfield.thumbnailcard:
                addCard(cardContentTypes.thumbnail, currentActivity, rest);
                break;
            case activityfield.animationcard:
                addCard(cardContentTypes.animation, currentActivity, rest);
                break;
            case activityfield.mediacard:
                addCard(cardContentTypes.media, currentActivity, rest);
                break;
            case activityfield.audiocard:
                addCard(cardContentTypes.audio, currentActivity, rest);
                break;
            case activityfield.videocard:
                addCard(cardContentTypes.video, currentActivity, rest);
                break;
            // case activityfield.receiptcard:
            //     addCard(cardContentTypes.receipt, currentActivity, rest);
            //     break;
            case activityfield.signincard:
                addCard(cardContentTypes.signin, currentActivity, rest);
                break;
            case activityfield.oauthcard:
                addCard(cardContentTypes.oauth, currentActivity, rest);
                break;
            }
        }
        // Trim off this activity or activity field and continue.
        aggregate = aggregate.replace(`[${result[1]}]`, '');
        commandRegExp.lastIndex = 0;
    }
    currentActivity.text += aggregate.trim();
    currentActivity.timestamp = getIncrementedDate(delay);

    // if we have content, then add it
    if (currentActivity.text.length > 0 ||
        (currentActivity.attachments && currentActivity.attachments.length > 0) ||
        (currentActivity.suggestedActions && currentActivity.suggestedActions.actions.length > 0)) {
        newActivities.push(currentActivity);
    }
    return newActivities.length ? newActivities : null;
}