@@ -60,29 +60,65 @@ class OSUserExecutor {
6060 OneSignalUserDefaults . initShared ( ) . saveCodeableData ( forKey: OS_USER_EXECUTOR_REQUEST_QUEUE_KEY, withValue: self . requestQueue)
6161 }
6262 }
63+
64+ static func parseCreateUserResponse( _ response: [ AnyHashable : Any ] ) {
65+ let subscriptionObject = parseSubscriptionObjectResponse ( response)
66+ let identityObject = parseIdentityObjectResponse ( response)
67+ let propertiesObject = parsePropertiesObjectResponse ( response)
68+ }
69+
70+ static func parseSubscriptionObjectResponse( _ response: [ AnyHashable : Any ] ? ) -> [ [ String : Any ] ] ? {
71+ return response ? [ " subscriptions " ] as? [ [ String : Any ] ]
72+ }
73+
74+ static func parsePropertiesObjectResponse( _ response: [ AnyHashable : Any ] ? ) -> [ String : Any ] ? {
75+ return response ? [ " properties " ] as? [ String : Any ]
76+ }
77+
78+ static func parseIdentityObjectResponse( _ response: [ AnyHashable : Any ] ? ) -> [ String : Any ] ? {
79+ return response ? [ " identity " ] as? [ String : Any ]
80+ }
6381
6482 // We will pass minimal properties to this request
6583 static func createUser( _ user: OSUserInternal ) {
6684 let request = OSRequestCreateUser ( identityModel: user. identityModel, pushSubscriptionModel: user. pushSubscriptionModel)
6785
6886 // Currently there are no requirements needed before sending this request
69-
70- OneSignalClient . shared ( ) . execute ( request) { _ in
87+ guard request. prepareForExecution ( ) else {
88+ return
89+ }
90+ OneSignalClient . shared ( ) . execute ( request) { response in
7191 // On success, check if the current user is the same as the one in the request
7292 // If user has changed, don't hydrate, except for push subscription
73- OneSignalUserManagerImpl . sharedInstance. user. pushSubscriptionModel. hydrate ( [ " subscription_id " : " 0123456789 " ] ) // Mocked
74- OSNotificationsManager . setPushSubscriptionId ( " some_mocked_subscription_id " )
75-
7693 let modelInStore = OneSignalUserManagerImpl . sharedInstance. identityModelStore. getModel ( key: OS_IDENTITY_MODEL_KEY)
7794
7895 guard modelInStore? . modelId == request. identityModel. modelId else {
7996 return
8097 }
98+ if let identityObject = parseIdentityObjectResponse ( response) {
99+ OneSignalUserManagerImpl . sharedInstance. user. identityModel. hydrate ( identityObject)
100+ }
101+ if let subscriptionObject = parseSubscriptionObjectResponse ( response) {
102+ for subModel in subscriptionObject {
103+ if let subType = subModel [ " type " ] as? String {
104+ if subType == " iOSPush " {
105+ OneSignalUserManagerImpl . sharedInstance. user. pushSubscriptionModel. hydrate ( subModel)
106+ if let subId = subModel [ " id " ] as? String {
107+ OSNotificationsManager . setPushSubscriptionId ( subId)
108+ }
109+ }
110+ }
111+ }
112+ }
113+ if let propertiesObject = parsePropertiesObjectResponse ( response) {
114+ OneSignalUserManagerImpl . sharedInstance. user. propertiesModel. hydrate ( propertiesObject)
115+ }
81116
82117 // hydrate identity, properties, subscriptions
83118
84119 executePendingRequests ( )
85- } onFailure: { _ in
120+ } onFailure: { error in
121+ print ( " ECM test + \( error. debugDescription) " )
86122 // Depending on error, Client is responsible for retrying.
87123 // executePendingRequests() ?
88124 }
@@ -192,7 +228,11 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
192228 let pushSubscriptionModel : OSSubscriptionModel
193229
194230 func prepareForExecution( ) -> Bool {
231+ guard let appId = OneSignalConfigManager . getAppId ( ) else {
232+ return false
233+ }
195234 self . addJWTHeader ( identityModel: identityModel)
235+ self . path = " apps/ \( appId) /users "
196236 // The pushSub doesn't need to have a token.
197237 return true
198238 }
@@ -206,18 +246,19 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
206246 var pushSubscriptionObject : [ String : Any ] = [ : ]
207247 pushSubscriptionObject [ " id " ] = pushSubscriptionModel. subscriptionId
208248 pushSubscriptionObject [ " type " ] = pushSubscriptionModel. type. rawValue
209- pushSubscriptionObject [ " token " ] = pushSubscriptionModel. address
249+ pushSubscriptionObject [ " token " ] = " test " // pushSubscriptionModel.address
210250 // ... and more ? ...
211251
212252 var params : [ String : Any ] = [ : ]
213253 if let externalId = identityModel. externalId {
214254 params [ " identity " ] = [ OS_EXTERNAL_ID: externalId]
215255 }
216- params [ " subscriptions " ] = pushSubscriptionObject
256+ params [ " subscriptions " ] = [ pushSubscriptionObject]
257+ params [ " properties " ] = nil
217258
218259 self . parameters = params
219260 self . method = POST
220- self . path = " user "
261+
221262 }
222263
223264 func encode( with coder: NSCoder ) {
@@ -267,9 +308,9 @@ class OSRequestIdentifyUser: OneSignalRequest, OSUserRequest {
267308
268309 // requires a onesignal_id to send this request
269310 func prepareForExecution( ) -> Bool {
270- if let onesignalId = identityModelToIdentify. onesignalId {
311+ if let onesignalId = identityModelToIdentify. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
271312 self . addJWTHeader ( identityModel: identityModelToIdentify)
272- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity "
313+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity "
273314 return true
274315 } else {
275316 // self.path is non-nil, so set to empty string
@@ -343,14 +384,15 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
343384 func prepareForExecution( ) -> Bool {
344385 // If there is an alias, use that
345386 if let aliasLabelToUse = aliasLabel,
387+ let appId = OneSignalConfigManager . getAppId ( ) ,
346388 let aliasIdToUse = aliasId {
347389 self . addJWTHeader ( identityModel: identityModel)
348- self . path = " user /by/\( aliasLabelToUse) / \( aliasIdToUse) "
390+ self . path = " apps/ \( appId ) /users /by/\( aliasLabelToUse) / \( aliasIdToUse) "
349391 return true
350392 }
351393 // Otherwise, use the onesignal_id
352- if let onesignalId = identityModel. onesignalId {
353- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) "
394+ if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
395+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) "
354396 return true
355397 } else {
356398 // self.path is non-nil, so set to empty string
@@ -400,9 +442,9 @@ class OSRequestAddAliases: OneSignalRequest, OSUserRequest {
400442
401443 // requires a `onesignal_id` to send this request
402444 func prepareForExecution( ) -> Bool {
403- if let onesignalId = identityModel. onesignalId {
445+ if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
404446 self . addJWTHeader ( identityModel: identityModel)
405- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity "
447+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity "
406448 return true
407449 } else {
408450 // self.path is non-nil, so set to empty string
@@ -450,9 +492,9 @@ class OSRequestRemoveAlias: OneSignalRequest, OSUserRequest {
450492 let identityModel : OSIdentityModel
451493
452494 func prepareForExecution( ) -> Bool {
453- if let onesignalId = identityModel. onesignalId {
495+ if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
454496 self . addJWTHeader ( identityModel: identityModel)
455- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity/ \( labelToRemove) "
497+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /identity/ \( labelToRemove) "
456498 return true
457499 } else {
458500 // self.path is non-nil, so set to empty string
@@ -500,9 +542,9 @@ class OSRequestUpdateProperties: OneSignalRequest, OSUserRequest {
500542 let identityModel : OSIdentityModel
501543
502544 func prepareForExecution( ) -> Bool {
503- if let onesignalId = identityModel. onesignalId {
545+ if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
504546 self . addJWTHeader ( identityModel: identityModel)
505- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) "
547+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) "
506548 return true
507549 } else {
508550 // self.path is non-nil, so set to empty string
@@ -565,9 +607,9 @@ class OSRequestCreateSubscription: OneSignalRequest, OSUserRequest {
565607
566608 // Need the onesignal_id of the user
567609 func prepareForExecution( ) -> Bool {
568- if let onesignalId = identityModel. onesignalId {
610+ if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
569611 self . addJWTHeader ( identityModel: identityModel)
570- self . path = " user /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /subscription "
612+ self . path = " apps/ \( appId ) /users /by/\( OS_ONESIGNAL_ID) / \( onesignalId) /subscription "
571613 return true
572614 } else {
573615 self . path = " " // self.path is non-nil, so set to empty string
0 commit comments