Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: normalize reaction encoding, support legacy form #151

Merged
merged 4 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Sources/XMTP/Codecs/ReactionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@ public struct ReactionCodec: ContentCodec {
}

public func decode(content: EncodedContent) throws -> Reaction {
let reaction = try JSONDecoder().decode(Reaction.self, from: content.content)
return reaction
// First try to decode it in the canonical form.
// swiftlint:disable no_optional_try
if let reaction = try? JSONDecoder().decode(Reaction.self, from: content.content) {
return reaction
}
// swiftlint:disable no_optional_try
// If that fails, try to decode it in the legacy form.
// swiftlint:disable force_unwrapping
return Reaction(
reference: content.parameters["reference"] ?? "",
action: ReactionAction(rawValue: content.parameters["action"] ?? "")!,
content: String(data: content.content, encoding: .utf8) ?? "",
schema: ReactionSchema(rawValue: content.parameters["schema"] ?? "")!
)
//swiftlint:disable force_unwrapping
}
}
42 changes: 42 additions & 0 deletions Tests/XMTPTests/ReactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@ import XCTest

@available(iOS 15, *)
class ReactionTests: XCTestCase {

func testCanDecodeLegacyForm() async throws {
let codec = ReactionCodec()

// This is how clients send reactions now.
let canonicalEncoded = EncodedContent.with {
$0.type = ContentTypeReaction
$0.content = Data("""
{
"action": "added",
"content": "smile",
"reference": "abc123",
"schema": "shortcode"
}
""".utf8)
}

// Previously, some clients sent reactions like this.
// So we test here to make sure we can still decode them.
let legacyEncoded = EncodedContent.with {
$0.type = ContentTypeReaction
$0.parameters = [
"action": "added",
"reference": "abc123",
"schema": "shortcode",
]
$0.content = Data("smile".utf8)
}

let canonical = try codec.decode(content: canonicalEncoded)
let legacy = try codec.decode(content: legacyEncoded)

XCTAssertEqual(ReactionAction.added, canonical.action)
XCTAssertEqual(ReactionAction.added, legacy.action)
XCTAssertEqual("smile", canonical.content)
XCTAssertEqual("smile", legacy.content)
XCTAssertEqual("abc123", canonical.reference)
XCTAssertEqual("abc123", legacy.reference)
XCTAssertEqual(ReactionSchema.shortcode, canonical.schema)
XCTAssertEqual(ReactionSchema.shortcode, legacy.schema)
}

func testCanUseReactionCodec() async throws {
Client.register(codec: ReactionCodec())

Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.5.0-alpha0"
spec.version = "0.5.1-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down