Skip to content

Commit

Permalink
recover from invalid Operations missing onesignalId
Browse files Browse the repository at this point in the history
* When uncaching an Operation that is not login-related, check for the existence of onesignalId.
* Note: The login-related Operations should still have onesignalId but they can be sent without it. It is also less likely for onesignalId to be null based on when these Operations are created.
* This is to remedy a rare case that an Operation could be missing the onesignalId, which would continuously cause crashes when the Operation is processed. The particular reported Operation is an UpdateSubscriptionOperation.
  • Loading branch information
nan-li committed Jul 31, 2024
1 parent 429c1c9 commit 8287a28
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper
return null
}

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

Expand Down Expand Up @@ -69,4 +68,33 @@ internal class OperationModelStore(prefs: IPreferencesService) : ModelStore<Oper

return operation
}

/**
* Checks if a JSONObject is a valid Operation. Contains a check for onesignalId.
* This is a rare case that a cached Operation is missing the onesignalId,
* which would continuously cause crashes when the Operation is processed.
*
* @param jsonObject The [JSONObject] that represents an Operation
*/
private fun isValidOperation(jsonObject: JSONObject): Boolean {
if (!jsonObject.has(Operation::name.name)) {
Logging.error("jsonObject must have '${Operation::name.name}' attribute")
return false
}

val operationName = jsonObject.getString(Operation::name.name)

val excluded = setOf(
LoginUserOperationExecutor.LOGIN_USER,
LoginUserFromSubscriptionOperationExecutor.LOGIN_USER_FROM_SUBSCRIPTION_USER
)

// Must have onesignalId if it is not one of the excluded operations above
if (!jsonObject.has("onesignalId") && !excluded.contains(operationName)) {
Logging.error("$operationName jsonObject must have 'onesignalId' attribute")
return false
}

return true
}
}

0 comments on commit 8287a28

Please sign in to comment.