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 1 commit
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
13 changes: 11 additions & 2 deletions Sources/XMTP/Codecs/ReactionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ 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.
if let reaction = try? JSONDecoder().decode(Reaction.self, from: content.content) {
return reaction
}
// If that fails, try to decode it in the legacy form.
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"] ?? "")!
)
}
}
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
Loading