-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add reply content type * add the public init * make content type id codable * trying to make it codecable * Get reply test passing * format * format * remove codable of content type id --------- Co-authored-by: Pat Nakajima <[email protected]>
- Loading branch information
1 parent
996d707
commit 770dd3b
Showing
6 changed files
with
124 additions
and
1 deletion.
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
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,62 @@ | ||
// | ||
// ReplyCodec.swift | ||
// | ||
// | ||
// Created by Naomi Plasterer on 7/26/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public let ContentTypeReply = ContentTypeID(authorityID: "xmtp.org", typeID: "reply", versionMajor: 1, versionMinor: 0) | ||
|
||
public struct Reply { | ||
public var reference: String | ||
public var content: Any | ||
public var contentType: ContentTypeID | ||
} | ||
|
||
public struct ReplyCodec: ContentCodec { | ||
public var contentType = ContentTypeReply | ||
|
||
public init() {} | ||
|
||
public func encode(content reply: Reply) throws -> EncodedContent { | ||
var encodedContent = EncodedContent() | ||
let replyCodec = Client.codecRegistry.find(for: reply.contentType) | ||
|
||
encodedContent.type = contentType | ||
encodedContent.parameters["contentType"] = reply.contentType.description | ||
encodedContent.parameters["reference"] = reply.reference | ||
encodedContent.content = try encodeReply(codec: replyCodec, content: reply.content).serializedData() | ||
|
||
return encodedContent | ||
} | ||
|
||
public func decode(content: EncodedContent) throws -> Reply { | ||
guard let contentTypeString = content.parameters["contentType"] else { | ||
throw CodecError.codecNotFound | ||
} | ||
|
||
guard let reference = content.parameters["reference"] else { | ||
throw CodecError.invalidContent | ||
} | ||
|
||
let replyEncodedContent = try EncodedContent(serializedData: content.content) | ||
let replyCodec = Client.codecRegistry.find(for: contentTypeString) | ||
let replyContent = try replyCodec.decode(content: replyEncodedContent) | ||
|
||
return Reply( | ||
reference: reference, | ||
content: replyContent, | ||
contentType: replyCodec.contentType | ||
) | ||
} | ||
|
||
func encodeReply<Codec: ContentCodec>(codec: Codec, content: Any) throws -> EncodedContent { | ||
if let content = content as? Codec.T { | ||
return try codec.encode(content: content) | ||
} else { | ||
throw CodecError.invalidContent | ||
} | ||
} | ||
} |
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,43 @@ | ||
// | ||
// ReplyTests.swift | ||
// | ||
// | ||
// Created by Naomi Plasterer on 7/26/23. | ||
// | ||
import Foundation | ||
|
||
import XCTest | ||
@testable import XMTP | ||
|
||
@available(iOS 15, *) | ||
class ReplyTests: XCTestCase { | ||
func testCanUseReplyCodec() async throws { | ||
Client.register(codec: ReplyCodec()) | ||
|
||
let fixtures = await fixtures() | ||
let conversation = try await fixtures.aliceClient.conversations.newConversation(with: fixtures.bobClient.address) | ||
|
||
try await conversation.send(text: "hey alice 2 bob") | ||
|
||
let messageToReply = try await conversation.messages()[0] | ||
|
||
let reply = Reply( | ||
reference: messageToReply.id, | ||
content: "Hello", | ||
contentType: ContentTypeText | ||
) | ||
|
||
try await conversation.send( | ||
content: reply, | ||
options: .init(contentType: ContentTypeReply) | ||
) | ||
|
||
let updatedMessages = try await conversation.messages() | ||
|
||
let message = try await conversation.messages()[0] | ||
let content: Reply = try message.content() | ||
XCTAssertEqual("Hello", content.content as? String) | ||
XCTAssertEqual(messageToReply.id, content.reference) | ||
XCTAssertEqual(ContentTypeText, content.contentType) | ||
} | ||
} |
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