Skip to content

Commit

Permalink
remove client from all serializable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 14, 2025
1 parent 21d9032 commit a921671
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 125 deletions.
21 changes: 6 additions & 15 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public final class Client {
public lazy var preferences: PrivatePreferences = .init(
client: self, ffiClient: ffiClient)

var codecRegistry = CodecRegistry()
static var codecRegistry = CodecRegistry()

public func register(codec: any ContentCodec) {
public static func register(codec: any ContentCodec) {
codecRegistry.register(codec: codec)
}

Expand Down Expand Up @@ -142,7 +142,7 @@ public final class Client {

// Register codecs
for codec in options.codecs {
client.register(codec: codec)
register(codec: codec)
}

return client
Expand Down Expand Up @@ -321,13 +321,9 @@ public final class Client {
let address = "0x0000000000000000000000000000000000000000"
let inboxId = try await getOrCreateInboxId(api: api, address: address)

let directoryURL: URL = URL.documentsDirectory
let alias = "xmtp-\(api.env.rawValue)-\(inboxId).db3"
let dbURL = directoryURL.appendingPathComponent(alias).path

let ffiClient = try await LibXMTP.createClient(
api: connectToApiBackend(api: api),
db: dbURL,
db: nil,
encryptionKey: nil,
inboxId: inboxId,
accountAddress: address,
Expand All @@ -339,10 +335,6 @@ public final class Client {
let result = try await ffiClient.canMessage(
accountAddresses: accountAddresses)

try ffiClient.releaseDbConnection()
let fm = FileManager.default
try fm.removeItem(atPath: dbURL)

return result
}

Expand Down Expand Up @@ -470,7 +462,7 @@ public final class Client {
do {
return Group(
ffiGroup: try ffiClient.conversation(
conversationId: groupId.hexToData), client: self)
conversationId: groupId.hexToData), clientInboxId: self.inboxID)
} catch {
return nil
}
Expand Down Expand Up @@ -515,7 +507,7 @@ public final class Client {
do {
let conversation = try ffiClient.dmConversation(
targetInboxId: inboxId)
return Dm(ffiConversation: conversation, client: self)
return Dm(ffiConversation: conversation, clientInboxId: self.inboxID)
} catch {
return nil
}
Expand All @@ -532,7 +524,6 @@ public final class Client {
public func findMessage(messageId: String) throws -> Message? {
do {
return Message.create(
client: self,
ffiMessage: try ffiClient.message(
messageId: messageId.hexToData))
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/AttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct AttachmentCodec: ContentCodec {

public var contentType = ContentTypeAttachment

public func encode(content: Attachment, client _: Client) throws -> EncodedContent {
public func encode(content: Attachment) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeAttachment
Expand All @@ -44,7 +44,7 @@ public struct AttachmentCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> Attachment {
public func decode(content: EncodedContent) throws -> Attachment {
guard let mimeType = content.parameters["mimeType"],
let filename = content.parameters["filename"]
else {
Expand Down
10 changes: 5 additions & 5 deletions Sources/XMTPiOS/Codecs/ContentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ enum CodecError: String, Error {
public typealias EncodedContent = Xmtp_MessageContents_EncodedContent

extension EncodedContent {
public func decoded<T>(with client: Client) throws -> T {
let codec = client.codecRegistry.find(for: type)
public func decoded<T>() throws -> T {
let codec = Client.codecRegistry.find(for: type)

var encodedContent = self

if hasCompression {
encodedContent = try decompressContent()
}

if let content = try codec.decode(content: encodedContent, client: client) as? T {
if let content = try codec.decode(content: encodedContent) as? T {
return content
}

Expand Down Expand Up @@ -82,8 +82,8 @@ public protocol ContentCodec: Hashable, Equatable {
associatedtype T

var contentType: ContentTypeID { get }
func encode(content: T, client: Client) throws -> EncodedContent
func decode(content: EncodedContent, client: Client) throws -> T
func encode(content: T) throws -> EncodedContent
func decode(content: EncodedContent) throws -> T
func fallback(content: T) throws -> String?
func shouldPush(content: T) throws -> Bool
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/GroupUpdatedCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public struct GroupUpdatedCodec: ContentCodec {

public var contentType = ContentTypeGroupUpdated

public func encode(content: GroupUpdated, client _: Client) throws -> EncodedContent {
public func encode(content: GroupUpdated) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeGroupUpdated
Expand All @@ -28,7 +28,7 @@ public struct GroupUpdatedCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> GroupUpdated {
public func decode(content: EncodedContent) throws -> GroupUpdated {
return try GroupUpdated(serializedData: content.content)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/ReactionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public struct ReactionCodec: ContentCodec {

public init() {}

public func encode(content: Reaction, client _: Client) throws -> EncodedContent {
public func encode(content: Reaction) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeReaction
Expand All @@ -70,7 +70,7 @@ public struct ReactionCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> Reaction {
public func decode(content: EncodedContent) throws -> Reaction {
// First try to decode it in the canonical form.
// swiftlint:disable no_optional_try
if let reaction = try? JSONDecoder().decode(Reaction.self, from: content.content) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/ReadReceiptCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct ReadReceiptCodec: ContentCodec {

public var contentType = ContentTypeReadReceipt

public func encode(content: ReadReceipt, client _: Client) throws -> EncodedContent {
public func encode(content: ReadReceipt) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeReadReceipt
Expand All @@ -29,7 +29,7 @@ public struct ReadReceiptCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> ReadReceipt {
public func decode(content: EncodedContent) throws -> ReadReceipt {
return ReadReceipt()
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/XMTPiOS/Codecs/RemoteAttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public struct RemoteAttachment {
}
}

public static func encodeEncrypted<Codec: ContentCodec, T>(content: T, codec: Codec, with client: Client) throws -> EncryptedEncodedContent where Codec.T == T {
public static func encodeEncrypted<Codec: ContentCodec, T>(content: T, codec: Codec) throws -> EncryptedEncodedContent where Codec.T == T {
let secret = try Crypto.secureRandomBytes(count: 32)
let encodedContent = try codec.encode(content: content, client: client).serializedData()
let encodedContent = try codec.encode(content: content).serializedData()
let ciphertext = try Crypto.encrypt(secret, encodedContent)
let contentDigest = sha256(data: ciphertext.aes256GcmHkdfSha256.payload)

Expand Down Expand Up @@ -149,7 +149,7 @@ public struct RemoteAttachmentCodec: ContentCodec {

public var contentType = ContentTypeRemoteAttachment

public func encode(content: RemoteAttachment, client _: Client) throws -> EncodedContent {
public func encode(content: RemoteAttachment) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeRemoteAttachment
Expand All @@ -167,7 +167,7 @@ public struct RemoteAttachmentCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> RemoteAttachment {
public func decode(content: EncodedContent) throws -> RemoteAttachment {
guard let url = String(data: content.content, encoding: .utf8) else {
throw RemoteAttachmentError.invalidURL
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/XMTPiOS/Codecs/ReplyCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ public struct ReplyCodec: ContentCodec {

public init() {}

public func encode(content reply: Reply, client: Client) throws -> EncodedContent {
public func encode(content reply: Reply) throws -> EncodedContent {
var encodedContent = EncodedContent()
let replyCodec = client.codecRegistry.find(for: reply.contentType)
let replyCodec = Client.codecRegistry.find(for: reply.contentType)

encodedContent.type = contentType
// TODO: cut when we're certain no one is looking for "contentType" here.
encodedContent.parameters["contentType"] = reply.contentType.description
encodedContent.parameters["reference"] = reply.reference
encodedContent.content = try encodeReply(codec: replyCodec, content: reply.content, client: client).serializedData()
encodedContent.content = try encodeReply(codec: replyCodec, content: reply.content).serializedData()

return encodedContent
}

public func decode(content: EncodedContent, client: Client) throws -> Reply {
public func decode(content: EncodedContent) throws -> Reply {
guard let reference = content.parameters["reference"] else {
throw CodecError.invalidContent
}

let replyEncodedContent = try EncodedContent(serializedData: content.content)
let replyCodec = client.codecRegistry.find(for: replyEncodedContent.type)
let replyContent = try replyCodec.decode(content: replyEncodedContent, client: client)
let replyCodec = Client.codecRegistry.find(for: replyEncodedContent.type)
let replyContent = try replyCodec.decode(content: replyEncodedContent)

return Reply(
reference: reference,
Expand All @@ -55,9 +55,9 @@ public struct ReplyCodec: ContentCodec {
)
}

func encodeReply<Codec: ContentCodec>(codec: Codec, content: Any, client: Client) throws -> EncodedContent {
func encodeReply<Codec: ContentCodec>(codec: Codec, content: Any) throws -> EncodedContent {
if let content = content as? Codec.T {
return try codec.encode(content: content, client: client)
return try codec.encode(content: content)
} else {
throw CodecError.invalidContent
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Codecs/TextCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct TextCodec: ContentCodec {

public var contentType = ContentTypeText

public func encode(content: String, client _: Client) throws -> EncodedContent {
public func encode(content: String) throws -> EncodedContent {
var encodedContent = EncodedContent()

encodedContent.type = ContentTypeText
Expand All @@ -31,7 +31,7 @@ public struct TextCodec: ContentCodec {
return encodedContent
}

public func decode(content: EncodedContent, client _: Client) throws -> String {
public func decode(content: EncodedContent) throws -> String {
if let encoding = content.parameters["encoding"], encoding != "UTF-8" {
throw TextCodecError.invalidEncoding
}
Expand Down
13 changes: 0 additions & 13 deletions Sources/XMTPiOS/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,6 @@ public enum Conversation: Identifiable, Equatable, Hashable {
}
}

public var clientAddress: String {
return client.address
}

public var topic: String {
switch self {
case let .group(group):
Expand Down Expand Up @@ -217,13 +213,4 @@ public enum Conversation: Identifiable, Equatable, Hashable {
)
}
}

var client: Client {
switch self {
case let .group(group):
return group.client
case let .dm(dm):
return dm.client
}
}
}
5 changes: 2 additions & 3 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public actor Conversations {
AsyncThrowingStream { continuation in
let ffiStreamActor = FfiStreamActor()

let messageCallback = MessageCallback(client: self.client) {
let messageCallback = MessageCallback() {
message in
guard !Task.isCancelled else {
continuation.finish()
Expand All @@ -343,8 +343,7 @@ public actor Conversations {
}
return
}
if let message = Message.create(
client: self.client, ffiMessage: message)
if let message = Message.create(ffiMessage: message)
{
continuation.yield(message)
}
Expand Down
20 changes: 9 additions & 11 deletions Sources/XMTPiOS/Dm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import LibXMTP
public struct Dm: Identifiable, Equatable, Hashable {
var ffiConversation: FfiConversation
var ffiLastMessage: FfiMessage? = nil
var client: Client
var clientInboxId: String
let streamHolder = StreamHolder()

public var id: String {
Expand Down Expand Up @@ -32,7 +32,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
}

public func isCreator() async throws -> Bool {
return try await metadata().creatorInboxId() == client.inboxID
return try await metadata().creatorInboxId() == clientInboxId
}

public func creatorInboxId() async throws -> String {
Expand Down Expand Up @@ -74,7 +74,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
let message =
try await ffiConversation.processStreamedConversationMessage(
envelopeBytes: messageBytes)
return Message.create(client: client, ffiMessage: message)
return Message.create(ffiMessage: message)
}

public func send<T>(content: T, options: SendOptions? = nil) async throws
Expand All @@ -98,13 +98,13 @@ public struct Dm: Identifiable, Equatable, Hashable {
public func encodeContent<T>(content: T, options: SendOptions?) async throws
-> EncodedContent
{
let codec = client.codecRegistry.find(for: options?.contentType)
let codec = Client.codecRegistry.find(for: options?.contentType)

func encode<Codec: ContentCodec>(codec: Codec, content: Any) throws
-> EncodedContent
{
if let content = content as? Codec.T {
return try codec.encode(content: content, client: client)
return try codec.encode(content: content)
} else {
throw CodecError.invalidContent
}
Expand Down Expand Up @@ -171,15 +171,13 @@ public struct Dm: Identifiable, Equatable, Hashable {
AsyncThrowingStream { continuation in
let task = Task.detached {
self.streamHolder.stream = await self.ffiConversation.stream(
messageCallback: MessageCallback(client: self.client) {
messageCallback: MessageCallback {
message in
guard !Task.isCancelled else {
continuation.finish()
return
}
if let message = Message.create(
client: self.client, ffiMessage: message)
{
if let message = Message.create(ffiMessage: message) {
continuation.yield(message)
}
}
Expand All @@ -199,7 +197,7 @@ public struct Dm: Identifiable, Equatable, Hashable {

public func lastMessage() async throws -> Message? {
if let ffiMessage = ffiLastMessage {
return Message.create(client: self.client, ffiMessage: ffiMessage)
return Message.create(ffiMessage: ffiMessage)
} else {
return try await messages(limit: 1).first
}
Expand Down Expand Up @@ -262,7 +260,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
return try await ffiConversation.findMessages(opts: options).compactMap
{
ffiMessage in
return Message.create(client: self.client, ffiMessage: ffiMessage)
return Message.create(ffiMessage: ffiMessage)
}
}
}
Loading

0 comments on commit a921671

Please sign in to comment.