Skip to content

Commit 219acc2

Browse files
author
Rodrigo Gomez Palacio
committed
Use RywData everywhere we were previously using String (rywToken)
Motivation: update all usages of `rywToken` to now make use of the new `RywData` object. In `ConsistencyManager` we also renamed the method `setRywToken` to `setRywData`
1 parent e0d5bfc commit 219acc2

File tree

18 files changed

+132
-89
lines changed

18 files changed

+132
-89
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/consistency/IamFetchReadyCondition.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class IamFetchReadyCondition(
2020
override val id: String
2121
get() = ID
2222

23-
override fun isMet(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String>>): Boolean {
23+
override fun isMet(indexedTokens: Map<String, Map<IConsistencyKeyEnum, RywData>>): Boolean {
2424
val tokenMap = indexedTokens[key] ?: return false
2525
val userUpdateTokenSet = tokenMap[IamFetchRywTokenKey.USER] != null
2626

@@ -33,9 +33,13 @@ class IamFetchReadyCondition(
3333
return userUpdateTokenSet
3434
}
3535

36-
override fun getNewestToken(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String?>>): String? {
36+
override fun getNewestToken(indexedTokens: Map<String, Map<IConsistencyKeyEnum, RywData?>>): RywData? {
3737
val tokenMap = indexedTokens[key] ?: return null
38-
// maxOrNull compares lexicographically
39-
return listOfNotNull(tokenMap[IamFetchRywTokenKey.USER], tokenMap[IamFetchRywTokenKey.SUBSCRIPTION]).maxOrNull()
38+
39+
// Collect non-null RywData objects and find the one with the largest rywToken lexicographically
40+
return listOfNotNull(
41+
tokenMap[IamFetchRywTokenKey.USER],
42+
tokenMap[IamFetchRywTokenKey.SUBSCRIPTION],
43+
).maxByOrNull { it.rywToken ?: "" }
4044
}
4145
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/consistency/impl/ConsistencyManager.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.common.consistency.impl
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.consistency.models.ICondition
45
import com.onesignal.common.consistency.models.IConsistencyKeyEnum
56
import com.onesignal.common.consistency.models.IConsistencyManager
@@ -18,8 +19,8 @@ import kotlinx.coroutines.sync.withLock
1819
*/
1920
class ConsistencyManager : IConsistencyManager {
2021
private val mutex = Mutex()
21-
private val indexedTokens: MutableMap<String, MutableMap<IConsistencyKeyEnum, String>> = mutableMapOf()
22-
private val conditions: MutableList<Pair<ICondition, CompletableDeferred<String?>>> =
22+
private val indexedTokens: MutableMap<String, MutableMap<IConsistencyKeyEnum, RywData>> = mutableMapOf()
23+
private val conditions: MutableList<Pair<ICondition, CompletableDeferred<RywData?>>> =
2324
mutableListOf()
2425

2526
/**
@@ -29,10 +30,10 @@ class ConsistencyManager : IConsistencyManager {
2930
* key: K - corresponds to the operation for which we have a read-your-write token
3031
* value: String? - the token (read-your-write token)
3132
*/
32-
override suspend fun setRywToken(
33+
override suspend fun setRywData(
3334
id: String,
3435
key: IConsistencyKeyEnum,
35-
value: String,
36+
value: RywData,
3637
) {
3738
mutex.withLock {
3839
val rywTokens = indexedTokens.getOrPut(id) { mutableMapOf() }
@@ -46,7 +47,7 @@ class ConsistencyManager : IConsistencyManager {
4647
*/
4748
override suspend fun getRywDataFromAwaitableCondition(condition: ICondition): CompletableDeferred<RywData?> {
4849
mutex.withLock {
49-
val deferred = CompletableDeferred<String?>()
50+
val deferred = CompletableDeferred<RywData?>()
5051
val pair = Pair(condition, deferred)
5152
conditions.add(pair)
5253
checkConditionsAndComplete()
@@ -55,7 +56,7 @@ class ConsistencyManager : IConsistencyManager {
5556
}
5657

5758
override suspend fun resolveConditionsWithID(id: String) {
58-
val completedConditions = mutableListOf<Pair<ICondition, CompletableDeferred<String?>>>()
59+
val completedConditions = mutableListOf<Pair<ICondition, CompletableDeferred<RywData?>>>()
5960

6061
for ((condition, deferred) in conditions) {
6162
if (condition.id == id) {
@@ -74,13 +75,13 @@ class ConsistencyManager : IConsistencyManager {
7475
* IMPORTANT: calling code should be protected by mutex to avoid potential inconsistencies
7576
*/
7677
private fun checkConditionsAndComplete() {
77-
val completedConditions = mutableListOf<Pair<ICondition, CompletableDeferred<String?>>>()
78+
val completedConditions = mutableListOf<Pair<ICondition, CompletableDeferred<RywData?>>>()
7879

7980
for ((condition, deferred) in conditions) {
8081
if (condition.isMet(indexedTokens)) {
81-
val newestToken = condition.getNewestToken(indexedTokens)
82+
val rywDataForNewestToken = condition.getNewestToken(indexedTokens)
8283
if (!deferred.isCompleted) {
83-
deferred.complete(newestToken)
84+
deferred.complete(rywDataForNewestToken)
8485
}
8586
completedConditions.add(Pair(condition, deferred))
8687
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/consistency/models/ICondition.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.onesignal.common.consistency.models
22

3+
import com.onesignal.common.consistency.RywData
4+
35
interface ICondition {
46
/**
57
* Every implementation should define a unique ID & make available via a companion object for
@@ -11,11 +13,11 @@ interface ICondition {
1113
* Define a condition that "unblocks" execution
1214
* e.g. we have token (A && B) || A
1315
*/
14-
fun isMet(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String>>): Boolean
16+
fun isMet(indexedTokens: Map<String, Map<IConsistencyKeyEnum, RywData>>): Boolean
1517

1618
/**
1719
* Used to process tokens according to their format & return the newest token.
1820
* e.g. numeric strings would be compared differently from JWT tokens
1921
*/
20-
fun getNewestToken(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String?>>): String?
22+
fun getNewestToken(indexedTokens: Map<String, Map<IConsistencyKeyEnum, RywData?>>): RywData?
2123
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/consistency/models/IConsistencyManager.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.common.consistency.models
22

3+
import com.onesignal.common.consistency.RywData
34
import kotlinx.coroutines.CompletableDeferred
45

56
interface IConsistencyManager {
@@ -10,10 +11,10 @@ interface IConsistencyManager {
1011
* key: IConsistencyKeyEnum - corresponds to the operation for which we have a read-your-write token
1112
* value: String? - the read-your-write token
1213
*/
13-
suspend fun setRywToken(
14+
suspend fun setRywData(
1415
id: String,
1516
key: IConsistencyKeyEnum,
16-
value: String,
17+
value: RywData,
1718
)
1819

1920
/**
@@ -22,7 +23,7 @@ interface IConsistencyManager {
2223
* condition: ICondition - the condition to be registered
2324
* Returns: CompletableDeferred<String?> - a deferred action that completes when the condition is met
2425
*/
25-
suspend fun getRywDataFromAwaitableCondition(condition: ICondition): CompletableDeferred<String?>
26+
suspend fun getRywDataFromAwaitableCondition(condition: ICondition): CompletableDeferred<RywData?>
2627

2728
/**
2829
* Resolve all conditions with a specific ID

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/ISubscriptionBackendService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.backend
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.exceptions.BackendException
45

56
interface ISubscriptionBackendService {
@@ -21,7 +22,7 @@ interface ISubscriptionBackendService {
2122
aliasLabel: String,
2223
aliasValue: String,
2324
subscription: SubscriptionObject,
24-
): Pair<String, String?>?
25+
): Pair<String, RywData>?
2526

2627
/**
2728
* Update an existing subscription with the properties provided.
@@ -34,7 +35,7 @@ interface ISubscriptionBackendService {
3435
appId: String,
3536
subscriptionId: String,
3637
subscription: SubscriptionObject,
37-
): String?
38+
): RywData
3839

3940
/**
4041
* Delete an existing subscription.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/IUserBackendService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.backend
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.exceptions.BackendException
45

56
interface IUserBackendService {
@@ -47,7 +48,7 @@ interface IUserBackendService {
4748
properties: PropertiesObject,
4849
refreshDeviceMetadata: Boolean,
4950
propertyiesDelta: PropertiesDeltasObject,
50-
): String?
51+
): RywData
5152

5253
/**
5354
* Retrieve a user from the backend.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/SubscriptionBackendService.kt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.backend.impl
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.exceptions.BackendException
45
import com.onesignal.common.safeJSONObject
56
import com.onesignal.common.toMap
@@ -16,7 +17,7 @@ internal class SubscriptionBackendService(
1617
aliasLabel: String,
1718
aliasValue: String,
1819
subscription: SubscriptionObject,
19-
): Pair<String, String?>? {
20+
): Pair<String, RywData>? {
2021
val jsonSubscription = JSONConverter.convertToJSON(subscription)
2122
jsonSubscription.remove("id")
2223
val requestJSON = JSONObject().put("subscription", jsonSubscription)
@@ -38,14 +39,21 @@ internal class SubscriptionBackendService(
3839
rywToken = responseJSON.getString("ryw_token")
3940
}
4041

41-
return Pair(subscriptionJSON.getString("id"), rywToken)
42+
var rywDelay: Long? = null
43+
if (responseJSON.has("ryw_delay")) {
44+
rywDelay = responseJSON.getLong("ryw_delay")
45+
}
46+
47+
var rywData = RywData(rywToken, rywDelay)
48+
49+
return Pair(subscriptionJSON.getString("id"), rywData)
4250
}
4351

4452
override suspend fun updateSubscription(
4553
appId: String,
4654
subscriptionId: String,
4755
subscription: SubscriptionObject,
48-
): String? {
56+
): RywData {
4957
val requestJSON =
5058
JSONObject()
5159
.put("subscription", JSONConverter.convertToJSON(subscription))
@@ -56,12 +64,18 @@ internal class SubscriptionBackendService(
5664
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
5765
}
5866

59-
val responseBody = JSONObject(response.payload)
60-
return if (responseBody.has("ryw_token")) {
61-
responseBody.getString("ryw_token")
62-
} else {
63-
null
67+
val responseJSON = JSONObject(response.payload)
68+
var rywToken: String? = null
69+
if (responseJSON.has("ryw_token")) {
70+
rywToken = responseJSON.getString("ryw_token")
71+
}
72+
73+
var rywDelay: Long? = null
74+
if (responseJSON.has("ryw_delay")) {
75+
rywDelay = responseJSON.getLong("ryw_delay")
6476
}
77+
78+
return RywData(rywToken, rywDelay)
6579
}
6680

6781
override suspend fun deleteSubscription(

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/backend/impl/UserBackendService.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.onesignal.user.internal.backend.impl
22

3+
import com.onesignal.common.consistency.RywData
34
import com.onesignal.common.exceptions.BackendException
45
import com.onesignal.common.putMap
56
import com.onesignal.core.internal.http.IHttpClient
@@ -52,7 +53,7 @@ internal class UserBackendService(
5253
properties: PropertiesObject,
5354
refreshDeviceMetadata: Boolean,
5455
propertyiesDelta: PropertiesDeltasObject,
55-
): String? {
56+
): RywData {
5657
val jsonObject =
5758
JSONObject()
5859
.put("refresh_device_metadata", refreshDeviceMetadata)
@@ -71,12 +72,18 @@ internal class UserBackendService(
7172
throw BackendException(response.statusCode, response.payload, response.retryAfterSeconds)
7273
}
7374

74-
val responseBody = JSONObject(response.payload)
75-
return if (responseBody.has("ryw_token")) {
76-
responseBody.getString("ryw_token")
77-
} else {
78-
null
75+
val responseJSON = JSONObject(response.payload)
76+
var rywToken: String? = null
77+
if (responseJSON.has("ryw_token")) {
78+
rywToken = responseJSON.getString("ryw_token")
7979
}
80+
81+
var rywDelay: Long? = null
82+
if (responseJSON.has("ryw_delay")) {
83+
rywDelay = responseJSON.getLong("ryw_delay")
84+
}
85+
86+
return RywData(rywToken, rywDelay)
8087
}
8188

8289
override suspend fun getUser(

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/SubscriptionOperationExecutor.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ internal class SubscriptionOperationExecutor(
114114
) ?: return ExecutionResponse(ExecutionResult.SUCCESS)
115115

116116
val backendSubscriptionId = result.first
117-
val rywToken = result.second
117+
val rywData = result.second
118118

119-
if (rywToken != null) {
120-
_consistencyManager.setRywToken(createOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywToken)
119+
if (rywData.rywToken != null) {
120+
_consistencyManager.setRywData(createOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
121121
} else {
122122
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
123123
}
@@ -188,10 +188,10 @@ internal class SubscriptionOperationExecutor(
188188
AndroidUtils.getAppVersion(_applicationService.appContext),
189189
)
190190

191-
val rywToken = _subscriptionBackend.updateSubscription(lastOperation.appId, lastOperation.subscriptionId, subscription)
191+
val rywData = _subscriptionBackend.updateSubscription(lastOperation.appId, lastOperation.subscriptionId, subscription)
192192

193-
if (rywToken != null) {
194-
_consistencyManager.setRywToken(startingOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywToken)
193+
if (rywData.rywToken != null) {
194+
_consistencyManager.setRywData(startingOperation.onesignalId, IamFetchRywTokenKey.SUBSCRIPTION, rywData)
195195
} else {
196196
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
197197
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/operations/impl/executors/UpdateUserOperationExecutor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ internal class UpdateUserOperationExecutor(
138138

139139
if (appId != null && onesignalId != null) {
140140
try {
141-
val rywToken =
141+
val rywData =
142142
_userBackend.updateUser(
143143
appId,
144144
IdentityConstants.ONESIGNAL_ID,
@@ -148,8 +148,8 @@ internal class UpdateUserOperationExecutor(
148148
deltasObject,
149149
)
150150

151-
if (rywToken != null) {
152-
_consistencyManager.setRywToken(onesignalId, IamFetchRywTokenKey.USER, rywToken)
151+
if (rywData.rywToken != null) {
152+
_consistencyManager.setRywData(onesignalId, IamFetchRywTokenKey.USER, rywData)
153153
} else {
154154
_consistencyManager.resolveConditionsWithID(IamFetchReadyCondition.ID)
155155
}

0 commit comments

Comments
 (0)