All Downloads are FREE. Search and download functionalities are using the official Maven repository.

engine.message.GenericMessage.kt Maven / Gradle / Ivy

/*
 * Copyright (C) 2017/2021 e-voyageurs technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ai.tock.bot.engine.message

import ai.tock.bot.connector.ConnectorMessage
import ai.tock.bot.connector.ConnectorType
import ai.tock.bot.connector.ConnectorType.Companion.none

/**
 * An aggregation of [Message]s used in [Sentence].
 * This is usually a "generic" view of [ConnectorMessage].
 */
data class GenericMessage(
    val connectorType: ConnectorType = ConnectorType.none,
    val attachments: List = emptyList(),
    val choices: List = emptyList(),
    // a qualified text map (ie "title" to "Ok computer", "subtitle" to "please listen")
    val texts: Map = emptyMap(),
    val locations: List = emptyList(),
    val metadata: Map = emptyMap(),
    val subElements: List = emptyList(),
    @Transient private val connectorMessage: ConnectorMessage? = null
) {

    companion object {
        /**
         * A [texts] parameter for title.
         */
        const val TITLE_PARAM = "title"

        /**
         * A [texts] parameter for subtitle.
         */
        const val SUBTITLE_PARAM = "subtitle"

        /**
         * A [texts] parameter for text content.
         */
        const val TEXT_PARAM = "text"
    }

    constructor(
        connectorMessage: ConnectorMessage,
        attachments: List = emptyList(),
        choices: List = emptyList(),
        texts: Map = emptyMap(),
        locations: List = emptyList(),
        metadata: Map = emptyMap(),
        subElements: List = emptyList()
    ) : this(
        connectorMessage.connectorType,
        attachments,
        choices,
        texts,
        locations,
        metadata,
        subElements,
        connectorMessage
    )

    internal fun findConnectorMessage() = connectorMessage

    internal fun obfuscate(): GenericMessage =
        if (connectorMessage == null) {
            this
        } else {
            connectorMessage.obfuscate().toGenericMessage()
                ?: GenericMessage(
                    connectorMessage.obfuscate(),
                    attachments,
                    choices,
                    texts,
                    locations,
                    metadata,
                    subElements
                )
        }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as GenericMessage

        if (connectorType != other.connectorType && connectorType != none && other.connectorType != none) return false
        if (attachments != other.attachments) return false
        if (choices != other.choices) return false
        if (texts != other.texts) return false
        if (locations != other.locations) return false
        if (metadata != other.metadata) return false
        if (subElements != other.subElements) return false

        return true
    }

    override fun hashCode(): Int {
        var result = attachments.hashCode()
        result = 31 * result + choices.hashCode()
        result = 31 * result + texts.hashCode()
        result = 31 * result + locations.hashCode()
        result = 31 * result + metadata.hashCode()
        result = 31 * result + subElements.hashCode()
        return result
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy