Skip to content

Commit

Permalink
Rename AllowState to ConsentState (#180)
Browse files Browse the repository at this point in the history
* rename it to consent state

* rename back to ed
  • Loading branch information
nplasterer authored Oct 26, 2023
1 parent 0694a89 commit 74ea77a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
56 changes: 28 additions & 28 deletions Sources/XMTP/Contacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import XMTPRust

public typealias PrivatePreferencesAction = Xmtp_MessageContents_PrivatePreferencesAction

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

struct AllowListEntry: Codable, Hashable {
struct ConsentListEntry: Codable, Hashable {
enum EntryType: String, Codable {
case address
}

static func address(_ address: String, type: AllowState = .unknown) -> AllowListEntry {
AllowListEntry(value: address, entryType: .address, permissionType: type)
static func address(_ address: String, type: ConsentState = .unknown) -> ConsentListEntry {
ConsentListEntry(value: address, entryType: .address, consentType: type)
}

var value: String
var entryType: EntryType
var permissionType: AllowState
var consentType: ConsentState

var key: String {
"\(entryType)-\(value)"
Expand All @@ -37,8 +37,8 @@ public enum ContactError: Error {
case invalidIdentifier
}

class AllowList {
var entries: [String: AllowState] = [:]
class ConsentList {
var entries: [String: ConsentState] = [:]
var publicKey: Data
var privateKey: Data
var identifier: String?
Expand All @@ -54,14 +54,14 @@ class AllowList {
// swiftlint:enable no_optional_try
}

func load() async throws -> AllowList {
func load() async throws -> ConsentList {
guard let identifier = identifier else {
throw ContactError.invalidIdentifier
}

let envelopes = try await client.query(topic: .preferenceList(identifier))

let allowList = AllowList(client: client)
let consentList = ConsentList(client: client)

var preferences: [PrivatePreferencesAction] = []

Expand All @@ -79,23 +79,23 @@ class AllowList {

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

return allowList
return consentList
}

func publish(entry: AllowListEntry) async throws {
func publish(entry: ConsentListEntry) async throws {
guard let identifier = identifier else {
throw ContactError.invalidIdentifier
}

var payload = PrivatePreferencesAction()
switch entry.permissionType {
switch entry.consentType {
case .allowed:
payload.allow.walletAddresses = [entry.value]
case .blocked:
Expand All @@ -119,20 +119,20 @@ class AllowList {
try await client.publish(envelopes: [envelope])
}

func allow(address: String) -> AllowListEntry {
entries[AllowListEntry.address(address).key] = .allowed
func allow(address: String) -> ConsentListEntry {
entries[ConsentListEntry.address(address).key] = .allowed

return .address(address, type: .allowed)
}

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

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

func state(address: String) -> AllowState {
let state = entries[AllowListEntry.address(address).key]
func state(address: String) -> ConsentState {
let state = entries[ConsentListEntry.address(address).key]

return state ?? .unknown
}
Expand All @@ -148,34 +148,34 @@ public actor Contacts {
// Whether or not we have sent invite/intro to this contact
var hasIntroduced: [String: Bool] = [:]

var allowList: AllowList
var consentList: ConsentList

init(client: Client) {
self.client = client
self.allowList = AllowList(client: client)
self.consentList = ConsentList(client: client)
}

public func refreshAllowList() async throws {
self.allowList = try await AllowList(client: client).load()
public func refreshConsentList() async throws {
self.consentList = try await ConsentList(client: client).load()
}

public func isAllowed(_ address: String) -> Bool {
return allowList.state(address: address) == .allowed
return consentList.state(address: address) == .allowed
}

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTP/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum Conversation: Sendable {
case v1, v2
}

public func allowState() async -> AllowState {
public func consentState() async -> ConsentState {
let client: Client

switch self {
Expand All @@ -39,7 +39,7 @@ public enum Conversation: Sendable {
client = conversationV2.client
}

return await client.contacts.allowList.state(address: peerAddress)
return await client.contacts.consentList.state(address: peerAddress)
}

public var version: Version {
Expand Down
12 changes: 6 additions & 6 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -606,31 +606,31 @@ class ConversationTests: XCTestCase {
XCTAssertEqual(bob.address, messages[0].senderAddress)
}

func testCanHaveAllowState() async throws {
func testCanHaveConsentState() async throws {
let bobConversation = try await bobClient.conversations.newConversation(with: alice.address, context: InvitationV1.Context(conversationID: "hi"))
let isAllowed = (await bobConversation.allowState()) == .allowed
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.allowState()) == .unknown
let isUnknown = (await aliceConversation.consentState()) == .unknown

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

try await aliceClient.contacts.allow(addresses: [bob.address])

let isBobAllowed = (await aliceConversation.allowState()) == .allowed
let isBobAllowed = (await aliceConversation.consentState()) == .allowed
XCTAssertTrue(isBobAllowed)

let aliceClient2 = try await Client.create(account: alice, apiClient: fakeApiClient)
let aliceConversation2 = (try await aliceClient2.conversations.list())[0]

try await aliceClient2.contacts.refreshAllowList()
try await aliceClient2.contacts.refreshConsentList()

// Allow state should sync across clients
let isBobAllowed2 = (await aliceConversation2.allowState()) == .allowed
let isBobAllowed2 = (await aliceConversation2.consentState()) == .allowed

XCTAssertTrue(isBobAllowed2)
}
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.6.2-alpha0"
spec.version = "0.6.3-alpha0"
spec.summary = "XMTP SDK Cocoapod"

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

0 comments on commit 74ea77a

Please sign in to comment.