@@ -30,7 +30,8 @@ import OneSignalCore
3030/**
3131 This request will be made with the minimum information needed. The payload will contain an externalId or no identities.
3232 The push subscription may or may not have a token or suscriptionId already.
33- There will be no properties sent.
33+ This request is used for typical User Create, which will include properties and the push subscription,
34+ or to hydrate OneSignal ID for a given External ID, which will only contain the Identity object in the payload.
3435 */
3536class OSRequestCreateUser : OneSignalRequest , OSUserRequest {
3637 var sentToClient = false
@@ -40,7 +41,7 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
4041 }
4142
4243 var identityModel : OSIdentityModel
43- var pushSubscriptionModel : OSSubscriptionModel
44+ var pushSubscriptionModel : OSSubscriptionModel ?
4445 var originalPushToken : String ?
4546
4647 func prepareForExecution( ) -> Bool {
@@ -55,14 +56,17 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
5556 return true
5657 }
5758
58- // When reading from the cache, update the push subscription model
59+ // When reading from the cache, update the push subscription model, if appropriate
5960 func updatePushSubscriptionModel( _ pushSubscriptionModel: OSSubscriptionModel ) {
61+ guard self . pushSubscriptionModel != nil else {
62+ return
63+ }
6064 self . pushSubscriptionModel = pushSubscriptionModel
6165 self . parameters ? [ " subscriptions " ] = [ pushSubscriptionModel. jsonRepresentation ( ) ]
6266 self . originalPushToken = pushSubscriptionModel. address
6367 }
6468
65- init ( identityModel: OSIdentityModel , propertiesModel: OSPropertiesModel , pushSubscriptionModel: OSSubscriptionModel , originalPushToken: String ? ) {
69+ init ( identityModel: OSIdentityModel , propertiesModel: OSPropertiesModel ? , pushSubscriptionModel: OSSubscriptionModel ? , originalPushToken: String ? ) {
6670 self . identityModel = identityModel
6771 self . pushSubscriptionModel = pushSubscriptionModel
6872 self . originalPushToken = originalPushToken
@@ -78,14 +82,18 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
7882 }
7983
8084 // Properties Object
81- var propertiesObject : [ String : Any ] = [ : ]
82- propertiesObject [ " language " ] = propertiesModel. language
83- propertiesObject [ " timezone_id " ] = propertiesModel. timezoneId
84- params [ " properties " ] = propertiesObject
85+ if let propertiesModel = propertiesModel {
86+ var propertiesObject : [ String : Any ] = [ : ]
87+ propertiesObject [ " language " ] = propertiesModel. language
88+ propertiesObject [ " timezone_id " ] = propertiesModel. timezoneId
89+ params [ " properties " ] = propertiesObject
90+ }
8591
8692 params [ " refresh_device_metadata " ] = true
8793 self . parameters = params
88- self . updatePushSubscriptionModel ( pushSubscriptionModel)
94+ if let pushSub = pushSubscriptionModel {
95+ self . updatePushSubscriptionModel ( pushSub)
96+ }
8997 self . method = POST
9098 }
9199
@@ -102,7 +110,6 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
102110 required init ? ( coder: NSCoder ) {
103111 guard
104112 let identityModel = coder. decodeObject ( forKey: " identityModel " ) as? OSIdentityModel ,
105- let pushSubscriptionModel = coder. decodeObject ( forKey: " pushSubscriptionModel " ) as? OSSubscriptionModel ,
106113 let parameters = coder. decodeObject ( forKey: " parameters " ) as? [ String : Any ] ,
107114 let rawMethod = coder. decodeObject ( forKey: " method " ) as? UInt32 ,
108115 let path = coder. decodeObject ( forKey: " path " ) as? String ,
@@ -112,7 +119,7 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
112119 return nil
113120 }
114121 self . identityModel = identityModel
115- self . pushSubscriptionModel = pushSubscriptionModel
122+ self . pushSubscriptionModel = coder . decodeObject ( forKey : " pushSubscriptionModel " ) as? OSSubscriptionModel
116123 self . originalPushToken = coder. decodeObject ( forKey: " originalPushToken " ) as? String
117124 self . stringDescription = " <OSRequestCreateUser with externalId: \( identityModel. externalId ?? " nil " ) > "
118125 super. init ( )
0 commit comments