From db1400ed25385ddd75a395c635a2d506d93ff750 Mon Sep 17 00:00:00 2001 From: Sumeru Chatterjee Date: Tue, 7 Jan 2025 15:04:33 +0100 Subject: [PATCH] [MOB-9233] Updated code according to the new discussion --- .../Internal/in-app/InAppMessageParser.swift | 85 +++++++++++++------ tests/unit-tests/InAppTests.swift | 9 +- 2 files changed, 64 insertions(+), 30 deletions(-) diff --git a/swift-sdk/Internal/in-app/InAppMessageParser.swift b/swift-sdk/Internal/in-app/InAppMessageParser.swift index f5840b01..220f2cff 100644 --- a/swift-sdk/Internal/in-app/InAppMessageParser.swift +++ b/swift-sdk/Internal/in-app/InAppMessageParser.swift @@ -81,43 +81,74 @@ struct InAppMessageParser { return .failure(.parseFailed(reason: "no messageId", messageId: nil)) } - guard let contentDict = json[JsonKey.InApp.content] as? [AnyHashable: Any] else { - return .failure(.parseFailed(reason: "no content in json payload", messageId: messageId)) - } - - let content: IterableInAppContent let jsonOnly = (json[JsonKey.InApp.jsonOnly] as? Int ?? 0) == 1 - - switch InAppContentParser.parse(contentDict: contentDict) { - case let .success(parsedContent): - content = parsedContent - case let .failure(reason): - return .failure(.parseFailed(reason: reason, messageId: messageId)) - } + let customPayload = parseCustomPayload(fromPayload: json) + // For non-JSON-only messages, we require content + if !jsonOnly { + guard let contentDict = json[JsonKey.InApp.content] as? [AnyHashable: Any] else { + return .failure(.parseFailed(reason: "no content in json payload", messageId: messageId)) + } + + let content: IterableInAppContent + switch InAppContentParser.parse(contentDict: contentDict) { + case let .success(parsedContent): + content = parsedContent + case let .failure(reason): + return .failure(.parseFailed(reason: reason, messageId: messageId)) + } + + return .success(createMessage( + messageId: messageId, + json: json, + content: content, + customPayload: customPayload, + jsonOnly: jsonOnly + )) + } else { + // For JSON-only messages, use default HTML content + let content = IterableHtmlInAppContent(edgeInsets: .zero, html: "") + + return .success(createMessage( + messageId: messageId, + json: json, + content: content, + customPayload: customPayload, + jsonOnly: jsonOnly + )) + } + } + + private static func createMessage( + messageId: String, + json: [AnyHashable: Any], + content: IterableInAppContent, + customPayload: [AnyHashable: Any]?, + jsonOnly: Bool + ) -> IterableInAppMessage { let campaignId = json[JsonKey.campaignId] as? NSNumber - - let saveToInbox = json[JsonKey.saveToInbox] as? Bool ?? false + let saveToInbox = (json[JsonKey.saveToInbox] as? Bool ?? false) && !jsonOnly // Force false for JSON-only let inboxMetadata = parseInboxMetadata(fromPayload: json) let trigger = parseTrigger(fromTriggerElement: json[JsonKey.InApp.trigger] as? [AnyHashable: Any]) - let customPayload = parseCustomPayload(fromPayload: json) let createdAt = parseTime(withKey: JsonKey.inboxCreatedAt, fromJson: json) let expiresAt = parseTime(withKey: JsonKey.inboxExpiresAt, fromJson: json) let read = json[JsonKey.read] as? Bool ?? false let priorityLevel = json[JsonKey.priorityLevel] as? Double ?? Const.PriorityLevel.unassigned - return .success(IterableInAppMessage(messageId: messageId, - campaignId: campaignId, - trigger: trigger, - createdAt: createdAt, - expiresAt: expiresAt, - content: content, - saveToInbox: saveToInbox, - inboxMetadata: inboxMetadata, - customPayload: customPayload, - read: read, - priorityLevel: priorityLevel, - jsonOnly: jsonOnly)) + return IterableInAppMessage( + messageId: messageId, + campaignId: campaignId, + trigger: trigger, + createdAt: createdAt, + expiresAt: expiresAt, + content: content, + saveToInbox: saveToInbox, + inboxMetadata: inboxMetadata, + customPayload: customPayload, + read: read, + priorityLevel: priorityLevel, + jsonOnly: jsonOnly + ) } private static func parseTime(withKey key: AnyHashable, fromJson json: [AnyHashable: Any]) -> Date? { diff --git a/tests/unit-tests/InAppTests.swift b/tests/unit-tests/InAppTests.swift index 5b0dd01f..318ea6a1 100644 --- a/tests/unit-tests/InAppTests.swift +++ b/tests/unit-tests/InAppTests.swift @@ -1624,7 +1624,7 @@ class InAppTests: XCTestCase { wait(for: [expectation1, expectation2], timeout: testExpectationTimeout / 5) } - func testJsonOnlyInAppMessageRequiresCustomPayload() { + func testJsonOnlyInAppMessageWithoutCustomPayload() { let expectation1 = expectation(description: "message parsed") let mockInAppFetcher = MockInAppFetcher() @@ -1666,9 +1666,12 @@ class InAppTests: XCTestCase { return } - // Message should be ignored since it's marked as jsonOnly but has no customPayload + // Message should be not be ignored even if they are json only and have no payload let messages = internalApi.inAppManager.getMessages() - XCTAssertEqual(messages.count, 0) + XCTAssertEqual(messages.count, 1) + + let message = messages[0] + XCTAssertTrue(message.customPayload == nil) expectation1.fulfill() }