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

json.EventDeserializer.kt Maven / Gradle / Ivy

There is a newer version: 24.9.4
Show newest version
/*
 * 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.connector.twitter.json

import ai.tock.bot.connector.twitter.model.Application
import ai.tock.bot.connector.twitter.model.DirectMessage
import ai.tock.bot.connector.twitter.model.DirectMessageIndicateTyping
import ai.tock.bot.connector.twitter.model.Tweet
import ai.tock.bot.connector.twitter.model.User
import ai.tock.bot.connector.twitter.model.incoming.DirectMessageIncomingEvent
import ai.tock.bot.connector.twitter.model.incoming.DirectMessageIndicateTypingIncomingEvent
import ai.tock.bot.connector.twitter.model.incoming.IncomingEvent
import ai.tock.bot.connector.twitter.model.incoming.TweetIncomingEvent
import ai.tock.shared.jackson.JacksonDeserializer
import ai.tock.shared.jackson.read
import ai.tock.shared.jackson.readListValues
import ai.tock.shared.jackson.readValue
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.DeserializationContext
import mu.KotlinLogging

internal class EventDeserializer : JacksonDeserializer() {

    companion object {
        private val logger = KotlinLogging.logger {}
    }

    override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): IncomingEvent? {

        data class EventFields(
            var forUserId: String? = null,
            var users: Map? = null,
            var apps: Map? = null,
            var directMessages: List? = null,
            var directMessagesIndicateTyping: List? = null,
            var tweets: List? = null,
            var other: EmptyJson? = null
        )

        val (forUserId, users, apps, directMessages, directMessageIndicateTyping, statuses) = jp.read { fields, name ->
            with(fields) {
                when (name) {
                    "for_user_id" -> forUserId = jp.readValue()
                    DirectMessageIncomingEvent::users.name -> users = jp.readValueAs(object : TypeReference>() {})
                    DirectMessageIncomingEvent::apps.name -> apps = jp.readValueAs(object : TypeReference>() {})
                    "direct_message_events" -> directMessages = jp.readListValues()
                    "direct_message_indicate_typing_events" -> directMessagesIndicateTyping = jp.readListValues()
                    "tweet_create_events" -> tweets = jp.readValueAs(object : TypeReference>() {})
                    else -> other = jp.readUnknownValue()
                }
            }
        }

        return when {
            directMessages != null -> DirectMessageIncomingEvent(forUserId!!, users!!, apps, directMessages)
            directMessageIndicateTyping != null -> DirectMessageIndicateTypingIncomingEvent(forUserId!!, users!!, directMessageIndicateTyping)
            statuses != null -> TweetIncomingEvent(forUserId!!, statuses)
            else -> null
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy