Skip to content

Commit

Permalink
add back enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 7, 2024
1 parent 7b1f5f2 commit 15efb9f
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions Sources/XMTPiOS/Libxmtp/Message.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// MessageV3.swift
//
//
//
// Created by Naomi Plasterer on 4/10/24.
//
Expand All @@ -12,31 +12,40 @@ enum MessageError: Error {
case decodeError(String)
}

public enum MessageDeliveryStatus: String, RawRepresentable, Sendable {
case all,
published,
unpublished,
failed
}

public struct Message: Identifiable {
let client: Client
let ffiMessage: FfiMessage

init(client: Client, ffiMessage: FfiMessage) {
self.client = client
self.ffiMessage = ffiMessage
}

public var id: String {
return ffiMessage.id.toHex
}

var convoId: String {
return ffiMessage.convoId.toHex
}

var senderInboxId: String {
return ffiMessage.senderInboxId
}

var sentAt: Date {
return Date(timeIntervalSince1970: TimeInterval(ffiMessage.sentAtNs) / 1_000_000_000)
return Date(
timeIntervalSince1970: TimeInterval(ffiMessage.sentAtNs)
/ 1_000_000_000)
}

var deliveryStatus: MessageDeliveryStatus {
switch ffiMessage.deliveryStatus {
case .unpublished:
Expand All @@ -47,30 +56,36 @@ public struct Message: Identifiable {
return .failed
}
}

public func decode() throws -> DecodedMessage {
do {
let encodedContent = try EncodedContent(serializedData: ffiMessage.content)
let encodedContent = try EncodedContent(
serializedData: ffiMessage.content)

let decodedMessage = DecodedMessage(
id: id,
client: client,
topic: Topic.groupMessage(convoId).description,
encodedContent: encodedContent,
senderAddress: senderInboxId,
sent: sentAt,
deliveryStatus: deliveryStatus
)

if decodedMessage.encodedContent.type == ContentTypeGroupUpdated && ffiMessage.kind != .membershipChange {
throw MessageError.decodeError("Error decoding group membership change")
}


if decodedMessage.encodedContent.type == ContentTypeGroupUpdated
&& ffiMessage.kind != .membershipChange
{
throw MessageError.decodeError(
"Error decoding group membership change")
}

return decodedMessage
} catch {
throw MessageError.decodeError("Error decoding message: \(error.localizedDescription)")
throw MessageError.decodeError(
"Error decoding message: \(error.localizedDescription)")
}
}

public func decodeOrNull() -> DecodedMessage? {
do {
return try decode()
Expand Down

0 comments on commit 15efb9f

Please sign in to comment.