Skip to content

Commit aa98bb7

Browse files
move custom payload element from content/payload => customPayload.
1 parent 07c13b3 commit aa98bb7

File tree

5 files changed

+221
-6
lines changed

5 files changed

+221
-6
lines changed

Tests/swift-sdk-swift-tests/InAppHelperTests.swift

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,193 @@ class InAppHelperTests: XCTestCase {
303303
let dict = messages[0].trigger.dict as! [String : Any]
304304
TestUtils.validateMatch(keyPath: KeyPath("myPayload.var1"), value: "val1", inDictionary: dict, message: "Expected to find val1")
305305
}
306+
307+
// This tests payload as we are getting now
308+
func testInAppParsingWithCurrentPayload() {
309+
let customPayload1 = """
310+
{
311+
"contentType" : "html",
312+
"inAppType" : "default",
313+
"channelName" : "channel1"
314+
}
315+
"""
316+
let customPayload2 = """
317+
{
318+
"contentType" : "html",
319+
"inAppType" : "inBox",
320+
"channelName" : "channel2",
321+
"promoteToContent" : {
322+
"title" : "title",
323+
"subTitle" : "subtitle",
324+
"imageUrl" : "http://somewhere.com/something.jpg"
325+
}
326+
}
327+
"""
328+
329+
let payload = """
330+
{
331+
"inAppMessages" : [
332+
{
333+
"content" : {
334+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>",
335+
"payload" : \(customPayload1),
336+
},
337+
"messageId" : "messageIdxxx",
338+
"campaignId" : "campaignIdxxx",
339+
"trigger" : {
340+
"type" : "myNewKind",
341+
"myPayload" : {"var1" : "val1"}
342+
}
343+
},
344+
{
345+
"content" : {
346+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>",
347+
"payload" : \(customPayload2),
348+
},
349+
"messageId" : "messageIdxxx",
350+
"campaignId" : "campaignIdxxx",
351+
"trigger" : {
352+
"type" : "myNewKind",
353+
"myPayload" : {"var1" : "val1"}
354+
}
355+
},
356+
{
357+
"content" : {
358+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>",
359+
"payload" : {
360+
},
361+
},
362+
"messageId" : "messageIdxxx",
363+
"campaignId" : "campaignIdxxx",
364+
"trigger" : {
365+
"type" : "myNewKind",
366+
"myPayload" : {"var1" : "val1"}
367+
}
368+
},
369+
{
370+
"content" : {
371+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
372+
},
373+
"messageId" : "messageIdxxx",
374+
"campaignId" : "campaignIdxxx",
375+
"trigger" : {
376+
"type" : "myNewKind",
377+
"myPayload" : {"var1" : "val1"}
378+
}
379+
}
380+
]
381+
}
382+
""".toJsonDict()
383+
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
384+
385+
XCTAssertEqual(messages.count, 4)
386+
let message1 = messages[0]
387+
XCTAssertEqual(message1.channelName, "channel1")
388+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.extraInfo!, dict2: customPayload1.toJsonDict()))
389+
390+
let message2 = messages[1]
391+
XCTAssertEqual(message2.channelName, "channel2")
392+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.extraInfo!, dict2: customPayload2.toJsonDict()))
393+
394+
let message3 = messages[2]
395+
XCTAssertEqual(message3.channelName, "")
396+
397+
let message4 = messages[3]
398+
XCTAssertEqual(message4.channelName, "")
399+
}
400+
401+
// This tests payload as we expect to get when backend is fixed
402+
func testInAppParsingWithFuturePayload() {
403+
let customPayload1 = """
404+
{
405+
"contentType" : "html",
406+
"inAppType" : "default",
407+
"channelName" : "channel1"
408+
}
409+
"""
410+
let customPayload2 = """
411+
{
412+
"contentType" : "html",
413+
"inAppType" : "inBox",
414+
"channelName" : "channel2",
415+
"promoteToContent" : {
416+
"title" : "title",
417+
"subTitle" : "subtitle",
418+
"imageUrl" : "http://somewhere.com/something.jpg"
419+
}
420+
}
421+
"""
422+
423+
let payload = """
424+
{
425+
"inAppMessages" : [
426+
{
427+
"content" : {
428+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
429+
},
430+
"messageId" : "messageIdxxx",
431+
"campaignId" : "campaignIdxxx",
432+
"trigger" : {
433+
"type" : "myNewKind",
434+
"myPayload" : {"var1" : "val1"}
435+
},
436+
"customPayload" : \(customPayload1)
437+
},
438+
{
439+
"content" : {
440+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
441+
},
442+
"messageId" : "messageIdxxx",
443+
"campaignId" : "campaignIdxxx",
444+
"trigger" : {
445+
"type" : "myNewKind",
446+
"myPayload" : {"var1" : "val1"}
447+
},
448+
"customPayload" : \(customPayload2)
449+
},
450+
{
451+
"content" : {
452+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
453+
},
454+
"messageId" : "messageIdxxx",
455+
"campaignId" : "campaignIdxxx",
456+
"trigger" : {
457+
"type" : "myNewKind",
458+
"myPayload" : {"var1" : "val1"}
459+
},
460+
"customPayload" : {}
461+
},
462+
{
463+
"content" : {
464+
"html" : "<a href=\\"http://somewhere.com\\">Click here</a>"
465+
},
466+
"messageId" : "messageIdxxx",
467+
"campaignId" : "campaignIdxxx",
468+
"trigger" : {
469+
"type" : "myNewKind",
470+
"myPayload" : {"var1" : "val1"}
471+
}
472+
}
473+
]
474+
}
475+
""".toJsonDict()
476+
let messages = InAppHelper.inAppMessages(fromPayload: payload, internalApi: IterableAPI.internalImplementation!)
477+
478+
XCTAssertEqual(messages.count, 4)
479+
let message1 = messages[0]
480+
XCTAssertEqual(message1.channelName, "channel1")
481+
XCTAssertTrue(TestUtils.areEqual(dict1: message1.extraInfo!, dict2: customPayload1.toJsonDict()))
482+
483+
let message2 = messages[1]
484+
XCTAssertEqual(message2.channelName, "channel2")
485+
XCTAssertTrue(TestUtils.areEqual(dict1: message2.extraInfo!, dict2: customPayload2.toJsonDict()))
486+
487+
let message3 = messages[2]
488+
XCTAssertEqual(message3.channelName, "")
489+
490+
let message4 = messages[3]
491+
XCTAssertEqual(message4.channelName, "")
492+
}
306493

307494
private func createInAppPayload(withExtraInfo extraInfo: [AnyHashable : Any]) -> [AnyHashable : Any] {
308495
return [

Tests/swift-sdk-swift-tests/TestUtils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ struct TestUtils {
6060
return TestHelper.clearTestUserDefaults()
6161
}
6262

63+
static func areEqual(dict1: [AnyHashable : Any], dict2: [AnyHashable : Any]) -> Bool {
64+
return NSDictionary(dictionary: dict1).isEqual(to: dict2)
65+
}
66+
6367
private static func validateQueryParameters(inUrlComponents urlComponents: URLComponents, queryParams: [(name:String, value:String)]) {
6468
queryParams.forEach { (name, value) in
6569
validateQueryParameter(inUrlComponents: urlComponents, withName: name, andValue: value)

swift-sdk/ITBConsts.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public extension AnyHashable {
156156
public static let ITBL_IN_APP_HTML = "html"
157157
public static let ITBL_IN_APP_HREF = "href"
158158
public static let ITBL_IN_APP_DISPLAY_SETTINGS = "inAppDisplaySettings"
159-
public static let ITBL_IN_APP_PAYLOAD = "payload"
159+
public static let ITBL_IN_APP_LEGACY_PAYLOAD = "payload"
160+
public static let ITBL_IN_APP_CUSTOM_PAYLOAD = "customPayload"
161+
public static let ITBL_IN_APP_CHANNEL_NAME = "channelName"
160162
}
161163

swift-sdk/Internal/InAppHelper.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,31 @@ struct InAppHelper {
324324

325325
private static func parseInApps(fromPayload payload: [AnyHashable : Any]) -> [InAppParseResult] {
326326
return getInAppDicts(fromPayload: payload).map {
327-
parseInApp(fromDict: $0)
327+
parseInApp(fromDict: preProcess(dict:$0))
328328
}
329329
}
330+
331+
// Change the in-app payload coming from the server to one that we expect it to be like
332+
// This is temporary until we fix the backend to do the right thing.
333+
// 1. Move 'payload' from inside 'content' to top level 'customPayload'
334+
// 2. form this 'customPayload' move elements to top level making sure not to
335+
// override 'messageId' etc. Moving elements to top level will help us simulate when we add these
336+
// elements later in backend
337+
#warning("Remove when we have backend support")
338+
private static func preProcess(dict: [AnyHashable : Any]) -> [AnyHashable : Any] {
339+
var result = dict
340+
guard let contentDict = dict[.ITBL_IN_APP_CONTENT] as? [AnyHashable : Any], let legacyPayloadDict = contentDict[.ITBL_IN_APP_LEGACY_PAYLOAD] as? [AnyHashable : Any] else {
341+
return result
342+
}
343+
344+
var newContentDict = contentDict
345+
newContentDict[.ITBL_IN_APP_LEGACY_PAYLOAD] = nil
346+
result[.ITBL_IN_APP_CONTENT] = newContentDict
347+
348+
result[.ITBL_IN_APP_CUSTOM_PAYLOAD] = legacyPayloadDict
349+
350+
return result
351+
}
330352

331353
private static func parseInApp(fromDict dict: [AnyHashable : Any]) -> InAppParseResult {
332354
guard let content = dict[.ITBL_IN_APP_CONTENT] as? [AnyHashable : Any] else {
@@ -350,7 +372,7 @@ struct InAppHelper {
350372
campaignId = ""
351373
}
352374

353-
let extraInfo = parseExtraInfo(fromContent: content)
375+
let extraInfo = parseCustomPayload(fromPayload: dict)
354376

355377
// this is temporary until we fix backend
356378
let channelName = extraInfo?["channelName"] as? String ?? ""
@@ -390,8 +412,8 @@ struct InAppHelper {
390412
return IterableInAppTrigger(dict: element)
391413
}
392414

393-
private static func parseExtraInfo(fromContent content: [AnyHashable : Any]) -> [AnyHashable : Any]? {
394-
return content[.ITBL_IN_APP_PAYLOAD] as? [AnyHashable : Any]
415+
private static func parseCustomPayload(fromPayload payload: [AnyHashable : Any]) -> [AnyHashable : Any]? {
416+
return payload[.ITBL_IN_APP_CUSTOM_PAYLOAD] as? [AnyHashable : Any]
395417
}
396418

397419
private static func toMessage(fromInAppParseResult inAppParseResult: InAppHelper.InAppParseResult, internalApi: IterableAPIInternal) -> IterableInAppMessage? {

swift-sdk/IterableInApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public final class IterableInAppMessage : NSObject {
144144
init(
145145
messageId: String,
146146
campaignId: String,
147-
channelName: String = "reserved",
147+
channelName: String = "",
148148
contentType: IterableInAppContentType = .html,
149149
trigger: IterableInAppTrigger = .defaultTrigger,
150150
expiresAt: Date? = nil,

0 commit comments

Comments
 (0)