From 91ab15c428872489fd2d4a0ac3dd75bcba3aeb6a Mon Sep 17 00:00:00 2001 From: Umair Date: Fri, 15 Nov 2024 02:04:27 +0000 Subject: [PATCH] Add and improve logging for Messages and Reactions --- Sources/AblyChat/DefaultOccupancy.swift | 2 +- Sources/AblyChat/DefaultPresence.swift | 14 +++++++------- Sources/AblyChat/Events.swift | 4 ++++ Sources/AblyChat/Presence.swift | 1 + 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Sources/AblyChat/DefaultOccupancy.swift b/Sources/AblyChat/DefaultOccupancy.swift index 45c0c501..380b6029 100644 --- a/Sources/AblyChat/DefaultOccupancy.swift +++ b/Sources/AblyChat/DefaultOccupancy.swift @@ -23,7 +23,7 @@ internal final class DefaultOccupancy: Occupancy, EmitsDiscontinuities { internal func subscribe(bufferingPolicy: BufferingPolicy) async -> Subscription { logger.log(message: "Subscribing to occupancy events", level: .debug) let subscription = Subscription(bufferingPolicy: bufferingPolicy) - channel.subscribe("[meta]occupancy") { [logger] message in + channel.subscribe(OccupancyEvents.meta.rawValue) { [logger] message in logger.log(message: "Received occupancy message: \(message)", level: .debug) guard let data = message.data as? [String: Any], let metrics = data["metrics"] as? [String: Any] diff --git a/Sources/AblyChat/DefaultPresence.swift b/Sources/AblyChat/DefaultPresence.swift index 98e4b0ca..789c2a16 100644 --- a/Sources/AblyChat/DefaultPresence.swift +++ b/Sources/AblyChat/DefaultPresence.swift @@ -24,8 +24,8 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { return try await withCheckedThrowingContinuation { continuation in channel.presence.get { [processPresenceGet] members, error in do { - try processPresenceGet(continuation, members, error) - // No need to resume continuation here as it's handled inside processPresenceGet + let presenceMembers = try processPresenceGet(members, error) + continuation.resume(returning: presenceMembers) } catch { continuation.resume(throwing: error) // processPresenceGet will log any errors @@ -39,8 +39,8 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { return try await withCheckedThrowingContinuation { continuation in channel.presence.get(params.asARTRealtimePresenceQuery()) { [processPresenceGet] members, error in do { - try processPresenceGet(continuation, members, error) - // No need to resume continuation here as it's handled inside processPresenceGet + let presenceMembers = try processPresenceGet(members, error) + continuation.resume(returning: presenceMembers) } catch { continuation.resume(throwing: error) // processPresenceGet will log any errors @@ -147,7 +147,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { } private func decodePresenceData(from data: Any?) -> PresenceData? { - guard let userData = (data as? [String: Any]) else { + guard let userData = data as? [String: Any] else { return nil } @@ -162,7 +162,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { } } - private func processPresenceGet(continuation: CheckedContinuation<[PresenceMember], any Error>, members: [ARTPresenceMessage]?, error: ARTErrorInfo?) throws { + private func processPresenceGet(members: [ARTPresenceMessage]?, error: ARTErrorInfo?) throws -> [PresenceMember] { guard let members else { let error = error ?? ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data or text") logger.log(message: error.message, level: .error) @@ -203,7 +203,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities { logger.log(message: "Returning presence member: \(presenceMember)", level: .debug) return presenceMember } - continuation.resume(returning: presenceMembers) + return presenceMembers } private func processPresenceSubscribe(_ message: ARTPresenceMessage, for event: PresenceEventType) throws -> PresenceEvent { diff --git a/Sources/AblyChat/Events.swift b/Sources/AblyChat/Events.swift index 73141dee..31f27c29 100644 --- a/Sources/AblyChat/Events.swift +++ b/Sources/AblyChat/Events.swift @@ -5,3 +5,7 @@ internal enum MessageEvent: String { internal enum RoomReactionEvents: String { case reaction = "roomReaction" } + +internal enum OccupancyEvents: String { + case meta = "[meta]occupancy" +} diff --git a/Sources/AblyChat/Presence.swift b/Sources/AblyChat/Presence.swift index d7bbbd81..7c58ba88 100644 --- a/Sources/AblyChat/Presence.swift +++ b/Sources/AblyChat/Presence.swift @@ -1,5 +1,6 @@ import Ably +// TODO: (https://github.com/ably-labs/ably-chat-swift/issues/13): try to improve this type public enum PresenceCustomData: Sendable, Codable, Equatable { case string(String) case number(Int) // Changed from NSNumber to Int to conform to Codable. Address in linked issue above.