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

com.pubnub.internal.endpoints.presence.HeartbeatEndpoint.kt Maven / Gradle / Ivy

package com.pubnub.internal.endpoints.presence

import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.retry.RetryableEndpointGroup
import com.pubnub.internal.EndpointCore
import com.pubnub.internal.PubNubCore
import com.pubnub.internal.PubNubUtil
import retrofit2.Call
import retrofit2.Response

class HeartbeatEndpoint internal constructor(
    pubnub: PubNubCore,
    val channels: List = listOf(),
    val channelGroups: List = listOf(),
    val state: Any? = null,
) : EndpointCore(pubnub) {
    override fun getAffectedChannels() = channels

    override fun getAffectedChannelGroups() = channelGroups

    override fun validateParams() {
        super.validateParams()
        if (channels.isEmpty() && channelGroups.isEmpty()) {
            throw PubNubException(PubNubError.CHANNEL_AND_GROUP_MISSING)
        }
    }

    override fun doWork(queryParams: HashMap): Call {
        addQueryParams(queryParams)

        val channelsCsv =
            if (channels.isNotEmpty()) {
                channels.joinToString(",")
            } else {
                ","
            }

        return retrofitManager.presenceService.heartbeat(
            configuration.subscribeKey,
            channelsCsv,
            queryParams,
        )
    }

    private fun addQueryParams(queryParams: HashMap) {
        queryParams["heartbeat"] = pubnub.configuration.presenceTimeout.toString()

        if (channelGroups.isNotEmpty()) {
            queryParams["channel-group"] = channelGroups.joinToString(",")
        }

        state?.let {
            queryParams["state"] = pubnub.mapper.toJson(it)
        }

        PubNubUtil.maybeAddEeQueryParam(queryParams)
    }

    override fun createResponse(input: Response): Boolean {
        return true
    }

    override fun operationType() = PNOperationType.PNHeartbeatOperation

    override fun getEndpointGroupName(): RetryableEndpointGroup = RetryableEndpointGroup.PRESENCE
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy