Skip to content

Commit

Permalink
clean up the consent code and add streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 23, 2024
1 parent 4e4a7f9 commit e60533b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
4 changes: 0 additions & 4 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,6 @@ public final class Client {
try await ffiClient.sendSyncRequest(kind: .messages)
}

public func syncConsent() async throws {
try await ffiClient.sendSyncRequest(kind: .consent)
}

public func inboxState(refreshFromNetwork: Bool) async throws -> InboxState
{
return InboxState(
Expand Down
16 changes: 16 additions & 0 deletions Sources/XMTPiOS/Extensions/Ffi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ extension FfiConsentState {
}
}

extension FfiConsentEntityType {
var fromFFI: EntryType {
switch self {
case .inboxId: return EntryType.inbox_id
case .address: return EntryType.address
case .conversationId: return EntryType.conversation_id
}
}
}

extension EntryType {
var toFFI: FfiConsentEntityType {
switch self {
Expand All @@ -62,3 +72,9 @@ extension ConsentListEntry {
)
}
}

extension FfiConsent {
var fromFfi: ConsentListEntry {
ConsentListEntry(value: self.entity, entryType: self.entityType.fromFFI, consentType: self.state.fromFFI)
}
}
93 changes: 62 additions & 31 deletions Sources/XMTPiOS/PrivatePreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public enum EntryType: String, Codable {
}

public struct ConsentListEntry: Codable, Hashable {
public init(value: String, entryType: EntryType, consentType: ConsentState) {
public init(value: String, entryType: EntryType, consentType: ConsentState)
{
self.value = value
self.entryType = entryType
self.consentType = consentType
}

static func address(_ address: String, type: ConsentState = .unknown)
-> ConsentListEntry
{
Expand All @@ -25,7 +26,8 @@ public struct ConsentListEntry: Codable, Hashable {
conversationId: String, type: ConsentState = ConsentState.unknown
) -> ConsentListEntry {
ConsentListEntry(
value: conversationId, entryType: .conversation_id, consentType: type)
value: conversationId, entryType: .conversation_id,
consentType: type)
}

static func inboxId(_ inboxId: String, type: ConsentState = .unknown)
Expand All @@ -44,25 +46,8 @@ public struct ConsentListEntry: Codable, Hashable {
}
}

public enum ContactError: Error {
case invalidIdentifier
}

public actor EntriesManager {
public var map: [String: ConsentListEntry] = [:]

func set(_ key: String, _ object: ConsentListEntry) {
map[key] = object
}

func get(_ key: String) -> ConsentListEntry? {
map[key]
}
}

public class ConsentList {
public let entriesManager = EntriesManager()
var lastFetched: Date?
/// Provides access to contact bundles.
public actor PrivatePreferences {
var client: Client
var ffiClient: FfiXmtpClient

Expand All @@ -82,7 +67,9 @@ public class ConsentList {
).fromFFI
}

public func conversationState(conversationId: String) async throws -> ConsentState {
public func conversationState(conversationId: String) async throws
-> ConsentState
{
return try await ffiClient.getConsentState(
entityType: .conversationId,
entity: conversationId
Expand All @@ -95,17 +82,61 @@ public class ConsentList {
entity: inboxId
).fromFFI
}

public func syncConsent() async throws {
try await ffiClient.sendSyncRequest(kind: .consent)
}

public func streamConsent()
-> AsyncThrowingStream<ConsentListEntry, Error>
{
AsyncThrowingStream { continuation in
let ffiStreamActor = FfiStreamActor()

let consentCallback = ConsentCallback(client: self.client) {
consent in
guard !Task.isCancelled else {
continuation.finish()
Task {
await ffiStreamActor.endStream()
}
return
}
continuation.yield(consent)
}

let task = Task {
let stream = await ffiClient.conversations().streamConsent(
callback: consentCallback)
await ffiStreamActor.setFfiStream(stream)
}

continuation.onTermination = { _ in
task.cancel()
Task {
await ffiStreamActor.endStream()
}
}
}
}
}

/// Provides access to contact bundles.
public actor PrivatePreferences {
var client: Client
var ffiClient: FfiXmtpClient
public var consentList: ConsentList
final class ConsentCallback: FfiConsentCallback {
let client: Client
let callback: (ConsentListEntry) -> Void

init(client: Client, ffiClient: FfiXmtpClient) {
init(client: Client, _ callback: @escaping (ConsentListEntry) -> Void) {
self.client = client
self.ffiClient = ffiClient
consentList = ConsentList(client: client, ffiClient: ffiClient)
self.callback = callback
}

func onConsentUpdate(consent: [LibXMTP.FfiConsent]) {
for record in consent {
callback(record.fromFfi)
}
}

func onError(error: LibXMTP.FfiSubscribeError) {
print("Error ConsentCallback \(error)")
}
}

0 comments on commit e60533b

Please sign in to comment.