Skip to content

Commit

Permalink
feat: include topic in the DecodedMessage (#148)
Browse files Browse the repository at this point in the history
* feat: include topic in the DecodedMessage

* bump the pod spec

---------

Co-authored-by: Naomi Plasterer <naomi@xmtp.com>
  • Loading branch information
dmccartney and nplasterer authored Aug 23, 2023
1 parent 2d81b1a commit 38687a8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions Sources/XMTP/ConversationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public struct ConversationV1 {
let header = try message.v1.header

var decoded = DecodedMessage(
topic: envelope.contentTopic,
encodedContent: encodedMessage,
senderAddress: header.sender.walletAddress,
sent: message.v1.sentAt
Expand Down
9 changes: 6 additions & 3 deletions Sources/XMTP/DecodedMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Foundation

/// Decrypted messages from a conversation.
public struct DecodedMessage: Sendable {
public var topic: String

public var id: String = ""

public var encodedContent: EncodedContent
Expand All @@ -19,7 +21,8 @@ public struct DecodedMessage: Sendable {
/// When the message was sent
public var sent: Date

public init(encodedContent: EncodedContent, senderAddress: String, sent: Date) {
public init(topic: String, encodedContent: EncodedContent, senderAddress: String, sent: Date) {
self.topic = topic
self.encodedContent = encodedContent
self.senderAddress = senderAddress
self.sent = sent
Expand All @@ -43,10 +46,10 @@ public struct DecodedMessage: Sendable {
}

public extension DecodedMessage {
static func preview(body: String, senderAddress: String, sent: Date) -> DecodedMessage {
static func preview(topic: String, body: String, senderAddress: String, sent: Date) -> DecodedMessage {
// swiftlint:disable force_try
let encoded = try! TextCodec().encode(content: body)
// swiftlint:enable force_try
return DecodedMessage(encodedContent: encoded, senderAddress: senderAddress, sent: sent)
return DecodedMessage(topic: topic, encodedContent: encoded, senderAddress: senderAddress, sent: sent)
}
}
1 change: 1 addition & 0 deletions Sources/XMTP/Messages/MessageV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension MessageV2 {
let header = try MessageHeaderV2(serializedData: message.headerBytes)

return DecodedMessage(
topic: header.topic,
encodedContent: encodedMessage,
senderAddress: try signed.sender.walletAddress,
sent: Date(timeIntervalSince1970: Double(header.createdNs / 1_000_000) / 1000)
Expand Down
5 changes: 5 additions & 0 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class ConversationTests: XCTestCase {
let messages = try await aliceConversation.messages(limit: 1)
XCTAssertEqual(1, messages.count)
XCTAssertEqual("hey alice 3", messages[0].body)
XCTAssertEqual(aliceConversation.topic.description, messages[0].topic)

let messages2 = try await aliceConversation.messages(limit: 1, before: messages[0].sent)
XCTAssertEqual(1, messages2.count)
Expand Down Expand Up @@ -368,6 +369,7 @@ class ConversationTests: XCTestCase {
let messages = try await aliceConversation.messages(limit: 1)
XCTAssertEqual(1, messages.count)
XCTAssertEqual("hey alice 3", messages[0].body)
XCTAssertEqual(aliceConversation.topic, messages[0].topic)

let messages2 = try await aliceConversation.messages(limit: 1, before: messages[0].sent)
XCTAssertEqual(1, messages2.count)
Expand Down Expand Up @@ -427,6 +429,9 @@ class ConversationTests: XCTestCase {
topics: [bobConversation.topic : Pagination(limit:3)]
)
XCTAssertEqual(3, messages.count)
XCTAssertEqual(bobConversation.topic, messages[0].topic)
XCTAssertEqual(bobConversation.topic, messages[1].topic)
XCTAssertEqual(bobConversation.topic, messages[2].topic)
}

func testImportV1ConversationFromJS() async throws {
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.4.8-alpha0"
spec.version = "0.4.9-alpha0"
spec.summary = "XMTP SDK Cocoapod"

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

0 comments on commit 38687a8

Please sign in to comment.