Skip to content

Commit

Permalink
Make handling of presence data consistent
Browse files Browse the repository at this point in the history
There was one place where we throw and one where we fail silently (even
though the surrounding code throws for a bunch of other reasons of
seemingly similar severity). So throw consistently.
  • Loading branch information
lawrence-forooghian committed Dec 10, 2024
1 parent d7b5e53 commit f11e0a1
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Sources/AblyChat/DefaultPresence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,15 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
await featureChannel.onDiscontinuity(bufferingPolicy: bufferingPolicy)
}

private func decodePresenceData(from data: Any?) -> PresenceData? {
guard let userData = data as? [String: Any] else {
return nil
private func decodePresenceData(from data: Any?) throws -> PresenceData? {
guard let data = data as? [String: Any] else {
let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data")
logger.log(message: error.message, level: .error)
throw error
}

do {
let jsonData = try JSONSerialization.data(withJSONObject: userData, options: [])
let jsonData = try JSONSerialization.data(withJSONObject: data, options: [])
let presenceData = try JSONDecoder().decode(PresenceData.self, from: jsonData)
return presenceData
} catch {
Expand All @@ -221,11 +223,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
throw error
}
let presenceMembers = try members.map { member in
guard let data = member.data as? [String: Any] else {
let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without data")
logger.log(message: error.message, level: .error)
throw error
}
let userCustomData = try decodePresenceData(from: member.data)

guard let clientID = member.clientId else {
let error = ARTErrorInfo.create(withCode: 50000, status: 500, message: "Received incoming message without clientId")
Expand All @@ -239,8 +237,6 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
throw error
}

let userCustomData = decodePresenceData(from: data)

// Seems like we want to just forward on `extras` from the cocoa SDK but that is an `ARTJsonCompatible` type which is not `Sendable`... currently just converting this to a `Sendable` type (`String`) until we know what to do with this.
let extras = member.extras?.toJSONString()

Expand Down Expand Up @@ -271,7 +267,7 @@ internal final class DefaultPresence: Presence, EmitsDiscontinuities {
throw error
}

let userCustomDataDecoded = decodePresenceData(from: message.data)
let userCustomDataDecoded = try decodePresenceData(from: message.data)

let presenceEvent = PresenceEvent(
action: event,
Expand Down

0 comments on commit f11e0a1

Please sign in to comment.