|
| 1 | +package com.onesignal.user.internal.migrations |
| 2 | + |
| 3 | +import com.onesignal.common.IDManager |
| 4 | +import com.onesignal.core.internal.config.ConfigModelStore |
| 5 | +import com.onesignal.core.internal.operations.IOperationRepo |
| 6 | +import com.onesignal.core.internal.operations.containsInstanceOf |
| 7 | +import com.onesignal.core.internal.startup.IStartableService |
| 8 | +import com.onesignal.debug.internal.logging.Logging |
| 9 | +import com.onesignal.user.internal.identity.IdentityModelStore |
| 10 | +import com.onesignal.user.internal.operations.LoginUserOperation |
| 11 | + |
| 12 | +/** |
| 13 | + * Purpose: Automatically recovers a stalled User in the OperationRepo due |
| 14 | + * to a bug in the SDK from 5.0.0 to 5.1.7. |
| 15 | + * |
| 16 | + * Issue: Some calls to OneSignal.login() would not be reflected on the |
| 17 | + * backend and would stall the the queue for that User. This would result |
| 18 | + * in User and Subscription operations to not be processed by |
| 19 | + * OperationRepo. |
| 20 | + * See PR #2046 for more details. |
| 21 | + * |
| 22 | + * Even if the developer called OneSignal.login() again with the same |
| 23 | + * externalId it would not correct the stalled User. |
| 24 | + * - Only calling logout() or login() with different externalId would |
| 25 | + * have un-stalled the OperationRepo. And then only after logging |
| 26 | + * back to the stalled user would it have recover all the unsent |
| 27 | + * operations they may exist. |
| 28 | + */ |
| 29 | +class RecoverFromDroppedLoginBug( |
| 30 | + private val _operationRepo: IOperationRepo, |
| 31 | + private val _identityModelStore: IdentityModelStore, |
| 32 | + private val _configModelStore: ConfigModelStore, |
| 33 | +) : IStartableService { |
| 34 | + override fun start() { |
| 35 | + if (isInBadState()) { |
| 36 | + Logging.warn( |
| 37 | + "User with externalId:" + |
| 38 | + "${_identityModelStore.model.externalId} " + |
| 39 | + "was in a bad state, causing it to not update on OneSignal's " + |
| 40 | + "backend! We are recovering and replaying all unsent " + |
| 41 | + "operations now.", |
| 42 | + ) |
| 43 | + recoverByAddingBackDroppedLoginOperation() |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // We are in the bad state if ALL are true: |
| 48 | + // 1. externalId is set (because OneSignal.login was called) |
| 49 | + // 2. We don't have a real yet onesignalId |
| 50 | + // - We haven't made a successful user create call yet. |
| 51 | + // 3. There is no attempt to create the User left in the |
| 52 | + // OperationRepo's queue. |
| 53 | + private fun isInBadState(): Boolean { |
| 54 | + val externalId = _identityModelStore.model.externalId |
| 55 | + val onesignalId = _identityModelStore.model.onesignalId |
| 56 | + |
| 57 | + // NOTE: We are not accounting a more rare (and less important case) |
| 58 | + // where a previously logged in User was never created. |
| 59 | + // That is, if another user already logged in successfully, but |
| 60 | + // the last user still has stuck pending operations due to the |
| 61 | + // User never being created on the OneSignal's backend. |
| 62 | + return externalId != null && |
| 63 | + IDManager.isLocalId(onesignalId) && |
| 64 | + !_operationRepo.containsInstanceOf<LoginUserOperation>() |
| 65 | + } |
| 66 | + |
| 67 | + private fun recoverByAddingBackDroppedLoginOperation() { |
| 68 | + // This is the operation that was dropped by mistake in some cases, |
| 69 | + // once it is added to the queue all and it gets executed, all |
| 70 | + // operations waiting on it will be sent. |
| 71 | + |
| 72 | + // This enqueues at the end, however this is ok, since |
| 73 | + // the OperationRepo is designed find the first operation that is |
| 74 | + // executable. |
| 75 | + _operationRepo.enqueue( |
| 76 | + LoginUserOperation( |
| 77 | + _configModelStore.model.appId, |
| 78 | + _identityModelStore.model.onesignalId, |
| 79 | + _identityModelStore.model.externalId, |
| 80 | + null, |
| 81 | + ), |
| 82 | + true, |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
0 commit comments