-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix sending and receiving of reaction metadata and headers
Similar issue to the one which was fixed in 7fcab5c. Haven’t added tests for the handling of the incoming message, since there were existing tests to update; will leave this for whoever writes these tests in #88. The approach that I’ve taken here of using a DTO is consistent with the approach that we use for presence data. I should have done this in 7fcab5c too; will do it separately. TODO add tests for DTO Resolves #198.
- Loading branch information
1 parent
f6ec891
commit ad6b02b
Showing
6 changed files
with
114 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// CHA-ER3a | ||
internal struct RoomReactionDTO { | ||
internal var data: Data | ||
internal var extras: Extras | ||
|
||
internal struct Data { | ||
internal var type: String | ||
internal var metadata: ReactionMetadata? | ||
} | ||
|
||
internal struct Extras { | ||
internal var headers: ReactionHeaders? | ||
} | ||
} | ||
|
||
internal extension RoomReactionDTO { | ||
init(type: String, metadata: ReactionMetadata?, headers: ReactionHeaders?) { | ||
data = .init(type: type, metadata: metadata) | ||
extras = .init(headers: headers) | ||
} | ||
|
||
var type: String { | ||
data.type | ||
} | ||
|
||
var metadata: ReactionMetadata? { | ||
data.metadata | ||
} | ||
|
||
var headers: ReactionHeaders? { | ||
extras.headers | ||
} | ||
} | ||
|
||
// MARK: - JSONCodable | ||
|
||
extension RoomReactionDTO.Data: JSONObjectCodable { | ||
internal enum JSONKey: String { | ||
case type | ||
case metadata | ||
} | ||
|
||
internal init(jsonObject: [String: JSONValue]) throws { | ||
type = try jsonObject.stringValueForKey(JSONKey.type.rawValue) | ||
metadata = try jsonObject.optionalObjectValueForKey(JSONKey.metadata.rawValue)?.mapValues { try .init(jsonValue: $0) } | ||
} | ||
|
||
internal var toJSONObject: [String: JSONValue] { | ||
var result: [String: JSONValue] = [JSONKey.type.rawValue: .string(type)] | ||
|
||
if let metadata { | ||
result[JSONKey.metadata.rawValue] = .object(metadata.mapValues(\.toJSONValue)) | ||
} | ||
|
||
return result | ||
} | ||
} | ||
|
||
extension RoomReactionDTO.Extras: JSONObjectCodable { | ||
internal enum JSONKey: String { | ||
case headers | ||
} | ||
|
||
internal init(jsonObject: [String: JSONValue]) throws { | ||
headers = try jsonObject.optionalObjectValueForKey(JSONKey.headers.rawValue)?.mapValues { try .init(jsonValue: $0) } | ||
} | ||
|
||
internal var toJSONObject: [String: JSONValue] { | ||
var result: [String: JSONValue] = [:] | ||
|
||
if let headers { | ||
result[JSONKey.headers.rawValue] = .object(headers.mapValues(\.toJSONValue)) | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters