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

Implement Persistent Preferences #175

Merged
merged 11 commits into from
Oct 26, 2023
Merged

Implement Persistent Preferences #175

merged 11 commits into from
Oct 26, 2023

Conversation

nakajima
Copy link
Contributor

Implements the ideas as described in https://github.com/orgs/xmtp/discussions/49.

Comment on lines -126 to +148
// 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)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard formatting changes here.

Comment on lines -202 to 221
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)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard formatting changes here.

Comment on lines -51 to +65
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)
}
Copy link
Contributor Author

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard formatting changes here.

Comment on lines -426 to +478
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())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disregard formatting changes here.

@nakajima nakajima marked this pull request as ready for review October 13, 2023 19:27
@nakajima nakajima requested a review from a team as a code owner October 13, 2023 19:27
@nplasterer nplasterer changed the title Implement persistent Allow State Implement Persistent Preferences Oct 25, 2023
@nplasterer nplasterer merged commit 7e69703 into main Oct 26, 2023
1 check passed
@nplasterer nplasterer deleted the pat-v2-consent branch October 26, 2023 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants