Skip to content

Commit

Permalink
Merge pull request #665 from Iterable/anelson/mob-6648-update-getmess…
Browse files Browse the repository at this point in the history
…ages

[MOB-6648] update get messages to pull messages from placement
  • Loading branch information
evantk91 authored Aug 10, 2023
2 parents 01e2392 + 0bad7d4 commit ae07c49
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion swift-sdk/Internal/ApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ extension ApiClient: ApiClientProtocol {

// MARK: - Embedded Messaging

func getEmbeddedMessages() -> Pending<EmbeddedMessagesPayload, SendRequestError> {
func getEmbeddedMessages() -> Pending<PlacementsPayload, SendRequestError> {
let result = createRequestCreator().flatMap { $0.createGetEmbeddedMessagesRequest() }
return send(iterableRequestResult: result)
}
Expand Down
2 changes: 1 addition & 1 deletion swift-sdk/Internal/ApiClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protocol ApiClientProtocol: AnyObject {

func getRemoteConfiguration() -> Pending<RemoteConfiguration, SendRequestError>

func getEmbeddedMessages() -> Pending<EmbeddedMessagesPayload, SendRequestError>
func getEmbeddedMessages() -> Pending<PlacementsPayload, SendRequestError>

@discardableResult func track(embeddedMessageReceived message: IterableEmbeddedMessage) -> Pending<SendRequestValue, SendRequestError>

Expand Down
22 changes: 14 additions & 8 deletions swift-sdk/Internal/EmbeddedMessagingSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ struct AnyDecodable: Decodable {


struct EmbeddedMessagingSerialization {
static func encode(messages: [IterableEmbeddedMessage]) -> Data {
guard let encoded = try? JSONEncoder().encode(EmbeddedMessagesPayload(embeddedMessages: messages)) else {
ITBError("unable to encode embedded messages into JSON payload")
static func encode(placements: [Placement]) -> Data {
guard let encoded = try? JSONEncoder().encode(PlacementsPayload(placements: placements)) else {
ITBError("unable to encode placements into JSON payload")
return Data()
}

return encoded
}

static func decode(messages: Data) -> [IterableEmbeddedMessage] {
guard let decoded = try? JSONDecoder().decode(EmbeddedMessagesPayload.self, from: messages) else {
ITBError("unable to decode JSON payload into embedded messages")
static func decode(placements: Data) -> [IterableEmbeddedMessage] {
guard let decoded = try? JSONDecoder().decode(PlacementsPayload.self, from: placements) else {
ITBError("unable to decode JSON payload into placements")
return []
}

return decoded.embeddedMessages
let messages = decoded.placements.flatMap { $0.embeddedMessages }
return messages
}

static func encode(payload: [AnyHashable: Any]?) -> Data? {
Expand All @@ -72,10 +73,15 @@ struct EmbeddedMessagingSerialization {
}
}

struct EmbeddedMessagesPayload: Codable {
struct Placement: Codable {
let placementId: Int?
let embeddedMessages: [IterableEmbeddedMessage]
}

struct PlacementsPayload: Codable {
let placements: [Placement]
}

extension IterableEmbeddedMessage: Codable {
enum CodingKeys: String, CodingKey {
case metadata
Expand Down
4 changes: 4 additions & 0 deletions swift-sdk/Internal/EmptyEmbeddedManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class EmptyEmbeddedManager: IterableEmbeddedManagerProtocol {
func getMessages() -> [IterableEmbeddedMessage] {
return []
}

func getMessages(for placementId: Int) -> [IterableEmbeddedMessage] {
return []
}

func syncMessages(completion: @escaping () -> Void) {

Expand Down
33 changes: 22 additions & 11 deletions swift-sdk/Internal/IterableEmbeddedManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class IterableEmbeddedManager: NSObject, IterableEmbeddedManagerProtocol {
return messages
}

public func getMessages(for placementId: Int) -> [IterableEmbeddedMessage] {

return messages.filter { $0.metadata.placementId == placementId }
}

public func addUpdateListener(_ listener: IterableEmbeddedUpdateDelegate) {
listeners.add(listener)
}
Expand Down Expand Up @@ -58,23 +63,29 @@ class IterableEmbeddedManager: NSObject, IterableEmbeddedManagerProtocol {


private func retrieveEmbeddedMessages(completion: @escaping () -> Void) {
print("retrieve embeddeded messages")
apiClient.getEmbeddedMessages()
.onCompletion(
receiveValue: { embeddedMessagesPayload in
let fetchedMessages = embeddedMessagesPayload.embeddedMessages

// TODO: decide if parsing errors should be accounted for here

let processor = EmbeddedMessagingProcessor(currentMessages: self.messages,
fetchedMessages: fetchedMessages)

self.setMessages(processor)
self.trackNewlyRetrieved(processor)
self.notifyUpdateDelegates(processor)
completion()
},
print("got embeddedMessagesPayload")
print(embeddedMessagesPayload)
let placements = embeddedMessagesPayload.placements
let fetchedMessages = placements.flatMap { $0.embeddedMessages }

// TODO: decide if parsing errors should be accounted for here

let processor = EmbeddedMessagingProcessor(currentMessages: self.messages,
fetchedMessages: fetchedMessages)

self.setMessages(processor)
self.trackNewlyRetrieved(processor)
self.notifyUpdateDelegates(processor)
completion()
},

receiveError: { sendRequestError in
print("receive error: \(sendRequestError)")
//TODO: This check can go away once eligibility based retrieval comes in place.
if sendRequestError.reason == "SUBSCRIPTION_INACTIVE" ||
sendRequestError.reason == "Invalid API Key" {
Expand Down
1 change: 1 addition & 0 deletions swift-sdk/IterableEmbeddedManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Foundation

@objc public protocol IterableEmbeddedManagerProtocol {
func getMessages() -> [IterableEmbeddedMessage]
func getMessages(for placementId: Int) -> [IterableEmbeddedMessage]

func addUpdateListener(_ listener: IterableEmbeddedUpdateDelegate)
func removeUpdateListener(_ listener: IterableEmbeddedUpdateDelegate)
Expand Down
12 changes: 10 additions & 2 deletions swift-sdk/IterableEmbeddedMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public final class IterableEmbeddedMessage: NSObject {
self.payload = payload
}

convenience init(messageId: String, campaignId: Int? = nil, isProof: Bool? = nil) {
let metadata = EmbeddedMessageMetadata(messageId: messageId, campaignId: campaignId, isProof: isProof)
convenience init(messageId: String, campaignId: Int? = nil, isProof: Bool? = nil, placementId: Int? = nil) {
let metadata = EmbeddedMessageMetadata(messageId: messageId, campaignId: campaignId, isProof: isProof, placementId: placementId)

self.init(metadata: metadata)
}
Expand All @@ -30,6 +30,14 @@ extension IterableEmbeddedMessage {
public let messageId: String
public let campaignId: Int?
public let isProof: Bool?
public let placementId: Int?

init(messageId: String, campaignId: Int? = nil, isProof: Bool? = nil, placementId: Int? = nil) {
self.messageId = messageId
self.campaignId = campaignId
self.isProof = isProof
self.placementId = placementId
}
}

public struct EmbeddedMessageElements: Codable {
Expand Down
4 changes: 2 additions & 2 deletions tests/embedded-messaging-tests/EmbeddedManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ final class EmbeddedManagerTests: XCTestCase {
return ids.map { IterableEmbeddedMessage(messageId: $0) }
}

override func getEmbeddedMessages() -> IterableSDK.Pending<IterableSDK.EmbeddedMessagesPayload, IterableSDK.SendRequestError> {
override func getEmbeddedMessages() -> IterableSDK.Pending<IterableSDK.PlacementsPayload, IterableSDK.SendRequestError> {
if newMessages {
let messages = EmbeddedMessagesPayload(embeddedMessages: makeBlankMessagesList(with: ["1"]))
let messages = PlacementsPayload(placements: [Placement(placementId: 0, embeddedMessages: makeBlankMessagesList(with: ["1"]))])

return Fulfill(value: messages)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/BlankApiClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BlankApiClient: ApiClientProtocol {
Pending()
}

func getEmbeddedMessages() -> Pending<EmbeddedMessagesPayload, SendRequestError> {
func getEmbeddedMessages() -> Pending<PlacementsPayload, SendRequestError> {
Pending()
}

Expand Down

0 comments on commit ae07c49

Please sign in to comment.