@@ -467,6 +467,7 @@ class OSUserExecutor {
467467// MARK: - User Request Classes
468468
469469protocol OSUserRequest : OneSignalRequest , NSCoding {
470+ var sentToClient : Bool { get set }
470471 func prepareForExecution( ) -> Bool
471472}
472473
@@ -479,13 +480,15 @@ protocol OSUserRequest: OneSignalRequest, NSCoding {
479480 There will be no properties sent.
480481 */
481482class OSRequestCreateUser : OneSignalRequest , OSUserRequest {
483+ var sentToClient = false
482484 let stringDescription : String
483485 override var description : String {
484486 return stringDescription
485487 }
486488
487- let identityModel : OSIdentityModel
488- let pushSubscriptionModel : OSSubscriptionModel
489+ var identityModel : OSIdentityModel
490+ var pushSubscriptionModel : OSSubscriptionModel
491+ let originalPushToken : String ?
489492
490493 func prepareForExecution( ) -> Bool {
491494 guard let appId = OneSignalConfigManager . getAppId ( ) else {
@@ -498,13 +501,10 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
498501 return true
499502 }
500503
501- init ( identityModel : OSIdentityModel , pushSubscriptionModel : OSSubscriptionModel ) {
502- self . identityModel = identityModel
504+ // When reading from the cache, update the push subscription model
505+ func updatePushSubscriptionModel ( _ pushSubscriptionModel : OSSubscriptionModel ) {
503506 self . pushSubscriptionModel = pushSubscriptionModel
504- self . stringDescription = " OSRequestCreateUser "
505-
506- super. init ( )
507-
507+ // Push Subscription Object
508508 var pushSubscriptionObject : [ String : Any ] = [ : ]
509509 pushSubscriptionObject [ " id " ] = pushSubscriptionModel. subscriptionId
510510 pushSubscriptionObject [ " type " ] = pushSubscriptionModel. type. rawValue
@@ -520,22 +520,38 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
520520 if pushSubscriptionModel. notificationTypes != - 1 {
521521 pushSubscriptionObject [ " notification_types " ] = pushSubscriptionModel. notificationTypes
522522 }
523+ self . parameters ? [ " subscriptions " ] = [ pushSubscriptionObject]
524+ }
525+
526+ init ( identityModel: OSIdentityModel , propertiesModel: OSPropertiesModel , pushSubscriptionModel: OSSubscriptionModel , originalPushToken: String ? ) {
527+ self . identityModel = identityModel
528+ self . pushSubscriptionModel = pushSubscriptionModel
529+ self . originalPushToken = originalPushToken
530+ self . stringDescription = " OSRequestCreateUser "
531+ super. init ( )
523532
524533 var params : [ String : Any ] = [ : ]
534+
535+ // Identity Object
525536 params [ " identity " ] = [ : ]
526537 if let externalId = identityModel. externalId {
527538 params [ " identity " ] = [ OS_EXTERNAL_ID: externalId]
528539 }
529- params [ " subscriptions " ] = [ pushSubscriptionObject]
530- params [ " properties " ] = [ : ]
540+
541+ // Properties Object
542+ var propertiesObject : [ String : Any ] = [ : ]
543+ propertiesObject [ " language " ] = propertiesModel. language
544+ params [ " properties " ] = propertiesObject
531545
532546 self . parameters = params
547+ self . updatePushSubscriptionModel ( pushSubscriptionModel)
533548 self . method = POST
534549 }
535550
536551 func encode( with coder: NSCoder ) {
537552 coder. encode ( identityModel, forKey: " identityModel " )
538553 coder. encode ( pushSubscriptionModel, forKey: " pushSubscriptionModel " )
554+ coder. encode ( originalPushToken, forKey: " originalPushToken " )
539555 coder. encode ( parameters, forKey: " parameters " )
540556 coder. encode ( method. rawValue, forKey: " method " ) // Encodes as String
541557 coder. encode ( path, forKey: " path " )
@@ -556,6 +572,7 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
556572 }
557573 self . identityModel = identityModel
558574 self . pushSubscriptionModel = pushSubscriptionModel
575+ self . originalPushToken = coder. decodeObject ( forKey: " originalPushToken " ) as? String
559576 self . stringDescription = " OSRequestCreateUser "
560577 super. init ( )
561578 self . parameters = parameters
@@ -569,18 +586,18 @@ class OSRequestCreateUser: OneSignalRequest, OSUserRequest {
569586 The `identityModelToIdentify` is used for the `onesignal_id` of the user we want to associate with this alias.
570587 This request will tell us if we should continue with the previous user who is now identitfied, or to change users to the one this alias already exists on.
571588
572- Note: The SDK needs an user to operate on before this request returns. However, at the time of this request's creation, the SDK does not know if there is already
573- an user associated with this alias. So, it creates a blank new user (whose identity model is passed in as `identityModelToUpdate`,
589+ Note: The SDK needs an user to operate on before this request returns. However, at the time of this request's creation, the SDK does not know if there is already an user associated with this alias. So, it creates a blank new user (whose identity model is passed in as `identityModelToUpdate`,
574590 which is the model used to make a subsequent ``OSRequestFetchUser``).
575591 */
576592class OSRequestIdentifyUser : OneSignalRequest , OSUserRequest {
593+ var sentToClient = false
577594 let stringDescription : String
578595 override var description : String {
579596 return stringDescription
580597 }
581598
582- let identityModelToIdentify : OSIdentityModel
583- let identityModelToUpdate : OSIdentityModel
599+ var identityModelToIdentify : OSIdentityModel
600+ var identityModelToUpdate : OSIdentityModel
584601 let aliasLabel : String
585602 let aliasId : String
586603
@@ -657,40 +674,31 @@ class OSRequestIdentifyUser: OneSignalRequest, OSUserRequest {
657674 The `identityModel` is also used to reference the user that is updated with the response.
658675 */
659676class OSRequestFetchUser : OneSignalRequest , OSUserRequest {
677+ var sentToClient = false
660678 let stringDescription : String
661679 override var description : String {
662680 return stringDescription
663681 }
664682
665683 let identityModel : OSIdentityModel
666- let aliasLabel : String ?
667- let aliasId : String ?
684+ let aliasLabel : String
685+ let aliasId : String
668686
669687 func prepareForExecution( ) -> Bool {
670- // If there is an alias, use that
671- if let aliasLabelToUse = aliasLabel,
672- let appId = OneSignalConfigManager . getAppId ( ) ,
673- let aliasIdToUse = aliasId {
674- self . addJWTHeader ( identityModel: identityModel)
675- self . path = " apps/ \( appId) /users/by/ \( aliasLabelToUse) / \( aliasIdToUse) "
676- return true
677- }
678- // Otherwise, use the onesignal_id
679- if let onesignalId = identityModel. onesignalId, let appId = OneSignalConfigManager . getAppId ( ) {
680- self . path = " apps/ \( appId) /users/by/ \( OS_ONESIGNAL_ID) / \( onesignalId) "
681- return true
682- } else {
683- // self.path is non-nil, so set to empty string
684- self . path = " "
688+ guard let appId = OneSignalConfigManager . getAppId ( ) else {
689+ OneSignalLog . onesignalLog ( . LL_DEBUG, message: " Cannot generate the fetch user request due to null app ID. " )
685690 return false
686691 }
692+ self . addJWTHeader ( identityModel: identityModel)
693+ self . path = " apps/ \( appId) /users/by/ \( aliasLabel) / \( aliasId) "
694+ return true
687695 }
688696
689- init ( identityModel: OSIdentityModel , aliasLabel: String ? , aliasId: String ? ) {
697+ init ( identityModel: OSIdentityModel , aliasLabel: String , aliasId: String ) {
690698 self . identityModel = identityModel
691699 self . aliasLabel = aliasLabel
692700 self . aliasId = aliasId
693- self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel ?? " nil " ) aliasId: \( aliasId ?? " nil " ) "
701+ self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel) aliasId: \( aliasId) "
694702 super. init ( )
695703 self . method = GET
696704 _ = prepareForExecution ( ) // sets the path property
@@ -707,16 +715,18 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
707715 required init ? ( coder: NSCoder ) {
708716 guard
709717 let identityModel = coder. decodeObject ( forKey: " identityModel " ) as? OSIdentityModel ,
718+ let aliasLabel = coder. decodeObject ( forKey: " aliasLabel " ) as? String ,
719+ let aliasId = coder. decodeObject ( forKey: " aliasId " ) as? String ,
710720 let rawMethod = coder. decodeObject ( forKey: " method " ) as? UInt32 ,
711721 let timestamp = coder. decodeObject ( forKey: " timestamp " ) as? Date
712722 else {
713723 // Log error
714724 return nil
715725 }
716726 self . identityModel = identityModel
717- self . aliasLabel = coder . decodeObject ( forKey : " aliasLabel " ) as? String
718- self . aliasId = coder . decodeObject ( forKey : " aliasId " ) as? String
719- self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel ?? " nil " ) aliasId: \( aliasId ?? " nil " ) "
727+ self . aliasLabel = aliasLabel
728+ self . aliasId = aliasId
729+ self . stringDescription = " OSRequestFetchUser with aliasLabel: \( aliasLabel) aliasId: \( aliasId) "
720730 super. init ( )
721731 self . method = HTTPMethod ( rawValue: rawMethod)
722732 self . timestamp = timestamp
@@ -725,6 +735,7 @@ class OSRequestFetchUser: OneSignalRequest, OSUserRequest {
725735}
726736
727737class OSRequestAddAliases : OneSignalRequest , OSUserRequest {
738+ var sentToClient = false
728739 let stringDescription : String
729740 override var description : String {
730741 return stringDescription
@@ -782,6 +793,7 @@ class OSRequestAddAliases: OneSignalRequest, OSUserRequest {
782793}
783794
784795class OSRequestRemoveAlias : OneSignalRequest , OSUserRequest {
796+ var sentToClient = false
785797 let stringDescription : String
786798 override var description : String {
787799 return stringDescription
@@ -839,15 +851,19 @@ class OSRequestRemoveAlias: OneSignalRequest, OSUserRequest {
839851}
840852
841853class OSRequestUpdateProperties : OneSignalRequest , OSUserRequest {
854+ var sentToClient = false
842855 let stringDescription : String
843856 override var description : String {
844857 return stringDescription
845858 }
846859
847860 // TODO: does updating properties even have a response in which we need to hydrate from? Then we can get rid of modelToUpdate
861+ // Yes we may, if we cleared local state
848862 var modelToUpdate : OSPropertiesModel
849863 var identityModel : OSIdentityModel
850864
865+ // TODO: Decide if addPushSubscriptionIdToAdditionalHeadersIfNeeded should block.
866+ // Note Android adds it to requests, if the push sub ID exists
851867 func prepareForExecution( ) -> Bool {
852868 if let onesignalId = identityModel. onesignalId,
853869 let appId = OneSignalConfigManager . getAppId ( ) ,
@@ -866,7 +882,7 @@ class OSRequestUpdateProperties: OneSignalRequest, OSUserRequest {
866882 guard let parameters = self . parameters else {
867883 return true
868884 }
869- if parameters [ " deltas " ] != nil {
885+ if parameters [ " deltas " ] != nil { // , !parameters["deltas"].isEmpty
870886 if let pushSubscriptionId = OneSignalUserManagerImpl . sharedInstance. pushSubscriptionId {
871887 var additionalHeaders = self . additionalHeaders ?? [ String: String] ( )
872888 additionalHeaders [ " OneSignal-Subscription-Id " ] = pushSubscriptionId
@@ -931,6 +947,7 @@ class OSRequestUpdateProperties: OneSignalRequest, OSUserRequest {
931947 this request because they will be created with ``OSRequestCreateUser``.
932948 */
933949class OSRequestCreateSubscription : OneSignalRequest , OSUserRequest {
950+ var sentToClient = false
934951 let stringDescription : String
935952 override var description : String {
936953 return stringDescription
@@ -1001,57 +1018,46 @@ class OSRequestCreateSubscription: OneSignalRequest, OSUserRequest {
10011018
10021019/**
10031020 Transfers the Subscription specified by the subscriptionId to the User identified by the identity in the payload.
1004- Only one entry is allowed, `onesignal_id` or an Alias. We will not use the identityModel at all if there is an alias specified.
1021+ Only one entry is allowed, `onesignal_id` or an Alias. We will use the alias specified.
10051022 The anticipated usage of this request is only for push subscriptions.
10061023 */
10071024class OSRequestTransferSubscription : OneSignalRequest , OSUserRequest {
1025+ var sentToClient = false
10081026 let stringDescription : String
10091027 override var description : String {
10101028 return stringDescription
10111029 }
10121030
1013- let subscriptionModel : OSSubscriptionModel
1014- let identityModel : OSIdentityModel ?
1015- let aliasLabel : String ?
1016- let aliasId : String ?
1031+ var subscriptionModel : OSSubscriptionModel
1032+ let aliasLabel : String
1033+ let aliasId : String
10171034
10181035 // Need an alias and subscription_id
10191036 func prepareForExecution( ) -> Bool {
10201037 if let subscriptionId = subscriptionModel. subscriptionId, let appId = OneSignalConfigManager . getAppId ( ) {
10211038 self . path = " apps/ \( appId) /subscriptions/ \( subscriptionId) /owner "
10221039 // Check alias pair
1023- if let label = aliasLabel,
1024- let id = aliasId {
1025- // parameters should be set in init(), so not optional
1026- self . parameters ? [ " identity " ] = [ label: id]
1027- return true
1028- }
1029- if let identityModel = identityModel, let onesignalId = identityModel. onesignalId {
1030- self . parameters ? [ " identity " ] = [ OS_ONESIGNAL_ID: onesignalId]
1031- self . addJWTHeader ( identityModel: identityModel)
1032- return true
1033- } else {
1034- return false
1035- }
1040+ // parameters should be set in init(), so not optional
1041+ self . parameters ? [ " identity " ] = [ aliasLabel: aliasId]
1042+ // TODO: self.addJWTHeader(identityModel: identityModel) ??
1043+ return true
10361044 } else {
10371045 self . path = " " // self.path is non-nil, so set to empty string
10381046 return false
10391047 }
10401048 }
10411049
10421050 /**
1043- Must pass either an `identityModel` or an Alias pair to identify the User.
1051+ Must pass an Alias pair to identify the User.
10441052 If `retainPreviousUser` flag is not passed in, it defaults to `true`.
10451053 */
10461054 init (
10471055 subscriptionModel: OSSubscriptionModel ,
1048- aliasLabel: String ? ,
1049- aliasId: String ? ,
1050- identityModel: OSIdentityModel ? ,
1056+ aliasLabel: String ,
1057+ aliasId: String ,
10511058 retainPreviousUser: Bool ?
10521059 ) {
10531060 self . subscriptionModel = subscriptionModel
1054- self . identityModel = identityModel
10551061 self . aliasLabel = aliasLabel
10561062 self . aliasId = aliasId
10571063 self . stringDescription = " OSRequestTransferSubscription "
@@ -1063,7 +1069,6 @@ class OSRequestTransferSubscription: OneSignalRequest, OSUserRequest {
10631069
10641070 func encode( with coder: NSCoder ) {
10651071 coder. encode ( subscriptionModel, forKey: " subscriptionModel " )
1066- coder. encode ( identityModel, forKey: " identityModel " )
10671072 coder. encode ( aliasLabel, forKey: " aliasLabel " )
10681073 coder. encode ( aliasId, forKey: " aliasId " )
10691074 coder. encode ( parameters, forKey: " parameters " )
@@ -1074,6 +1079,8 @@ class OSRequestTransferSubscription: OneSignalRequest, OSUserRequest {
10741079 required init ? ( coder: NSCoder ) {
10751080 guard
10761081 let subscriptionModel = coder. decodeObject ( forKey: " subscriptionModel " ) as? OSSubscriptionModel ,
1082+ let aliasLabel = coder. decodeObject ( forKey: " aliasLabel " ) as? String ,
1083+ let aliasId = coder. decodeObject ( forKey: " aliasId " ) as? String ,
10771084 let rawMethod = coder. decodeObject ( forKey: " method " ) as? UInt32 ,
10781085 let parameters = coder. decodeObject ( forKey: " parameters " ) as? [ String : Any ] ,
10791086 let timestamp = coder. decodeObject ( forKey: " timestamp " ) as? Date
@@ -1082,9 +1089,8 @@ class OSRequestTransferSubscription: OneSignalRequest, OSUserRequest {
10821089 return nil
10831090 }
10841091 self . subscriptionModel = subscriptionModel
1085- self . identityModel = coder. decodeObject ( forKey: " identityModel " ) as? OSIdentityModel
1086- self . aliasLabel = coder. decodeObject ( forKey: " aliasLabel " ) as? String
1087- self . aliasId = coder. decodeObject ( forKey: " aliasId " ) as? String
1092+ self . aliasLabel = aliasLabel
1093+ self . aliasId = aliasId
10881094 self . stringDescription = " OSRequestTransferSubscription "
10891095 super. init ( )
10901096 self . parameters = parameters
@@ -1098,6 +1104,7 @@ class OSRequestTransferSubscription: OneSignalRequest, OSUserRequest {
10981104 Currently, only the Push Subscription will make this Update Request.
10991105 */
11001106class OSRequestUpdateSubscription : OneSignalRequest , OSUserRequest {
1107+ var sentToClient = false
11011108 let stringDescription : String
11021109 override var description : String {
11031110 return stringDescription
@@ -1176,6 +1183,7 @@ class OSRequestUpdateSubscription: OneSignalRequest, OSUserRequest {
11761183 - Remark: If this model did not already exist in the store, no request is created.
11771184 */
11781185class OSRequestDeleteSubscription : OneSignalRequest , OSUserRequest {
1186+ var sentToClient = false
11791187 let stringDescription : String
11801188 override var description : String {
11811189 return stringDescription
0 commit comments