Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jguz-pubnub committed Nov 19, 2024
1 parent 03fc069 commit 100b6c9
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 158 deletions.
345 changes: 262 additions & 83 deletions Sources/PubNub/APIs/Objects+PubNub.swift

Large diffs are not rendered by default.

60 changes: 31 additions & 29 deletions Sources/PubNub/KMP/KMPPubNub+AppContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ extension KMPPubNub {
}
}

// swiftlint:disable todo
// TODO: Swift SDK allows to sort by the status field, it's not present in KMP

private func mapToMembershipSortFields(from array: [String]) -> [PubNub.MembershipSortField] {
array.compactMap {
switch $0 {
Expand All @@ -69,15 +66,21 @@ extension KMPPubNub {
return PubNub.MembershipSortField(property: .object(.name))
case "channel.updated", "uuid.updated":
return PubNub.MembershipSortField(property: .object(.updated))
case "channel.type", "uuid.type":
return PubNub.MembershipSortField(property: .object(.type))
case "channel.status", "uuid.status":
return PubNub.MembershipSortField(property: .object(.status))
case "updated":
return PubNub.MembershipSortField(property: .updated)
case "status":
return PubNub.MembershipSortField(property: .status)
case "type":
return PubNub.MembershipSortField(property: .type)
default:
return nil
}
}
}

// swiftlint:enable todo
}

@objc
Expand Down Expand Up @@ -118,7 +121,7 @@ public extension KMPPubNub {
onSuccess: @escaping ((KMPChannelMetadata) -> Void),
onFailure: @escaping ((Error) -> Void)
) {
pubnub.fetch(channel: channel, include: includeCustom) {
pubnub.fetch(channel: channel, include: PubNub.IncludeFields(custom: includeCustom)) {
switch $0 {
case .success(let metadata):
onSuccess(KMPChannelMetadata(metadata: metadata))
Expand All @@ -139,17 +142,15 @@ public extension KMPPubNub {
onSuccess: @escaping ((KMPChannelMetadata) -> Void),
onFailure: @escaping ((Error) -> Void)
) {
pubnub.set(
channel: PubNubChannelMetadataBase(
metadataId: channel,
name: name,
type: type,
status: status,
channelDescription: description,
custom: convertDictionaryToScalars(custom?.asMap())
),
include: includeCustom
) {
let channelMetadata = PubNubChannelMetadataBase(
metadataId: channel,
name: name,
type: type,
status: status,
channelDescription: description,
custom: convertDictionaryToScalars(custom?.asMap())
)
pubnub.set(channel: channelMetadata, include: PubNub.IncludeFields(custom: includeCustom)) {
switch $0 {
case .success(let metadata):
onSuccess(KMPChannelMetadata(metadata: metadata))
Expand Down Expand Up @@ -210,7 +211,7 @@ public extension KMPPubNub {
onSuccess: @escaping ((KMPUUIDMetadata) -> Void),
onFailure: @escaping ((Error) -> Void)
) {
pubnub.fetch(uuid: uuid, include: includeCustom) {
pubnub.fetch(uuid: uuid, include: PubNub.IncludeFields(custom: includeCustom)) {
switch $0 {
case .success(let metadata):
onSuccess(KMPUUIDMetadata(metadata: metadata))
Expand All @@ -233,18 +234,19 @@ public extension KMPPubNub {
onSuccess: @escaping ((KMPUUIDMetadata) -> Void),
onFailure: @escaping ((Error) -> Void)
) {
let uuidMetadata = PubNubUUIDMetadataBase(
metadataId: uuid ?? pubnub.configuration.userId,
name: name,
type: type,
status: status,
externalId: externalId,
profileURL: profileUrl,
email: email,
custom: convertDictionaryToScalars(custom?.asMap())
)
pubnub.set(
uuid: PubNubUUIDMetadataBase(
metadataId: uuid ?? pubnub.configuration.userId,
name: name,
type: type,
status: status,
externalId: externalId,
profileURL: profileUrl,
email: email,
custom: convertDictionaryToScalars(custom?.asMap())
),
include: includeCustom
uuid: uuidMetadata,
include: PubNub.IncludeFields(custom: includeCustom)
) {
switch $0 {
case .success(let metadata):
Expand Down
2 changes: 2 additions & 0 deletions Sources/PubNub/KMP/KMPPubNub+History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public extension KMPPubNub {
includeMeta: Bool,
includeMessageActions: Bool,
includeMessageType: Bool,
includeCustomMessageType: Bool,
page: KMPBoundedPage?,
onSuccess: @escaping ((KMPFetchMessagesResult)) -> Void,
onFailure: @escaping ((Error) -> Void)
Expand All @@ -35,6 +36,7 @@ public extension KMPPubNub {
includeMeta: includeMeta,
includeUUID: includeUUID,
includeMessageType: includeMessageType,
includeCustomMessageType: includeCustomMessageType,
page: PubNubBoundedPageBase(
start: page?.start?.uint64Value,
end: page?.end?.uint64Value,
Expand Down
2 changes: 2 additions & 0 deletions Sources/PubNub/KMP/Wrappers/KMPAppContextEventResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public class KMPMembershipMetadata: NSObject {
@objc public var uuidMetadataId: String
@objc public var channelMetadataId: String
@objc public var status: String?
@objc public var type: String?
@objc public var uuid: KMPUUIDMetadata?
@objc public var channel: KMPChannelMetadata?
@objc public var updated: String?
Expand All @@ -336,6 +337,7 @@ public class KMPMembershipMetadata: NSObject {
self.uuidMetadataId = from.uuidMetadataId
self.channelMetadataId = from.channelMetadataId
self.status = from.status
self.type = from.type
self.uuid = if let uuid = from.uuid { KMPUUIDMetadata(metadata: uuid) } else { nil }
self.channel = if let channel = from.channel { KMPChannelMetadata(metadata: channel) } else { nil }
self.updated = if let date = from.updated { DateFormatter.iso8601.string(from: date) } else { nil }
Expand Down
2 changes: 2 additions & 0 deletions Sources/PubNub/KMP/Wrappers/KMPMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class KMPMessage: NSObject {
self.published = message.published
self.metadata = if let value = message.metadata { KMPAnyJSON(value.codableValue) } else { nil }
self.messageType = message.messageType.rawValue
self.customMessageType = message.customMessageType
self.error = message.error
}

Expand All @@ -39,5 +40,6 @@ public class KMPMessage: NSObject {
@objc public let published: Timetoken
@objc public let metadata: KMPAnyJSON?
@objc public let messageType: Int
@objc public let customMessageType: String?
@objc public let error: Error?
}
4 changes: 2 additions & 2 deletions Sources/PubNub/Models/PubNubMembershipMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension PubNubMembershipMetadataBase: Codable {
concreteCustom = try container.decodeIfPresent([String: JSONCodableScalarType].self, forKey: .custom)
status = try container.decodeIfPresent(String.self, forKey: .status)
type = try container.decodeIfPresent(String.self, forKey: .type)

if let concreteChannel = try? container.decodeIfPresent(PubNubChannelMetadataBase.self, forKey: .channel) {
self.concreteChannel = concreteChannel
channelMetadataId = concreteChannel.metadataId
Expand All @@ -203,7 +203,7 @@ extension PubNubMembershipMetadataBase: Codable {
try container.encodeIfPresent(custom?.mapValues { $0.scalarValue }, forKey: .custom)
try container.encodeIfPresent(status, forKey: .status)
try container.encodeIfPresent(type, forKey: .type)

if let channelObject = concreteChannel {
try container.encode(channelObject, forKey: .channel)
} else {
Expand Down
38 changes: 18 additions & 20 deletions Sources/PubNub/Networking/Routers/ObjectsChannelRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@

import Foundation

extension Array where Element == ObjectsChannelRouter.Include {
func values() -> String {
return map { $0.rawValue }.csvString
}
}

public struct ObjectsChannelRouter: HTTPRouter {
public enum Endpoint: CustomStringConvertible {
case all(
customFields: Bool, totalCount: Bool, filter: String?, sort: [String], limit: Int?, start: String?, end: String?
)
case fetch(metadataId: String, customFields: Bool)
case set(metadata: PubNubChannelMetadata, customFields: Bool)
case all(include: [Include]?, totalCount: Bool, filter: String?, sort: [String], limit: Int?, start: String?, end: String?)
case fetch(metadataId: String, include: [Include]?)
case set(metadata: PubNubChannelMetadata, include: [Include]?)
case remove(metadataId: String)

public var description: String {
Expand All @@ -42,19 +46,13 @@ public struct ObjectsChannelRouter: HTTPRouter {
var custom: [String: JSONCodableScalarType]?
}

enum Include {
static let custom = "custom"
static let status = "status"
static let type = "type"

static func includes(custom: Bool) -> [String] {
var includes = [Include.status, Include.type]

if custom {
includes.append(Include.custom)
}
public enum Include: String {
case custom
case status
case type

return includes
static func includes(from array: [Include]?) -> String? {
array?.map { $0.rawValue }.csvString
}
}

Expand Down Expand Up @@ -100,14 +98,14 @@ public struct ObjectsChannelRouter: HTTPRouter {
query.appendIfPresent(key: .filter, value: filter)
query.appendIfNotEmpty(key: .sort, value: sort)
query.appendIfPresent(key: .limit, value: limit?.description)
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
query.appendIfPresent(key: .count, value: totalCount ? totalCount.description : nil)
query.appendIfPresent(key: .start, value: start?.description)
query.appendIfPresent(key: .end, value: end?.description)
case let .fetch(_, customFields):
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
case let .set(_, customFields):
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
case .remove:
break
}
Expand Down
38 changes: 14 additions & 24 deletions Sources/PubNub/Networking/Routers/ObjectsUUIDRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import Foundation

public struct ObjectsUUIDRouter: HTTPRouter {
public enum Endpoint: CustomStringConvertible {
case all(customFields: Bool, totalCount: Bool, filter: String?, sort: [String], limit: Int?, start: String?, end: String?)
case fetch(metadataId: String, customFields: Bool)
case set(metadata: PubNubUUIDMetadata, customFields: Bool)
case all(include: [Include]?, totalCount: Bool, filter: String?, sort: [String], limit: Int?, start: String?, end: String?)
case fetch(metadataId: String, include: [Include]?)
case set(metadata: PubNubUUIDMetadata, include: [Include]?)
case remove(metadataId: String)

public var description: String {
Expand Down Expand Up @@ -53,19 +53,13 @@ public struct ObjectsUUIDRouter: HTTPRouter {
}
}

enum Include {
static let custom = "custom"
static let status = "status"
static let type = "type"
public enum Include: String {
case custom
case status
case type

static func includes(custom: Bool) -> [String] {
var includes = [Include.status, Include.type]

if custom {
includes.append(Include.custom)
}

return includes
static func includes(from array: [Include]?) -> String? {
array?.map { $0.rawValue }.csvString
}
}

Expand Down Expand Up @@ -111,14 +105,14 @@ public struct ObjectsUUIDRouter: HTTPRouter {
query.appendIfPresent(key: .filter, value: filter)
query.appendIfNotEmpty(key: .sort, value: sort)
query.appendIfPresent(key: .limit, value: limit?.description)
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
query.appendIfPresent(key: .count, value: totalCount ? totalCount.description : nil)
query.appendIfPresent(key: .start, value: start?.description)
query.appendIfPresent(key: .end, value: end?.description)
case let .fetch(_, customFields):
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
case let .set(_, customFields):
query.appendIfPresent(key: .include, value: Include.includes(custom: customFields).csvString)
query.appendIfPresent(key: .include, value: Include.includes(from: customFields))
case .remove:
break
}
Expand Down Expand Up @@ -176,12 +170,8 @@ extension ObjectsUUIDRouter.Endpoint: Equatable {
lhs: ObjectsUUIDRouter.Endpoint, rhs: ObjectsUUIDRouter.Endpoint
) -> Bool {
switch (lhs, rhs) {
case let (
.all(lhs1, lhs2, lhs3, lhs4, lhs5, lhs6, lhs7),
.all(rhs1, rhs2, rhs3, rhs4, rhs5, rhs6, rhs7)
):
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3 &&
lhs4 == rhs4 && lhs5 == rhs5 && lhs6 == rhs6 && lhs7 == rhs7
case let (.all(lhs1, lhs2, lhs3, lhs4, lhs5, lhs6, lhs7), .all(rhs1, rhs2, rhs3, rhs4, rhs5, rhs6, rhs7)):
return lhs1 == rhs1 && lhs2 == rhs2 && lhs3 == rhs3 && lhs4 == rhs4 && lhs5 == rhs5 && lhs6 == rhs6 && lhs7 == rhs7
case let (.fetch(lhs1, lhs2), .fetch(rhs1, rhs2)):
return lhs1 == rhs1 && lhs2 == rhs2
case let (.set(lhs1, lhs2), .set(rhs1, rhs2)):
Expand Down

0 comments on commit 100b6c9

Please sign in to comment.