-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update to latest rust version * update the package resolve * filter out messages by kind * tweak the logic a big * write tests for it * bump the pod
- Loading branch information
1 parent
636e4a1
commit b77879f
Showing
9 changed files
with
141 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// MessageV3.swift | ||
// | ||
// | ||
// Created by Naomi Plasterer on 4/10/24. | ||
// | ||
|
||
import Foundation | ||
import LibXMTP | ||
|
||
enum MessageV3Error: Error { | ||
case decodeError(String) | ||
} | ||
|
||
public struct MessageV3: Identifiable { | ||
let client: Client | ||
let ffiMessage: FfiMessage | ||
|
||
init(client: Client, ffiMessage: FfiMessage) { | ||
self.client = client | ||
self.ffiMessage = ffiMessage | ||
} | ||
|
||
public var id: Data { | ||
return ffiMessage.id | ||
} | ||
|
||
var convoId: Data { | ||
return ffiMessage.convoId | ||
} | ||
|
||
var senderAddress: String { | ||
return ffiMessage.addrFrom | ||
} | ||
|
||
var sentAt: Date { | ||
return Date(timeIntervalSince1970: TimeInterval(ffiMessage.sentAtNs) / 1_000_000_000) | ||
} | ||
|
||
public func decode() throws -> DecodedMessage { | ||
do { | ||
let encodedContent = try EncodedContent(serializedData: ffiMessage.content) | ||
|
||
let decodedMessage = DecodedMessage( | ||
id: id.toHex, | ||
client: client, | ||
topic: Topic.groupMessage(convoId.toHex).description, | ||
encodedContent: encodedContent, | ||
senderAddress: senderAddress, | ||
sent: sentAt | ||
) | ||
|
||
if decodedMessage.encodedContent.type == ContentTypeGroupMembershipChanged && ffiMessage.kind != .membershipChange { | ||
throw MessageV3Error.decodeError("Error decoding group membership change") | ||
} | ||
|
||
return decodedMessage | ||
} catch { | ||
throw MessageV3Error.decodeError("Error decoding message: \(error.localizedDescription)") | ||
} | ||
} | ||
|
||
public func decodeOrNull() -> DecodedMessage? { | ||
do { | ||
return try decode() | ||
} catch { | ||
print("MESSAGE_V3: discarding message that failed to decode", error) | ||
return nil | ||
} | ||
} | ||
|
||
public func decryptOrNull() -> DecryptedMessage? { | ||
do { | ||
return try decrypt() | ||
} catch { | ||
print("MESSAGE_V3: discarding message that failed to decrypt", error) | ||
return nil | ||
} | ||
} | ||
|
||
public func decrypt() throws -> DecryptedMessage { | ||
let encodedContent = try EncodedContent(serializedData: ffiMessage.content) | ||
|
||
let decrytedMessage = DecryptedMessage( | ||
id: id.toHex, | ||
encodedContent: encodedContent, | ||
senderAddress: senderAddress, | ||
sentAt: Date(), | ||
topic: Topic.groupMessage(convoId.toHex).description | ||
) | ||
|
||
if decrytedMessage.encodedContent.type == ContentTypeGroupMembershipChanged && ffiMessage.kind != .membershipChange { | ||
throw MessageV3Error.decodeError("Error decoding group membership change") | ||
} | ||
|
||
return decrytedMessage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters