Skip to content

Commit 5724315

Browse files
chore: Adopt V9 Changes #WPB-18787
* Remove MessageCount notification event * Add Synchronization notification event * Generate new sync marker everytime the websocket is connected * Acknowledge synchronization event and add logs
1 parent 94490f8 commit 5724315

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

lib/src/main/kotlin/com/wire/integrations/jvm/client/BackendClient.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import com.wire.integrations.jvm.model.http.client.RegisterClientResponse
3030
import com.wire.integrations.jvm.model.http.conversation.ConversationResponse
3131
import com.wire.integrations.jvm.model.http.user.UserResponse
3232
import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
33+
import java.util.UUID
3334

3435
interface BackendClient {
36+
fun getCurrentSyncMarker(): UUID?
37+
3538
suspend fun connectWebSocket(handleFrames: suspend (DefaultClientWebSocketSession) -> Unit)
3639

3740
suspend fun getAvailableApiVersions(): ApiVersionResponse

lib/src/main/kotlin/com/wire/integrations/jvm/client/BackendClientDemo.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,29 @@ internal class BackendClientDemo(
8181
private var cachedFeatures: FeaturesResponse? = null
8282
private var cachedAccessToken: String? = null
8383
private var cachedDeviceId: String? = null
84+
private var currentSyncMarker: UUID? = null
85+
86+
override fun getCurrentSyncMarker(): UUID? = currentSyncMarker
8487

8588
override suspend fun connectWebSocket(
8689
handleFrames: suspend (DefaultClientWebSocketSession) -> Unit
8790
) {
8891
logger.info("Connecting to the webSocket, waiting for events")
8992

93+
currentSyncMarker = UUID.randomUUID()
9094
val token = loginUser()
95+
96+
val url = StringBuilder().apply {
97+
append("/$API_VERSION/events")
98+
append("?client=$cachedDeviceId")
99+
append("&access_token=$token")
100+
append("&sync_marker=$currentSyncMarker")
101+
}.toString()
102+
91103
httpClient.wss(
92104
host = IsolatedKoinContext.getApiHost()?.replace("https://", "")
93105
?.replace("-https", "-ssl"),
94-
path = "/$API_VERSION/events?client=$cachedDeviceId&access_token=$token"
106+
path = url
95107
) {
96108
handleFrames(this)
97109
}

lib/src/main/kotlin/com/wire/integrations/jvm/client/BackendClientImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import io.ktor.client.request.get
3939
import io.ktor.client.request.header
4040
import io.ktor.client.request.post
4141
import io.ktor.http.HttpHeaders
42+
import java.util.UUID
4243
import org.slf4j.LoggerFactory
4344

4445
/**
@@ -47,6 +48,8 @@ import org.slf4j.LoggerFactory
4748
internal class BackendClientImpl(private val httpClient: HttpClient) : BackendClient {
4849
private val logger = LoggerFactory.getLogger(this::class.java)
4950

51+
override fun getCurrentSyncMarker(): UUID? = null
52+
5053
override suspend fun connectWebSocket(
5154
handleFrames: suspend (DefaultClientWebSocketSession) -> Unit
5255
) {

lib/src/main/kotlin/com/wire/integrations/jvm/model/http/ConsumableNotificationResponse.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ sealed class ConsumableNotificationResponse {
3030
@SerialName("data") val data: EventDataDTO
3131
) : ConsumableNotificationResponse()
3232

33-
@Serializable
34-
@SerialName("message_count")
35-
data class MessageCount(
36-
@SerialName("data") val data: NotificationCount
37-
) : ConsumableNotificationResponse()
38-
3933
@Serializable
4034
@SerialName("notifications_missed")
4135
data object MissedNotification : ConsumableNotificationResponse()
36+
37+
@Serializable
38+
@SerialName("synchronization")
39+
data class SynchronizationNotification(
40+
@SerialName("data") val data: SynchronizationDataDTO
41+
) : ConsumableNotificationResponse()
4242
}
4343

4444
@Serializable
@@ -50,7 +50,9 @@ data class EventDataDTO(
5050
)
5151

5252
@Serializable
53-
data class NotificationCount(
54-
@SerialName("count")
55-
val count: ULong
53+
data class SynchronizationDataDTO(
54+
@SerialName("delivery_tag")
55+
val deliveryTag: ULong?,
56+
@SerialName("marker_id")
57+
val markerId: String
5658
)

lib/src/main/kotlin/com/wire/integrations/jvm/service/WireTeamEventsListener.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,27 @@ internal class WireTeamEventsListener internal constructor(
8989
}
9090
}
9191

92-
is ConsumableNotificationResponse.MessageCount -> {
93-
logger.info("Websocket back online, ${notification.data.count} events to fetch")
94-
}
95-
9692
is ConsumableNotificationResponse.MissedNotification -> {
9793
logger.warn("App was offline for too long, missed some notifications")
9894
val ackRequest = EventAcknowledgeRequest.notificationMissedAck()
9995
ackEvent(ackRequest, session)
10096
}
97+
98+
is ConsumableNotificationResponse.SynchronizationNotification -> {
99+
notification.data.deliveryTag?.let { deliveryTag ->
100+
val ackRequest = EventAcknowledgeRequest.basicAck(deliveryTag)
101+
ackEvent(ackRequest, session)
102+
}
103+
val currentSyncMarker = backendClient.getCurrentSyncMarker()
104+
if (notification.data.markerId == currentSyncMarker.toString()) {
105+
logger.info("Notifications are up to date since last sync marker.")
106+
} else {
107+
logger.debug(
108+
"Skipping sync marker [${notification.data.markerId}], " +
109+
"as it is not valid for this session."
110+
)
111+
}
112+
}
101113
}
102114
}
103115

0 commit comments

Comments
 (0)