private getHighestPriorityMessageFromCache()

in src/logic/BrazeMessages.ts [181:228]


    private getHighestPriorityMessageFromCache(
        slotName: MessageSlotName,
        articleContext?: BrazeArticleContext,
    ) {
        const messagesFromCache = this.cache.all(slotName, this.braze, this.errorHandler);

        const allRenderableMessages = messagesFromCache.filter((msg) =>
            this.canRender(msg.message.extras),
        );

        // We want to prioritise messages with a filter if any of those match
        const { messagesWithFilters, messagesWithoutFilters } =
            splitByContextFilters(allRenderableMessages);

        const [firstRenderableMessage] = messagesWithFilters
            .concat(messagesWithoutFilters)
            .filter((msg) => {
                // The message does not have a section filter
                if (!msg.message.extras.section || typeof msg.message.extras.section != 'string') {
                    return true;
                }

                // The message has section(s) but no section was provided by the page
                const pageSection = articleContext?.section?.toLowerCase();
                if (!pageSection) {
                    return false;
                }

                // The message has a section filter and the page provided a section, do they match?
                const messageSections = msg.message.extras.section
                    .split('|')
                    .map((section) => section.toLowerCase());
                return messageSections.includes(pageSection);
            });

        if (firstRenderableMessage) {
            return new BrazeMessage(
                firstRenderableMessage.id,
                firstRenderableMessage.message,
                this.braze,
                slotName,
                this.cache,
                this.errorHandler,
            );
        }

        return;
    }