Skip to content

Commit

Permalink
feat: Move fallback content definition to content codecs (#153)
Browse files Browse the repository at this point in the history
* feat: add fallbacks to supported content types

* bump the pod spec

* update the copy to the approved copy
  • Loading branch information
nplasterer authored Sep 9, 2023
1 parent 00d14b1 commit 0ae04c8
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Sources/XMTP/Codecs/AttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public struct AttachmentCodec: ContentCodec {

return attachment
}

public func fallback(content: Attachment) throws -> String? {
return "Can’t display “\(content.filename)”. This app doesn’t support attachments."
}
}
4 changes: 4 additions & 0 deletions Sources/XMTP/Codecs/Composite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ struct CompositeCodec: ContentCodec {
let decodedComposite = fromComposite(composite: composite)
return decodedComposite
}

public func fallback(content: DecodedComposite) throws -> String? {
return nil
}

func toComposite(content decodedComposite: DecodedComposite) -> Composite {
var composite = Composite()
Expand Down
1 change: 1 addition & 0 deletions Sources/XMTP/Codecs/ContentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public protocol ContentCodec: Hashable, Equatable {
var contentType: ContentTypeID { get }
func encode(content: T) throws -> EncodedContent
func decode(content: EncodedContent) throws -> T
func fallback(content: T) throws -> String?
}

public extension ContentCodec {
Expand Down
9 changes: 9 additions & 0 deletions Sources/XMTP/Codecs/ReactionCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ public struct ReactionCodec: ContentCodec {
)
//swiftlint:disable force_unwrapping
}

public func fallback(content: Reaction) throws -> String? {
switch content.action {
case .added:
return "Reacted “\(content.content)” to an earlier message"
case .removed:
return "Removed “\(content.content)” from an earlier message"
}
}
}
4 changes: 4 additions & 0 deletions Sources/XMTP/Codecs/ReadReceiptCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public struct ReadReceiptCodec: ContentCodec {

return ReadReceipt(timestamp: timestamp)
}

public func fallback(content: ReadReceipt) throws -> String? {
return nil
}
}
5 changes: 5 additions & 0 deletions Sources/XMTP/Codecs/RemoteAttachmentCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public struct RemoteAttachment {
}

public struct RemoteAttachmentCodec: ContentCodec {

public typealias T = RemoteAttachment

public init() {}
Expand Down Expand Up @@ -189,6 +190,10 @@ public struct RemoteAttachmentCodec: ContentCodec {

return attachment
}

public func fallback(content: RemoteAttachment) throws -> String? {
return "Can’t display “\(String(describing: content.filename))”. This app doesn’t support attachments."
}

private func getHexParameter(_ name: String, from parameters: [String: String]) throws -> Data {
guard let parameterHex = parameters[name] else {
Expand Down
4 changes: 4 additions & 0 deletions Sources/XMTP/Codecs/ReplyCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public struct ReplyCodec: ContentCodec {
throw CodecError.invalidContent
}
}

public func fallback(content: Reply) throws -> String? {
return "Replied with “\(content.content)” to an earlier message"
}
}
5 changes: 5 additions & 0 deletions Sources/XMTP/Codecs/TextCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum TextCodecError: Error {
}

public struct TextCodec: ContentCodec {

public typealias T = String

public init() {}
Expand Down Expand Up @@ -41,4 +42,8 @@ public struct TextCodec: ContentCodec {
throw TextCodecError.unknownDecodingError
}
}

public func fallback(content: String) throws -> String? {
return nil
}
}
15 changes: 13 additions & 2 deletions Sources/XMTP/ConversationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,19 @@ public struct ConversationV1 {

let content = content as T
var encoded = try encode(codec: codec, content: content)
encoded.fallback = options?.contentFallback ?? ""


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

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

if let compression = options?.compression {
encoded = try encoded.compress(compression)
}
Expand Down
17 changes: 14 additions & 3 deletions Sources/XMTP/ConversationV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,20 @@ public struct ConversationV2 {
}

var encoded = try encode(codec: codec, content: content)
encoded.fallback = options?.contentFallback ?? ""

if let compression = options?.compression {

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

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

if let compression = options?.compression {
encoded = try encoded.compress(compression)
}

Expand Down
4 changes: 1 addition & 3 deletions Sources/XMTP/SendOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import Foundation
public struct SendOptions {
public var compression: EncodedContentCompression?
public var contentType: ContentTypeID?
public var contentFallback: String?
public var ephemeral: Bool = false

public init(compression: EncodedContentCompression? = nil, contentType: ContentTypeID? = nil, contentFallback: String? = nil, ephemeral: Bool = false) {
public init(compression: EncodedContentCompression? = nil, contentType: ContentTypeID? = nil, ephemeral: Bool = false) {
self.compression = compression
self.contentType = contentType
self.contentFallback = contentFallback
self.ephemeral = ephemeral
}
}
6 changes: 5 additions & 1 deletion Tests/XMTPTests/CodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import XCTest
@testable import XMTP

struct NumberCodec: ContentCodec {
func fallback(content: Double) throws -> String? {
return "pi"
}

typealias T = Double

var contentType: XMTP.ContentTypeID {
Expand Down Expand Up @@ -57,7 +61,7 @@ class CodecTests: XCTestCase {
let aliceClient = fixtures.aliceClient!
let aliceConversation = try await aliceClient.conversations.newConversation(with: fixtures.bob.address)

try await aliceConversation.send(content: 3.14, options: .init(contentType: NumberCodec().contentType, contentFallback: "pi"))
try await aliceConversation.send(content: 3.14, options: .init(contentType: NumberCodec().contentType))

// Remove number codec from registry
Client.codecRegistry.codecs.removeValue(forKey: NumberCodec().id)
Expand Down
2 changes: 1 addition & 1 deletion Tests/XMTPTests/RemoteAttachmentTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RemoteAttachmentTests: XCTestCase {
remoteAttachmentContent.filename = "hello.txt"
remoteAttachmentContent.contentLength = 5

_ = try await conversation.send(content: remoteAttachmentContent, options: .init(contentType: ContentTypeRemoteAttachment, contentFallback: "hey"))
_ = try await conversation.send(content: remoteAttachmentContent, options: .init(contentType: ContentTypeRemoteAttachment))
}

func testCanUseAttachmentCodec() async throws {
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "XMTP"
spec.version = "0.5.2-alpha0"
spec.version = "0.5.3-alpha0"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down

0 comments on commit 0ae04c8

Please sign in to comment.