Skip to content

Commit

Permalink
calling logout disable push subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliu9508 committed Nov 5, 2024
1 parent 5b84764 commit ffa634a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.onesignal.user.internal.identity.IdentityModelStore
import com.onesignal.user.internal.operations.LoginUserFromSubscriptionOperation
import com.onesignal.user.internal.operations.LoginUserOperation
import com.onesignal.user.internal.operations.TransferSubscriptionOperation
import com.onesignal.user.internal.operations.UpdateSubscriptionOperation
import com.onesignal.user.internal.properties.PropertiesModel
import com.onesignal.user.internal.properties.PropertiesModelStore
import com.onesignal.user.internal.subscriptions.SubscriptionModel
Expand Down Expand Up @@ -426,13 +427,22 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {

// calling createAndSwitchToNewUser() replaces model with a default empty jwt
createAndSwitchToNewUser()
operationRepo!!.enqueue(
LoginUserOperation(
configModel!!.appId,
identityModelStore!!.model.onesignalId,
identityModelStore!!.model.externalId,
),
)

if (useIdentityVerification) {
// disable subscription if identity verification is on
operationRepo!!.enqueue(
UpdateSubscriptionOperation(),
)
} else {
// login to the anonymous user if identity verification is off
operationRepo!!.enqueue(
LoginUserOperation(
configModel!!.appId,
identityModelStore!!.model.onesignalId,
identityModelStore!!.model.externalId,
),
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class SubscriptionModelStoreListener(
private val _configModelStore: ConfigModelStore,
) : ModelStoreListener<SubscriptionModel>(store, opRepo) {
override fun getAddOperation(model: SubscriptionModel): Operation {
val enabledAndStatus = getSubscriptionEnabledAndStatus(model)
val enabledAndStatus = getSubscriptionEnabledAndStatus(model, _identityModelStore, _configModelStore)
return CreateSubscriptionOperation(
_configModelStore.model.appId,
_identityModelStore.model.onesignalId,
Expand All @@ -42,7 +42,7 @@ internal class SubscriptionModelStoreListener(
oldValue: Any?,
newValue: Any?,
): Operation {
val enabledAndStatus = getSubscriptionEnabledAndStatus(model)
val enabledAndStatus = getSubscriptionEnabledAndStatus(model, _identityModelStore, _configModelStore)
return UpdateSubscriptionOperation(
_configModelStore.model.appId,
_identityModelStore.model.onesignalId,
Expand All @@ -55,11 +55,20 @@ internal class SubscriptionModelStoreListener(
}

companion object {
fun getSubscriptionEnabledAndStatus(model: SubscriptionModel): Pair<Boolean, SubscriptionStatus> {
fun getSubscriptionEnabledAndStatus(
model: SubscriptionModel,
identityModelStore: IdentityModelStore,
configModelStore: ConfigModelStore,
): Pair<Boolean, SubscriptionStatus> {
val status: SubscriptionStatus
val enabled: Boolean

if (model.optedIn && model.status == SubscriptionStatus.SUBSCRIBED && model.address.isNotEmpty()) {
/*
When identity verification is off, we can enable the subscription regardless of the login status.
When identity verification is on, the subscription is enabled only when a user is currently logged in.
*/
val isUserLoggedInWhenIdentityRequired = !configModelStore.model.useIdentityVerification || !identityModelStore.model.externalId.isNullOrEmpty()
if (isUserLoggedInWhenIdentityRequired && model.optedIn && model.status == SubscriptionStatus.SUBSCRIBED && model.address.isNotEmpty()) {
enabled = true
status = SubscriptionStatus.SUBSCRIBED
} else {
Expand Down

0 comments on commit ffa634a

Please sign in to comment.