Skip to content

Commit

Permalink
Merge branch 'main' into bwcDvorak-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer authored Nov 1, 2023
2 parents d75f30a + 628fdf5 commit d0d0426
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
24 changes: 12 additions & 12 deletions Sources/XMTP/Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XMTPRust
public typealias PrivatePreferencesAction = Xmtp_MessageContents_PrivatePreferencesAction

public enum ConsentState: String, Codable {
case allowed, blocked, unknown
case allowed, denied, unknown
}

struct ConsentListEntry: Codable, Hashable {
Expand Down Expand Up @@ -59,7 +59,7 @@ class ConsentList {
throw ContactError.invalidIdentifier
}

let envelopes = try await client.query(topic: .preferenceList(identifier))
let envelopes = try await client.query(topic: .preferenceList(identifier), pagination: Pagination(direction: .ascending))

let consentList = ConsentList(client: client)

Expand All @@ -77,12 +77,12 @@ class ConsentList {
preferences.append(try PrivatePreferencesAction(serializedData: Data(payload)))
}

preferences.reversed().forEach { preference in
preferences.forEach { preference in
preference.allow.walletAddresses.forEach { address in
consentList.allow(address: address)
}
preference.block.walletAddresses.forEach { address in
consentList.block(address: address)
consentList.deny(address: address)
}
}

Expand All @@ -98,7 +98,7 @@ class ConsentList {
switch entry.consentType {
case .allowed:
payload.allow.walletAddresses = [entry.value]
case .blocked:
case .denied:
payload.block.walletAddresses = [entry.value]
case .unknown:
payload.messageType = nil
Expand All @@ -125,10 +125,10 @@ class ConsentList {
return .address(address, type: .allowed)
}

func block(address: String) -> ConsentListEntry {
entries[ConsentListEntry.address(address).key] = .blocked
func deny(address: String) -> ConsentListEntry {
entries[ConsentListEntry.address(address).key] = .denied

return .address(address, type: .blocked)
return .address(address, type: .denied)
}

func state(address: String) -> ConsentState {
Expand Down Expand Up @@ -163,8 +163,8 @@ public actor Contacts {
return consentList.state(address: address) == .allowed
}

public func isBlocked(_ address: String) -> Bool {
return consentList.state(address: address) == .blocked
public func isDenied(_ address: String) -> Bool {
return consentList.state(address: address) == .denied
}

public func allow(addresses: [String]) async throws {
Expand All @@ -173,9 +173,9 @@ public actor Contacts {
}
}

public func block(addresses: [String]) async throws {
public func deny(addresses: [String]) async throws {
for address in addresses {
try await ConsentList(client: client).publish(entry: consentList.block(address: address))
try await ConsentList(client: client).publish(entry: consentList.deny(address: address))
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/XMTP/ConversationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public struct ConversationV1 {

@discardableResult func send(prepared: PreparedMessage) async throws -> String {
try await client.publish(envelopes: prepared.envelopes)
if((await client.contacts.consentList.state(address: peerAddress)) == .unknown) {
try await client.contacts.allow(addresses: [peerAddress])
}
return prepared.messageID
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/XMTP/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ public struct ConversationV2 {

@discardableResult func send(prepared: PreparedMessage) async throws -> String {
try await client.publish(envelopes: prepared.envelopes)
if((await client.contacts.consentList.state(address: peerAddress)) == .unknown) {
try await client.contacts.allow(addresses: [peerAddress])
}
return prepared.messageID
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/XMTPTests/ContactsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class ContactsTests: XCTestCase {

XCTAssertFalse(result)

try await contacts.block(addresses: [fixtures.alice.address])
try await contacts.deny(addresses: [fixtures.alice.address])

result = await contacts.isBlocked(fixtures.alice.address)
result = await contacts.isDenied(fixtures.alice.address)
XCTAssertTrue(result)
}
}
28 changes: 25 additions & 3 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,12 @@ class ConversationTests: XCTestCase {
// Conversations you start should start as allowed
XCTAssertTrue(isAllowed)

try await bobClient.contacts.block(addresses: [alice.address])
try await bobClient.contacts.deny(addresses: [alice.address])
try await bobClient.contacts.refreshConsentList()

let isBlocked = (await bobConversation.consentState()) == .blocked
let isDenied = (await bobConversation.consentState()) == .denied

XCTAssertTrue(isBlocked)
XCTAssertTrue(isDenied)

let aliceConversation = (try await aliceClient.conversations.list())[0]
let isUnknown = (await aliceConversation.consentState()) == .unknown
Expand All @@ -641,4 +641,26 @@ class ConversationTests: XCTestCase {

XCTAssertTrue(isBobAllowed2)
}

func testCanHaveImplicitConsentOnMessageSend() async throws {
let bobConversation = try await bobClient.conversations.newConversation(with: alice.address, context: InvitationV1.Context(conversationID: "hi"))
let isAllowed = (await bobConversation.consentState()) == .allowed

// Conversations you start should start as allowed
XCTAssertTrue(isAllowed)


let aliceConversation = (try await aliceClient.conversations.list())[0]
let isUnknown = (await aliceConversation.consentState()) == .unknown

// Conversations started with you should start as unknown
XCTAssertTrue(isUnknown)

try await aliceConversation.send(content: "hey bob")
try await aliceClient.contacts.refreshConsentList()
let isNowAllowed = (await aliceConversation.consentState()) == .allowed

// Conversations you send a message to get marked as allowed
XCTAssertTrue(isNowAllowed)
}
}
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.6.5-alpha0"
spec.version = "0.6.6-alpha0"
spec.summary = "XMTP SDK Cocoapod"

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

0 comments on commit d0d0426

Please sign in to comment.