-
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.
feat: improve PreparedMessage handling
- Loading branch information
1 parent
10e9739
commit 16477f9
Showing
7 changed files
with
107 additions
and
73 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 |
---|---|---|
@@ -1,27 +1,36 @@ | ||
// | ||
// PreparedMessage.swift | ||
// | ||
// | ||
// Created by Pat Nakajima on 3/9/23. | ||
// | ||
|
||
import CryptoKit | ||
import Foundation | ||
|
||
// This houses a fully prepared message that can be published | ||
// as soon as the API client has connectivity. | ||
// | ||
// To support persistance layers that queue pending messages (e.g. while offline) | ||
// this struct supports serializing to/from bytes that can be written to disk or elsewhere. | ||
// See serializedData() and fromSerializedData() | ||
public struct PreparedMessage { | ||
var messageEnvelope: Envelope | ||
var conversation: Conversation | ||
var onSend: () async throws -> Void | ||
|
||
// The first envelope should send the message to the conversation itself. | ||
// Any more are for required intros/invites etc. | ||
// A client can just publish these when it has connectivity. | ||
public let envelopes: [Envelope] | ||
|
||
// Note: we serialize as a PublishRequest as a convenient `envelopes` wrapper. | ||
public static func fromSerializedData(_ serializedData: Data) throws -> PreparedMessage { | ||
let req = try Xmtp_MessageApi_V1_PublishRequest(serializedData: serializedData) | ||
return PreparedMessage(envelopes: req.envelopes) | ||
} | ||
|
||
public func decodedMessage() throws -> DecodedMessage { | ||
return try conversation.decode(messageEnvelope) | ||
} | ||
// Note: we serialize as a PublishRequest as a convenient `envelopes` wrapper. | ||
public func serializedData() throws -> Data { | ||
let req = Xmtp_MessageApi_V1_PublishRequest.with { $0.envelopes = envelopes } | ||
return try req.serializedData() | ||
} | ||
|
||
public func send() async throws { | ||
try await onSend() | ||
} | ||
public var messageID: String { | ||
Data(SHA256.hash(data: envelopes[0].message)).toHex | ||
} | ||
|
||
var messageID: String { | ||
Data(SHA256.hash(data: messageEnvelope.message)).toHex | ||
} | ||
public var conversationTopic: String { | ||
envelopes[0].contentTopic | ||
} | ||
} |
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