-
Notifications
You must be signed in to change notification settings - Fork 24
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
Implement Persistent Preferences #175
Conversation
// This is a convenience for invoking the underlying `client.publish(prepared.envelopes)` | ||
// If a caller has a `Client` handy, they may opt to do that directly instead. | ||
@discardableResult public func send(prepared: PreparedMessage) async throws -> String { | ||
switch self { | ||
case let .v1(conversationV1): | ||
return try await conversationV1.send(prepared: prepared) | ||
case let .v2(conversationV2): | ||
return try await conversationV2.send(prepared: prepared) | ||
} | ||
} | ||
// This is a convenience for invoking the underlying `client.publish(prepared.envelopes)` | ||
// If a caller has a `Client` handy, they may opt to do that directly instead. | ||
@discardableResult public func send(prepared: PreparedMessage) async throws -> String { | ||
switch self { | ||
case let .v1(conversationV1): | ||
return try await conversationV1.send(prepared: prepared) | ||
case let .v2(conversationV2): | ||
return try await conversationV2.send(prepared: prepared) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard formatting changes here.
public func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] { | ||
public func messages(limit: Int? = nil, before: Date? = nil, after: Date? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] { | ||
switch self { | ||
case let .v1(conversationV1): | ||
return try await conversationV1.messages(limit: limit, before: before, after: after, direction: direction) | ||
return try await conversationV1.messages(limit: limit, before: before, after: after, direction: direction) | ||
case let .v2(conversationV2): | ||
return try await conversationV2.messages(limit: limit, before: before, after: after, direction: direction) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard formatting changes here.
func testCanSendPreparedMessagesWithoutAConversation() async throws { | ||
let conversation = try await aliceClient.conversations.newConversation(with: bob.address) | ||
let preparedMessage = try await conversation.prepareMessage(content: "hi") | ||
let messageID = preparedMessage.messageID | ||
func testCanSendPreparedMessagesWithoutAConversation() async throws { | ||
let conversation = try await aliceClient.conversations.newConversation(with: bob.address) | ||
let preparedMessage = try await conversation.prepareMessage(content: "hi") | ||
let messageID = preparedMessage.messageID | ||
|
||
// This does not need the `conversation` to `.publish` the message. | ||
// This simulates a background task publishes all pending messages upon connection. | ||
try await aliceClient.publish(envelopes: preparedMessage.envelopes) | ||
// This does not need the `conversation` to `.publish` the message. | ||
// This simulates a background task publishes all pending messages upon connection. | ||
try await aliceClient.publish(envelopes: preparedMessage.envelopes) | ||
|
||
let messages = try await conversation.messages() | ||
let message = messages[0] | ||
let messages = try await conversation.messages() | ||
let message = messages[0] | ||
|
||
XCTAssertEqual("hi", message.body) | ||
XCTAssertEqual(message.id, messageID) | ||
} | ||
XCTAssertEqual("hi", message.body) | ||
XCTAssertEqual(message.id, messageID) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard formatting changes here.
try await conversation.send(prepared: preparedMessage) | ||
try await conversation.send(prepared: preparedMessage) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard formatting changes here.
do { | ||
let content = "hey alice \(i)" | ||
let sentAt = Date().addingTimeInterval(-1000) | ||
try await bobConversation.send(content: content, sentAt: sentAt) | ||
} catch { | ||
print("Error sending message:", error) | ||
} | ||
} | ||
|
||
let messages = try await aliceClient.conversations.listBatchMessages( | ||
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 testProperlyDiscardBadBatchMessages() async throws { | ||
|
||
guard case let .v2(bobConversation) = try await aliceClient.conversations | ||
.newConversation(with: bob.address) else { | ||
XCTFail("did not get a v2 conversation for bob") | ||
return | ||
} | ||
|
||
try await bobConversation.send(content: "Hello") | ||
|
||
// Now we send some garbage and expect it to be properly ignored. | ||
try await bobClient.apiClient.publish(envelopes: [ | ||
Envelope( | ||
topic: bobConversation.topic, | ||
timestamp: Date(), | ||
message: Data([1, 2, 3]) // garbage, malformed message | ||
) | ||
]) | ||
|
||
try await bobConversation.send(content: "Goodbye") | ||
|
||
let messages = try await aliceClient.conversations.listBatchMessages( | ||
topics: [bobConversation.topic : nil] | ||
) | ||
XCTAssertEqual(2, messages.count) | ||
XCTAssertEqual("Goodbye", try messages[0].content()) | ||
XCTAssertEqual("Hello", try messages[1].content()) | ||
} | ||
|
||
func testCanRetrieveBatchMessages() async throws { | ||
guard case let .v2(bobConversation) = try await aliceClient.conversations.newConversation(with: bob.address, context: InvitationV1.Context(conversationID: "hi")) else { | ||
XCTFail("did not get a v2 conversation for bob") | ||
return | ||
} | ||
|
||
for i in 0 ..< 3 { | ||
do { | ||
let content = "hey alice \(i)" | ||
let sentAt = Date().addingTimeInterval(-1000) | ||
try await bobConversation.send(content: content, sentAt: sentAt) | ||
} catch { | ||
print("Error sending message:", error) | ||
} | ||
} | ||
|
||
let messages = try await aliceClient.conversations.listBatchMessages( | ||
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 testProperlyDiscardBadBatchMessages() async throws { | ||
guard case let .v2(bobConversation) = try await aliceClient.conversations | ||
.newConversation(with: bob.address) | ||
else { | ||
XCTFail("did not get a v2 conversation for bob") | ||
return | ||
} | ||
|
||
try await bobConversation.send(content: "Hello") | ||
|
||
// Now we send some garbage and expect it to be properly ignored. | ||
try await bobClient.apiClient.publish(envelopes: [ | ||
Envelope( | ||
topic: bobConversation.topic, | ||
timestamp: Date(), | ||
message: Data([1, 2, 3]) // garbage, malformed message | ||
), | ||
]) | ||
|
||
try await bobConversation.send(content: "Goodbye") | ||
|
||
let messages = try await aliceClient.conversations.listBatchMessages( | ||
topics: [bobConversation.topic: nil] | ||
) | ||
XCTAssertEqual(2, messages.count) | ||
XCTAssertEqual("Goodbye", try messages[0].content()) | ||
XCTAssertEqual("Hello", try messages[1].content()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disregard formatting changes here.
Implements the ideas as described in https://github.com/orgs/xmtp/discussions/49.