Skip to content

Commit

Permalink
Merge branch 'user-model/main' into fix/language_not_set
Browse files Browse the repository at this point in the history
  • Loading branch information
jennantilla committed Sep 25, 2023
2 parents d49858c + 6839f4c commit 1c184ff
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 32 deletions.
2 changes: 2 additions & 0 deletions OneSignalSDK/onesignal/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ android {
jvmTarget = '1.8'
}
namespace 'com.onesignal.core'

kotlinOptions.freeCompilerArgs += ['-module-name', namespace]
}

tasks.withType(Test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object TimeUtils {
return offset / 1000
}

fun getTimeZoneId(): String? {
fun getTimeZoneId(): String {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ZoneId.systemDefault().id
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface IModelStore<TModel> :
*
* @param jsonObject The optional [JSONObject] to initialize the new model with.
*/
fun create(jsonObject: JSONObject? = null): TModel
fun create(jsonObject: JSONObject? = null): TModel?

/**
* List the models that are owned by this model store.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ abstract class ModelStore<TModel>(
val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
val jsonArray = JSONArray(str)
for (index in 0 until jsonArray.length()) {
val newModel = create(jsonArray.getJSONObject(index))
val newModel = create(jsonArray.getJSONObject(index)) ?: continue
_models.add(newModel)
// listen for changes to this model
newModel.subscribe(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class SingletonModelStore<TModel>(
return model
}

val createdModel = store.create()
val createdModel = store.create() ?: throw Exception("Unable to initialize model from store $store")
createdModel.id = _singletonId
store.add(createdModel)
return createdModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ enum class ExecutionResult {
* retried if authorization can be achieved.
*/
FAIL_UNAUTHORIZED,

/**
* Used in special login case.
* The operation failed due to a conflict and can be handled.
*/
FAIL_CONFLICT,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.onesignal.core.internal.operations.impl
import com.onesignal.common.modeling.ModelStore
import com.onesignal.core.internal.operations.Operation
import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.user.internal.operations.CreateSubscriptionOperation
import com.onesignal.user.internal.operations.DeleteAliasOperation
import com.onesignal.user.internal.operations.DeleteSubscriptionOperation
import com.onesignal.user.internal.operations.DeleteTagOperation
import com.onesignal.user.internal.operations.LoginUserOperation
import com.onesignal.user.internal.operations.LoginUserFromSubscriptionOperation
import com.onesignal.user.internal.operations.RefreshUserOperation
import com.onesignal.user.internal.operations.SetAliasOperation
import com.onesignal.user.internal.operations.SetPropertyOperation
Expand All @@ -18,6 +20,7 @@ import com.onesignal.user.internal.operations.TrackSessionStartOperation
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
import com.onesignal.user.internal.operations.UpdateSubscriptionOperation
import com.onesignal.user.internal.operations.impl.executors.IdentityOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.LoginUserFromSubscriptionOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.LoginUserOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.RefreshUserOperationExecutor
import com.onesignal.user.internal.operations.impl.executors.SubscriptionOperationExecutor
Expand All @@ -30,13 +33,15 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
load()
}

override fun create(jsonObject: JSONObject?): Operation {
override fun create(jsonObject: JSONObject?): Operation? {
if (jsonObject == null) {
throw NullPointerException("jsonObject")
Logging.error("null jsonObject sent to OperationModelStore.create")
return null
}

if (!jsonObject.has(Operation::name.name)) {
throw IllegalArgumentException("jsonObject must have '${Operation::name.name}' attribute")
Logging.error("jsonObject must have '${Operation::name.name}' attribute")
return null
}

// Determine the type of operation based on the name property in the json
Expand All @@ -48,6 +53,7 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
SubscriptionOperationExecutor.DELETE_SUBSCRIPTION -> DeleteSubscriptionOperation()
SubscriptionOperationExecutor.TRANSFER_SUBSCRIPTION -> TransferSubscriptionOperation()
LoginUserOperationExecutor.LOGIN_USER -> LoginUserOperation()
LoginUserFromSubscriptionOperationExecutor.LOGIN_USER_FROM_SUBSCRIPTION_USER -> LoginUserFromSubscriptionOperation()
RefreshUserOperationExecutor.REFRESH_USER -> RefreshUserOperation()
UpdateUserOperationExecutor.SET_TAG -> SetTagOperation()
UpdateUserOperationExecutor.DELETE_TAG -> DeleteTagOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ internal class OperationRepo(
}
ExecutionResult.FAIL_UNAUTHORIZED, // TODO: Need to provide callback for app to reset JWT. For now, fail with no retry.
ExecutionResult.FAIL_NORETRY,
ExecutionResult.FAIL_CONFLICT,
-> {
Logging.error("Operation execution failed without retry: $operations")
// on failure we remove the operation from the store and wake any waiters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ internal class IdentityOperationExecutor(
return when (responseType) {
NetworkUtils.ResponseStatusType.RETRYABLE ->
ExecutionResponse(ExecutionResult.FAIL_RETRY)
NetworkUtils.ResponseStatusType.INVALID,
NetworkUtils.ResponseStatusType.CONFLICT,
->
NetworkUtils.ResponseStatusType.INVALID ->
ExecutionResponse(ExecutionResult.FAIL_NORETRY)
NetworkUtils.ResponseStatusType.CONFLICT ->
ExecutionResponse(ExecutionResult.FAIL_CONFLICT)
NetworkUtils.ResponseStatusType.UNAUTHORIZED ->
ExecutionResponse(ExecutionResult.FAIL_UNAUTHORIZED)
NetworkUtils.ResponseStatusType.MISSING -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,16 @@ internal class LoginUserOperationExecutor(

ExecutionResponse(ExecutionResult.SUCCESS_STARTING_ONLY, mapOf(loginUserOp.onesignalId to backendOneSignalId))
}
ExecutionResult.FAIL_CONFLICT -> {
// When the SetAliasOperation fails with conflict that *most likely* means the externalId provided
// is already associated to a user. This *expected* condition means we must create a user.
// We hardcode the response of "user-2" in the log to provide information to the SDK consumer
Logging.debug("LoginUserOperationExecutor now handling 409 response with \"code\": \"user-2\" by switching to user with \"external_id\": \"${loginUserOp.externalId}\"")
createUser(loginUserOp, operations)
}
ExecutionResult.FAIL_NORETRY -> {
// When the SetAliasOperation fails without retry that *most likely* means the externalId provided
// is already associated to a user. This expected condition means we must create a user.
// Some other failure occurred, still try to recover by creating the user
Logging.error("LoginUserOperationExecutor encountered error. Attempt to recover by switching to user with \"external_id\": \"${loginUserOp.externalId}\"")
createUser(loginUserOp, operations)
}
else -> ExecutionResponse(result.result)
Expand Down
2 changes: 2 additions & 0 deletions OneSignalSDK/onesignal/in-app-messages/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ android {
jvmTarget = '1.8'
}
namespace 'com.onesignal.inAppMessages'

kotlinOptions.freeCompilerArgs += ['-module-name', namespace]
}

tasks.withType(Test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,14 @@ internal class WebViewManager(
}
Logging.debug("In app message, showing first one with height: $newHeight")

messageView!!.setWebView(webView!!)
messageView?.setWebView(webView!!)
if (newHeight != null) {
lastPageHeight = newHeight
messageView!!.updateHeight(newHeight)
messageView?.updateHeight(newHeight)
}
messageView!!.showView(activity)
messageView!!.checkIfShouldDismiss()
// showView does not return until in-app is dismissed
messageView?.showView(activity)
messageView?.checkIfShouldDismiss()
}
}

Expand Down
2 changes: 2 additions & 0 deletions OneSignalSDK/onesignal/location/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ android {
jvmTarget = '1.8'
}
namespace 'com.onesignal.location'

kotlinOptions.freeCompilerArgs += ['-module-name', namespace]
}

tasks.withType(Test) {
Expand Down
2 changes: 2 additions & 0 deletions OneSignalSDK/onesignal/notifications/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ android {
jvmTarget = '1.8'
}
namespace 'com.onesignal.notifications'

kotlinOptions.freeCompilerArgs += ['-module-name', namespace]
}

tasks.withType(Test) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@ import org.json.JSONObject
import java.security.SecureRandom

class NotificationGenerationJob(
private var _notification: Notification,
inNotification: Notification,
var jsonPayload: JSONObject,
) {
var notification: Notification
get() = _notification
private set(value) {
// If there is no android ID on the notification coming in, create one either
// copying from the previous one or generating a new one.
if (value != null && !value!!.hasNotificationId()) {
val curNotification = _notification
if (curNotification != null && curNotification.hasNotificationId()) {
value.androidNotificationId = curNotification.androidNotificationId
} else {
value.androidNotificationId = SecureRandom().nextInt()
}
}
val notification: Notification = inNotification.setAndroidNotificationId()

_notification = value
private fun Notification.setAndroidNotificationId() = this.also {
// If there is no android ID on the notification coming in, generate a new one.
if (it != null && !it.hasNotificationId()) {
it.androidNotificationId = SecureRandom().nextInt()
}
}

var isRestoring = false
var isNotificationToDisplay = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal class NotificationGenerationProcessor(
}.join()
}
} catch (to: TimeoutCancellationException) {
Logging.error("notificationWillShowInForegroundHandler timed out, continuing with wantsToDisplay=$wantsToDisplay.", to)
Logging.info("notificationWillShowInForegroundHandler timed out, continuing with wantsToDisplay=$wantsToDisplay.", to)
} catch (t: Throwable) {
Logging.error("notificationWillShowInForegroundHandler threw an exception. Displaying normal OneSignal notification.", t)
}
Expand Down

0 comments on commit 1c184ff

Please sign in to comment.