Skip to content

Commit

Permalink
Message refactor and cleanup (#457)
Browse files Browse the repository at this point in the history
* remove decoded message

* clean up decoded messages

* get all the tests passing
  • Loading branch information
nplasterer authored Jan 8, 2025
1 parent f0acecc commit 21d9032
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public final class Client {

public func findMessage(messageId: String) throws -> Message? {
do {
return Message(
return Message.create(
client: self,
ffiMessage: try ffiClient.message(
messageId: messageId.hexToData))
Expand Down
8 changes: 4 additions & 4 deletions Sources/XMTPiOS/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum Conversation: Identifiable, Equatable, Hashable {
}
}

public func lastMessage() async throws -> DecodedMessage? {
public func lastMessage() async throws -> Message? {
switch self {
case let .group(group):
return try await group.lastMessage()
Expand Down Expand Up @@ -80,7 +80,7 @@ public enum Conversation: Identifiable, Equatable, Hashable {
}
}

public func processMessage(messageBytes: Data) async throws -> Message {
public func processMessage(messageBytes: Data) async throws -> Message? {
switch self {
case let .group(group):
return try await group.processMessage(messageBytes: messageBytes)
Expand Down Expand Up @@ -188,7 +188,7 @@ public enum Conversation: Identifiable, Equatable, Hashable {
}
}

public func streamMessages() -> AsyncThrowingStream<DecodedMessage, Error> {
public func streamMessages() -> AsyncThrowingStream<Message, Error> {
switch self {
case let .group(group):
return group.streamMessages()
Expand All @@ -203,7 +203,7 @@ public enum Conversation: Identifiable, Equatable, Hashable {
afterNs: Int64? = nil,
direction: SortDirection? = .descending,
deliveryStatus: MessageDeliveryStatus = .all
) async throws -> [DecodedMessage] {
) async throws -> [Message] {
switch self {
case let .group(group):
return try await group.messages(
Expand Down
13 changes: 5 additions & 8 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public actor Conversations {
}

public func streamAllMessages(type: ConversationType = .all)
-> AsyncThrowingStream<DecodedMessage, Error>
-> AsyncThrowingStream<Message, Error>
{
AsyncThrowingStream { continuation in
let ffiStreamActor = FfiStreamActor()
Expand All @@ -343,13 +343,10 @@ public actor Conversations {
}
return
}
do {
continuation.yield(
try Message(client: self.client, ffiMessage: message)
.decode()
)
} catch {
print("Error onMessage \(error)")
if let message = Message.create(
client: self.client, ffiMessage: message)
{
continuation.yield(message)
}
}

Expand Down
57 changes: 0 additions & 57 deletions Sources/XMTPiOS/DecodedMessage.swift

This file was deleted.

28 changes: 11 additions & 17 deletions Sources/XMTPiOS/Dm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public struct Dm: Identifiable, Equatable, Hashable {
return try ffiConversation.consentState().fromFFI
}

public func processMessage(messageBytes: Data) async throws -> Message {
public func processMessage(messageBytes: Data) async throws -> Message? {
let message =
try await ffiConversation.processStreamedConversationMessage(
envelopeBytes: messageBytes)
return Message(client: client, ffiMessage: message)
return Message.create(client: client, ffiMessage: message)
}

public func send<T>(content: T, options: SendOptions? = nil) async throws
Expand Down Expand Up @@ -167,7 +167,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
self.streamHolder.stream?.end()
}

public func streamMessages() -> AsyncThrowingStream<DecodedMessage, Error> {
public func streamMessages() -> AsyncThrowingStream<Message, Error> {
AsyncThrowingStream { continuation in
let task = Task.detached {
self.streamHolder.stream = await self.ffiConversation.stream(
Expand All @@ -177,14 +177,10 @@ public struct Dm: Identifiable, Equatable, Hashable {
continuation.finish()
return
}
do {
continuation.yield(
try Message(
client: self.client, ffiMessage: message
).decode())
} catch {
print("Error onMessage \(error)")
continuation.finish(throwing: error)
if let message = Message.create(
client: self.client, ffiMessage: message)
{
continuation.yield(message)
}
}
)
Expand All @@ -201,10 +197,9 @@ public struct Dm: Identifiable, Equatable, Hashable {
}
}

public func lastMessage() async throws -> DecodedMessage? {
public func lastMessage() async throws -> Message? {
if let ffiMessage = ffiLastMessage {
return Message(client: self.client, ffiMessage: ffiMessage)
.decodeOrNull()
return Message.create(client: self.client, ffiMessage: ffiMessage)
} else {
return try await messages(limit: 1).first
}
Expand All @@ -216,7 +211,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
limit: Int? = nil,
direction: SortDirection? = .descending,
deliveryStatus: MessageDeliveryStatus = .all
) async throws -> [DecodedMessage] {
) async throws -> [Message] {
var options = FfiListMessagesOptions(
sentBeforeNs: nil,
sentAfterNs: nil,
Expand Down Expand Up @@ -267,8 +262,7 @@ public struct Dm: Identifiable, Equatable, Hashable {
return try await ffiConversation.findMessages(opts: options).compactMap
{
ffiMessage in
return Message(client: self.client, ffiMessage: ffiMessage)
.decodeOrNull()
return Message.create(client: self.client, ffiMessage: ffiMessage)
}
}
}
28 changes: 11 additions & 17 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ public struct Group: Identifiable, Equatable, Hashable {
return try ffiGroup.consentState().fromFFI
}

public func processMessage(messageBytes: Data) async throws -> Message {
public func processMessage(messageBytes: Data) async throws -> Message? {
let message = try await ffiGroup.processStreamedConversationMessage(
envelopeBytes: messageBytes)
return Message(client: client, ffiMessage: message)
return Message.create(client: client, ffiMessage: message)
}

public func send<T>(content: T, options: SendOptions? = nil) async throws
Expand Down Expand Up @@ -365,7 +365,7 @@ public struct Group: Identifiable, Equatable, Hashable {
self.streamHolder.stream?.end()
}

public func streamMessages() -> AsyncThrowingStream<DecodedMessage, Error> {
public func streamMessages() -> AsyncThrowingStream<Message, Error> {
AsyncThrowingStream { continuation in
let task = Task.detached {
self.streamHolder.stream = await self.ffiGroup.stream(
Expand All @@ -375,14 +375,10 @@ public struct Group: Identifiable, Equatable, Hashable {
continuation.finish()
return
}
do {
continuation.yield(
try Message(
client: self.client, ffiMessage: message
).decode())
} catch {
print("Error onMessage \(error)")
continuation.finish(throwing: error)
if let message = Message.create(
client: self.client, ffiMessage: message)
{
continuation.yield(message)
}
}
)
Expand All @@ -399,10 +395,9 @@ public struct Group: Identifiable, Equatable, Hashable {
}
}

public func lastMessage() async throws -> DecodedMessage? {
public func lastMessage() async throws -> Message? {
if let ffiMessage = ffiLastMessage {
return Message(client: self.client, ffiMessage: ffiMessage)
.decodeOrNull()
return Message.create(client: self.client, ffiMessage: ffiMessage)
} else {
return try await messages(limit: 1).first
}
Expand All @@ -414,7 +409,7 @@ public struct Group: Identifiable, Equatable, Hashable {
limit: Int? = nil,
direction: SortDirection? = .descending,
deliveryStatus: MessageDeliveryStatus = .all
) async throws -> [DecodedMessage] {
) async throws -> [Message] {
var options = FfiListMessagesOptions(
sentBeforeNs: nil,
sentAfterNs: nil,
Expand Down Expand Up @@ -464,8 +459,7 @@ public struct Group: Identifiable, Equatable, Hashable {

return try await ffiGroup.findMessages(opts: options).compactMap {
ffiMessage in
return Message(client: self.client, ffiMessage: ffiMessage)
.decodeOrNull()
return Message.create(client: self.client, ffiMessage: ffiMessage)
}
}
}
Loading

0 comments on commit 21d9032

Please sign in to comment.