1- import { createUserOnServer } from 'src/onesignal/userDirector' ;
1+ import type { SubscriptionModel } from 'src/core/models/SubscriptionModel' ;
2+ import { CreateSubscriptionOperation } from 'src/core/operations/CreateSubscriptionOperation' ;
3+ import { LoginUserOperation } from 'src/core/operations/LoginUserOperation' ;
24import LoginManager from 'src/page/managers/LoginManager' ;
35import FuturePushSubscriptionRecord from 'src/page/userModel/FuturePushSubscriptionRecord' ;
46import type { ContextInterface } from 'src/shared/context/types' ;
@@ -12,6 +14,7 @@ import {
1214 PermissionBlockedError ,
1315 SWRegistrationError ,
1416} from 'src/shared/errors/common' ;
17+ import { getAppId } from 'src/shared/helpers/main' ;
1518import {
1619 incrementPageViewCount ,
1720 isFirstPageView ,
@@ -48,25 +51,48 @@ function executeCallback<T>(callback?: (...args: any[]) => T, ...args: any[]) {
4851 }
4952}
5053
54+ async function createSubscribedUser (
55+ pushModel : SubscriptionModel ,
56+ ) : Promise < void > {
57+ const identityModel = OneSignal . _coreDirector . _getIdentityModel ( ) ;
58+ const appId = getAppId ( ) ;
59+
60+ OneSignal . _coreDirector . _operationRepo . _enqueue (
61+ new LoginUserOperation (
62+ appId ,
63+ identityModel . _onesignalId ,
64+ identityModel . _externalId ,
65+ ) ,
66+ ) ;
67+ await OneSignal . _coreDirector . _operationRepo . _enqueueAndWait (
68+ new CreateSubscriptionOperation ( {
69+ ...pushModel . toJSON ( ) ,
70+ appId,
71+ onesignalId : identityModel . _onesignalId ,
72+ subscriptionId : pushModel . id ! ,
73+ } ) ,
74+ ) ;
75+ }
76+
5177export const updatePushSubscriptionModelWithRawSubscription = async (
5278 rawPushSubscription : RawPushSubscription ,
5379) => {
5480 // incase a login op was called before user accepts the notifcations permissions, we need to wait for it to finish
5581 // otherwise there would be two login ops in the same bucket for LoginOperationExecutor which would error
5682 await LoginManager . _switchingUsersPromise ;
5783
84+ // for new Anonymous/not-logged-in users, we need to create a new push subscription model and also save its push id to IndexedDB
5885 let pushModel = await OneSignal . _coreDirector . _getPushSubscriptionModel ( ) ;
59- // for new users, we need to create a new push subscription model and also save its push id to IndexedDB
6086 if ( ! pushModel ) {
6187 pushModel =
6288 OneSignal . _coreDirector . _generatePushSubscriptionModel (
6389 rawPushSubscription ,
6490 ) ;
65- return createUserOnServer ( ) ;
91+ return createSubscribedUser ( pushModel ) ;
6692 }
6793 // for users with data failed to create a user or user + subscription on the server
6894 if ( IDManager . _isLocalId ( pushModel . id ) ) {
69- return createUserOnServer ( ) ;
95+ return createSubscribedUser ( pushModel ) ;
7096 }
7197
7298 // in case of notification state changes, we need to update its web_auth, web_p256, and other keys
0 commit comments