Skip to content

Commit

Permalink
[PBE-3941] Fix event deserialization issues (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar authored Jun 11, 2024
1 parent 257c5a4 commit e605226
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 40 deletions.
5 changes: 5 additions & 0 deletions stream-video-android-core/api/stream-video-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -10063,6 +10063,11 @@ public final class org/openapitools/client/models/UnpinResponse {
public fun toString ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UnsupportedVideoEventException : java/lang/Exception {
public fun <init> (Ljava/lang/String;)V
public final fun getType ()Ljava/lang/String;
}

public final class org/openapitools/client/models/UpdateCallMembersRequest {
public fun <init> ()V
public fun <init> (Ljava/util/List;Ljava/util/List;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import io.getstream.video.android.core.internal.network.NetworkStateProvider
import io.getstream.video.android.model.User
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import okhttp3.OkHttpClient
import okhttp3.WebSocket
import org.openapitools.client.infrastructure.Serializer
import org.openapitools.client.models.ConnectUserDetailsRequest
import org.openapitools.client.models.ConnectedEvent
import org.openapitools.client.models.ConnectionErrorEvent
import org.openapitools.client.models.UnsupportedVideoEventException
import org.openapitools.client.models.VideoEvent
import org.openapitools.client.models.WSAuthMessageRequest

Expand Down Expand Up @@ -81,45 +82,48 @@ public class CoordinatorSocket(
override fun onMessage(webSocket: WebSocket, text: String) {
logger.d { "[onMessage] text: $text " }

scope.launch(singleThreadDispatcher) {
// parse the message
val jsonAdapter: JsonAdapter<VideoEvent> =
Serializer.moshi.adapter(VideoEvent::class.java)
val processedEvent = try {
jsonAdapter.fromJson(text)
} catch (e: Throwable) {
logger.w { "[onMessage] VideoEvent parsing error ${e.message}" }
null
}

if (processedEvent is ConnectedEvent) {
_connectionId.value = processedEvent.connectionId
setConnectedStateAndContinue(processedEvent)
}
if (text.isEmpty() || text == "null") {
logger.w { "[onMessage] Received empty socket message" }
return
}

// handle errors
if (text.isNotEmpty() && processedEvent == null) {
try {
val parsedError = Json.decodeFromString<SocketError>(text)
parsedError.let {
logger.w { "[onMessage] socketErrorEvent: ${parsedError.error}" }
handleError(it.error)
}
} catch (e: Throwable) {
logger.w { "[onMessage] socketErrorEvent parsing error: ${e.message}" }
handleError(e)
scope.launch(singleThreadDispatcher) {
try {
Serializer.moshi.adapter(VideoEvent::class.java).let { eventAdapter ->
eventAdapter.fromJson(text)?.let { parsedEvent -> processEvent(parsedEvent) }
}
} else {
logger.d { "parsed event $processedEvent" }

// emit the message
if (processedEvent == null) {
logger.w { "[onMessage] failed to parse event: $text" }
} catch (e: Throwable) {
if (e.cause is UnsupportedVideoEventException) {
val ex = e.cause as UnsupportedVideoEventException
logger.w { "[onMessage] Received unsupported VideoEvent type: ${ex.type}. Ignoring." }
} else {
ackHealthMonitor()
events.emit(processedEvent)
logger.w { "[onMessage] VideoEvent parsing error ${e.message}." }
handleError(e)
}
}
}
}

private suspend fun processEvent(parsedEvent: VideoEvent) {
if (parsedEvent is ConnectionErrorEvent) {
handleError(
ErrorResponse(
code = parsedEvent.error?.code ?: -1,
message = parsedEvent.error?.message ?: "",
statusCode = parsedEvent.error?.statusCode ?: -1,
exceptionFields = parsedEvent.error?.exceptionFields ?: emptyMap(),
moreInfo = parsedEvent.error?.moreInfo ?: "",
),
)
return
}

if (parsedEvent is ConnectedEvent) {
_connectionId.value = parsedEvent.connectionId
setConnectedStateAndContinue(parsedEvent)
}

ackHealthMonitor()
events.emit(parsedEvent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ class VideoEventAdapter : JsonAdapter<VideoEvent>() {
"call.session_participant_joined" -> CallSessionParticipantJoinedEvent::class.java
"call.session_participant_left" -> CallSessionParticipantLeftEvent::class.java
"call.session_started" -> CallSessionStartedEvent::class.java
"call.transcription_failed" -> CallTranscriptionFailedEvent::class.java
"call.transcription_ready" -> CallTranscriptionReadyEvent::class.java
"call.transcription_started" -> CallTranscriptionStartedEvent::class.java
"call.transcription_stopped" -> CallTranscriptionStoppedEvent::class.java
"call.unblocked_user" -> UnblockedUserEvent::class.java
"call.updated" -> CallUpdatedEvent::class.java
"call.user_muted" -> CallUserMutedEvent::class.java
"call.transcription_started" -> CallTranscriptionStartedEvent::class.java
"call.transcription_stopped" -> CallTranscriptionStoppedEvent::class.java
"call.transcription_ready" -> CallTranscriptionReadyEvent::class.java
"call.transcription_failed" -> CallTranscriptionFailedEvent::class.java
"connection.error" -> ConnectionErrorEvent::class.java
"connection.ok" -> ConnectedEvent::class.java
"custom" -> CustomVideoEvent::class.java
Expand All @@ -163,7 +163,9 @@ class VideoEventAdapter : JsonAdapter<VideoEvent>() {
"user.reactivated" -> UserReactivatedEvent::class.java
"user.unbanned" -> UserUnbannedEvent::class.java
"user.updated" -> UserUpdatedEvent::class.java
else -> throw IllegalArgumentException("Unknown type: $type")
else -> throw UnsupportedVideoEventException(type)
}
}
}

class UnsupportedVideoEventException(val type: String) : Exception()

0 comments on commit e605226

Please sign in to comment.