Skip to content

Commit

Permalink
update groups to inherit from conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Feb 14, 2024
1 parent 00dedd3 commit ed0be28
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
61 changes: 37 additions & 24 deletions Sources/XMTPiOS/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
//

import Foundation
import LibXMTP

public enum ConversationContainer: Codable {
case v1(ConversationV1Container), v2(ConversationV2Container), group(GroupContainer)
case v1(ConversationV1Container), v2(ConversationV2Container)

public func decode(with client: Client) -> Conversation {
switch self {
case let .v1(container):
return .v1(container.decode(with: client))
case let .v2(container):
return .v2(container.decode(with: client))
case let .group(container):
return .group(container.decode(with: client))
}
}
}
Expand Down Expand Up @@ -68,14 +67,14 @@ public enum Conversation: Sendable {
}
}

public var encodedContainer: ConversationContainer {
public func encodedContainer() throws -> ConversationContainer {
switch self {
case let .v1(conversationV1):
return .v1(conversationV1.encodedContainer)
case let .v2(conversationV2):
return .v2(conversationV2.encodedContainer)
case let .group(group):
return .group(group.encodedContainer)
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -87,7 +86,11 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return conversationV2.peerAddress
case let .group(group):
<#code#>
var addresses = group.memberAddresses
if let index = addresses.firstIndex(of: clientAddress) {
addresses.remove(at: index)
}
return addresses.joined(separator: ",")
}
}

Expand All @@ -98,7 +101,11 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return [conversationV2.peerAddress]
case let .group(group):
<#code#>
var addresses = group.memberAddresses
if let index = addresses.firstIndex(of: clientAddress) {
addresses.remove(at: index)
}
return addresses
}
}

Expand Down Expand Up @@ -145,25 +152,31 @@ public enum Conversation: Sendable {
}
}

public func decode(_ envelope: Envelope) throws -> DecodedMessage {
public func decode(_ envelope: Envelope, message: FfiMessage? = nil) throws -> DecodedMessage {
switch self {
case let .v1(conversationV1):
return try conversationV1.decode(envelope: envelope)
case let .v2(conversationV2):
return try conversationV2.decode(envelope: envelope)
case let .group(group):
<#code#>
guard let messageDecoded = try message?.fromFFI(client: client) else {
throw GroupError.groupsRequireMessagePassed
}
return messageDecoded
}
}

public func decrypt(_ envelope: Envelope) throws -> DecryptedMessage {
public func decrypt(_ envelope: Envelope, message: FfiMessage? = nil) throws -> DecryptedMessage {
switch self {
case let .v1(conversationV1):
return try conversationV1.decrypt(envelope: envelope)
case let .v2(conversationV2):
return try conversationV2.decrypt(envelope: envelope)
case let .group(group):
<#code#>
guard let messageDecrypted = try message?.fromFFIDecrypted(client: client) else {
throw GroupError.groupsRequireMessagePassed
}
return messageDecrypted
}
}

Expand All @@ -174,7 +187,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.encode(codec: codec, content: content)
case let .group(group):
<#code#>
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -185,7 +198,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.prepareMessage(encodedContent: encodedContent, options: options)
case let .group(group):
<#code#>
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -196,7 +209,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.prepareMessage(content: content, options: options ?? .init())
case let .group(group):
<#code#>
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -209,7 +222,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.send(prepared: prepared)
case let .group(group):
<#code#>
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -220,7 +233,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.send(content: content, options: options)
case let .group(group):
<#code#>
return try await group.send(content: content, options: options)
}
}

Expand All @@ -231,7 +244,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.send(encodedContent: encodedContent, options: options)
case let .group(group):
<#code#>
return try await group.send(content: encodedContent, options: options)
}
}

Expand All @@ -243,7 +256,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.send(content: text, options: options)
case let .group(group):
<#code#>
return try await group.send(content: text, options: options)
}
}

Expand All @@ -263,14 +276,14 @@ public enum Conversation: Sendable {
}
}

public func streamEphemeral() -> AsyncThrowingStream<Envelope, Error>? {
public func streamEphemeral() throws -> AsyncThrowingStream<Envelope, Error>? {
switch self {
case let .v1(conversation):
return conversation.streamEphemeral()
case let .v2(conversation):
return conversation.streamEphemeral()
case let .group(group):
<#code#>
throw GroupError.notSupportedByGroups
}
}

Expand All @@ -285,7 +298,7 @@ public enum Conversation: Sendable {
case let .v2(conversation):
return conversation.streamMessages()
case let .group(group):
<#code#>
return group.streamMessages()
}
}

Expand All @@ -296,7 +309,7 @@ public enum Conversation: Sendable {
case let .v2(conversation):
return conversation.streamDecryptedMessages()
case let .group(group):
<#code#>
return group.streamDecryptedMessages()
}
}

Expand All @@ -308,7 +321,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.messages(limit: limit, before: before, after: after, direction: direction)
case let .group(group):
return try await group.messages(before: before, after: after, limit: limit)
return try await group.messages(before: before, after: after, limit: limit, direction: direction)
}
}

Expand All @@ -319,7 +332,7 @@ public enum Conversation: Sendable {
case let .v2(conversationV2):
return try await conversationV2.decryptedMessages(limit: limit, before: before, after: after, direction: direction)
case let .group(group):
return try await group.decryptedMessages(before: before, after: after, limit: limit)
return try await group.decryptedMessages(before: before, after: after, limit: limit, direction: direction)
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum ConversationError: Error, CustomStringConvertible {
}

public enum GroupError: Error, CustomStringConvertible {
case alphaMLSNotEnabled, emptyCreation, memberCannotBeSelf, memberNotRegistered([String])
case alphaMLSNotEnabled, emptyCreation, memberCannotBeSelf, memberNotRegistered([String]), groupsRequireMessagePassed, notSupportedByGroups

public var description: String {
switch self {
Expand All @@ -29,6 +29,10 @@ public enum GroupError: Error, CustomStringConvertible {
return "GroupError.memberCannotBeSelf you cannot add yourself to a group"
case .memberNotRegistered(let array):
return "GroupError.memberNotRegistered members not registered: \(array.joined(separator: ", "))"
case .groupsRequireMessagePassed:
return "GroupError.groupsRequireMessagePassed you cannot call this method without passing a message instead of an envelope"
case .notSupportedByGroups:
return "GroupError.notSupportedByGroups this method is not supported by groups"
}
}
}
Expand Down
36 changes: 28 additions & 8 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ import LibXMTP

final class MessageCallback: FfiMessageCallback {
let client: Client
let callback: (DecodedMessage) -> Void
let callback: (LibXMTP.FfiMessage) -> Void

init(client: Client, _ callback: @escaping (DecodedMessage) -> Void) {
init(client: Client, _ callback: @escaping (LibXMTP.FfiMessage) -> Void) {
self.client = client
self.callback = callback
}

func onMessage(message: LibXMTP.FfiMessage) {
do {
try callback(message.fromFFI(client: client))
} catch {
print("Error onMessage \(error)")
}
callback(message)
}
}

Expand Down Expand Up @@ -136,7 +132,31 @@ public struct Group: Identifiable, Equatable, Hashable {
do {
self.streamHolder.stream = try await ffiGroup.stream(
messageCallback: MessageCallback(client: self.client) { message in
continuation.yield(message)
do {
continuation.yield(try message.fromFFI(client: self.client))
} catch {
print("Error onMessage \(error)")
}
}
)
} catch {
print("STREAM ERR: \(error)")
}
}
}
}

public func streamDecryptedMessages() -> AsyncThrowingStream<DecryptedMessage, Error> {
AsyncThrowingStream { continuation in
Task.detached {
do {
self.streamHolder.stream = try await ffiGroup.stream(
messageCallback: MessageCallback(client: self.client) { message in
do {
continuation.yield(try message.fromFFIDecrypted(client: self.client))
} catch {
print("Error onMessage \(error)")
}
}
)
} catch {
Expand Down

0 comments on commit ed0be28

Please sign in to comment.