Skip to content

Commit

Permalink
[MOB-9233] Updated code according to the new discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeruchat committed Jan 7, 2025
1 parent a3a059b commit db1400e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
85 changes: 58 additions & 27 deletions swift-sdk/Internal/in-app/InAppMessageParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
9 changes: 6 additions & 3 deletions tests/unit-tests/InAppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit db1400e

Please sign in to comment.