Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proposal encode should push #238

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/xmtp/libxmtp-swift",
"state" : {
"branch" : "92274fe",
"revision" : "92274fe0dde1fc7f8f716ebcffa3d252813be56d"
"branch" : "503086d",
"revision" : "503086d91ba6c3420aca118e4812d62a004d17ee"
}
},
{
Expand Down
20 changes: 14 additions & 6 deletions Sources/XMTPiOS/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ public struct ConversationV2 {
}

func prepareMessage(encodedContent: EncodedContent, options: SendOptions?) async throws -> PreparedMessage {
let codec = client.codecRegistry.find(for: options?.contentType)

let message = try await MessageV2.encode(
client: client,
content: encodedContent,
topic: topic,
keyMaterial: keyMaterial,
codec: codec
keyMaterial: keyMaterial
)

let topic = options?.ephemeral == true ? ephemeralTopic : topic
Expand Down Expand Up @@ -122,6 +119,18 @@ public struct ConversationV2 {
if let compression = options?.compression {
encoded = try encoded.compress(compression)
}

func shouldPush<Codec: ContentCodec>(codec: Codec, content: Any) throws -> Bool? {
if let content = content as? Codec.T {
return try codec.shouldPush(content: content)
} else {
throw CodecError.invalidContent
}
}

if let shouldPush = try shouldPush(codec: codec, content: content) {
encoded.shouldPush = shouldPush
}

return try await prepareMessage(encodedContent: encoded, options: options)
}
Expand Down Expand Up @@ -236,8 +245,7 @@ public struct ConversationV2 {
client: client,
content: content,
topic: topic,
keyMaterial: keyMaterial,
codec: codec
keyMaterial: keyMaterial
)

let envelope = Envelope(
Expand Down
8 changes: 3 additions & 5 deletions Sources/XMTPiOS/Messages/MessageV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ extension MessageV2 {
}
}

static func encode<Codec: ContentCodec>(client: Client, content encodedContent: EncodedContent, topic: String, keyMaterial: Data, codec: Codec) async throws -> MessageV2 {
static func encode(client: Client, content encodedContent: EncodedContent, topic: String, keyMaterial: Data) async throws -> MessageV2 {
let payload = try encodedContent.serializedData()

let shouldPush = encodedContent.shouldPush

let date = Date()
let header = MessageHeaderV2(topic: topic, created: date)
let headerBytes = try header.serializedData()
Expand All @@ -106,9 +107,6 @@ extension MessageV2 {

let senderHmac = try Crypto.generateHmacSignature(secret: keyMaterial, info: infoEncoded, message: headerBytes)

let decoded = try codec.decode(content: encodedContent, client: client)
let shouldPush = try codec.shouldPush(content: decoded)


return MessageV2(
headerBytes: headerBytes,
Expand Down
11 changes: 10 additions & 1 deletion Sources/XMTPiOS/Proto/message_contents/content.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public struct Xmtp_MessageContents_EncodedContent {

/// encoded content itself
public var content: Data = Data()

/// Indicates whether the content should be pushed.
public var shouldPush: Bool = false

public var unknownFields = SwiftProtobuf.UnknownStorage()

Expand Down Expand Up @@ -256,6 +259,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto
3: .same(proto: "fallback"),
5: .same(proto: "compression"),
4: .same(proto: "content"),
6: .same(proto: "shouldPush"),
]

public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
Expand All @@ -269,6 +273,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto
case 3: try { try decoder.decodeSingularStringField(value: &self._fallback) }()
case 4: try { try decoder.decodeSingularBytesField(value: &self.content) }()
case 5: try { try decoder.decodeSingularEnumField(value: &self._compression) }()
case 6: try { try decoder.decodeSingularBoolField(value: &self.shouldPush) }()
default: break
}
}
Expand All @@ -294,7 +299,10 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto
try { if let v = self._compression {
try visitor.visitSingularEnumField(value: v, fieldNumber: 5)
} }()
try unknownFields.traverse(visitor: &visitor)
if self.shouldPush != false {
try visitor.visitSingularBoolField(value: self.shouldPush, fieldNumber: 6)
}
try unknownFields.traverse(visitor: &visitor)
}

public static func ==(lhs: Xmtp_MessageContents_EncodedContent, rhs: Xmtp_MessageContents_EncodedContent) -> Bool {
Expand All @@ -303,6 +311,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto
if lhs._fallback != rhs._fallback {return false}
if lhs._compression != rhs._compression {return false}
if lhs.content != rhs.content {return false}
if lhs.shouldPush != rhs.shouldPush {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
Expand Down
3 changes: 1 addition & 2 deletions Tests/XMTPTests/CodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class CodecTests: XCTestCase {
client: aliceClient,
content: messages[0].encodedContent,
topic: aliceConversation.topic,
keyMaterial: Data(aliceConversation.keyMaterial!),
codec: NumberCodec()
keyMaterial: Data(aliceConversation.keyMaterial!)
)

XCTAssertEqual(false, message.shouldPush)
Expand Down
3 changes: 1 addition & 2 deletions Tests/XMTPTests/ConversationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ class ConversationTests: XCTestCase {
client: bobClient,
content: encodedContent,
topic: conversation.topic,
keyMaterial: conversation.keyMaterial,
codec: encoder
keyMaterial: conversation.keyMaterial
)
).serializedData()
)
Expand Down
2 changes: 1 addition & 1 deletion Tests/XMTPTests/MessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MessageTests: XCTestCase {
let sealedInvitation = try SealedInvitation.createV1(sender: alice.toV2(), recipient: bob.toV2().getPublicKeyBundle(), created: Date(), invitation: invitationv1)
let encoder = TextCodec()
let encodedContent = try encoder.encode(content: "Yo!", client: client)
let message1 = try await MessageV2.encode(client: client, content: encodedContent, topic: invitationv1.topic, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial, codec: encoder)
let message1 = try await MessageV2.encode(client: client, content: encodedContent, topic: invitationv1.topic, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial)

let decoded = try MessageV2.decode("", "", message1, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial, client: client)
let result: String = try decoded.content()
Expand Down
Loading