com.pubnub.internal.endpoints.presence.GetStateEndpoint.kt Maven / Gradle / Ivy
package com.pubnub.internal.endpoints.presence
import com.google.gson.JsonElement
import com.pubnub.api.PubNubError
import com.pubnub.api.PubNubException
import com.pubnub.api.enums.PNOperationType
import com.pubnub.api.models.consumer.presence.PNGetStateResult
import com.pubnub.api.retry.RetryableEndpointGroup
import com.pubnub.internal.EndpointCore
import com.pubnub.internal.PubNubCore
import com.pubnub.internal.models.server.Envelope
import com.pubnub.internal.toCsv
import retrofit2.Call
import retrofit2.Response
/**
* @see [PubNubCore.getPresenceState]
*/
class GetStateEndpoint internal constructor(
pubnub: PubNubCore,
override val channels: List,
override val channelGroups: List,
override val uuid: String = pubnub.configuration.userId.value,
) : EndpointCore, PNGetStateResult>(pubnub), GetStateInterface {
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)
return retrofitManager.presenceService.getState(
configuration.subscribeKey,
channels.toCsv(),
uuid,
queryParams,
)
}
override fun createResponse(input: Response>): PNGetStateResult {
val stateMappings = hashMapOf()
if (channels.size == 1 && channelGroups.isEmpty()) {
stateMappings[channels.first()] = input.body()!!.payload!!
} else {
val it = pubnub.mapper.getObjectIterator(input.body()!!.payload!!)
while (it.hasNext()) {
val stateMapping = it.next()
stateMappings[stateMapping.key] = stateMapping.value
}
}
return PNGetStateResult(stateByUUID = stateMappings)
}
override fun operationType() = PNOperationType.PNGetState
override fun getEndpointGroupName(): RetryableEndpointGroup = RetryableEndpointGroup.PRESENCE
private fun addQueryParams(queryParams: MutableMap) {
if (channelGroups.isNotEmpty()) {
queryParams["channel-group"] = channelGroups.toCsv()
}
}
}