Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/xmtp/xmtp-react-native into…
Browse files Browse the repository at this point in the history
… np/fix-reaction-codec-crash
  • Loading branch information
nplasterer committed Sep 14, 2023
2 parents 6851f64 + ba299bd commit 4665673
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class XMTPModule : Module() {
DecryptedLocalAttachment(
fileUri = file.toURI().toString(),
mimeType = attachment.mimeType,
filename = attachment.filename
).toJson()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.google.gson.JsonParser
class DecryptedLocalAttachment(
val fileUri: String,
val mimeType: String,
val filename: String,
) {
companion object {
fun fromJsonObject(obj: JsonObject) = DecryptedLocalAttachment(
obj.get("fileUri").asString,
obj.get("mimeType").asString,
obj.get("filename")?.asString ?: "",
)

fun fromJson(json: String): DecryptedLocalAttachment {
Expand All @@ -26,6 +28,7 @@ class DecryptedLocalAttachment(
fun toJsonMap(): Map<String, Any> = mapOf(
"fileUri" to fileUri,
"mimeType" to mimeType,
"filename" to filename,
)

fun toJson(): String = GsonBuilder().create().toJson(toJsonMap())
Expand Down
3 changes: 3 additions & 0 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ test("remote attachments should work", async () => {
if (attached.mimeType !== "text/plain") {
throw new Error("Expected mimeType to match");
}
if (attached.filename !== filename) {
throw new Error(`Expected ${attached.filename} to equal ${filename}`);
}
const text = await fs.readFile(new URL(attached.fileUri).pathname, "utf8");
if (text !== "hello world") {
throw new Error("Expected text to match");
Expand Down
7 changes: 5 additions & 2 deletions ios/Wrappers/DecodedMessageWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,23 @@ struct EncryptedLocalAttachment {
struct DecryptedLocalAttachment {
var fileUri: String
var mimeType: String
var filename: String

static func fromJson(_ json: String) throws -> DecryptedLocalAttachment {
let data = json.data(using: .utf8)!
let obj = (try? JSONSerialization.jsonObject(with: data) as? [String: Any]) ?? [:]
return DecryptedLocalAttachment(
fileUri: obj["fileUri"] as? String ?? "",
mimeType: obj["mimeType"] as? String ?? ""
mimeType: obj["mimeType"] as? String ?? "",
filename: obj["filename"] as? String ?? ""
)
}

func toJson() throws -> String {
let obj: [String: Any] = [
"fileUri": fileUri,
"mimeType": mimeType
"mimeType": mimeType,
"filename": filename
]
return try obj.toJson()
}
Expand Down
3 changes: 2 additions & 1 deletion ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ public class XMTPModule: Module {
try attachment.data.write(to: file)
return try DecryptedLocalAttachment(
fileUri: file.absoluteString,
mimeType: attachment.mimeType
mimeType: attachment.mimeType,
filename: attachment.filename
).toJson()
}

Expand Down
1 change: 1 addition & 0 deletions src/XMTP.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type StaticAttachmentContent = {
export type DecryptedLocalAttachment = {
fileUri: string;
mimeType?: string;
filename?: string;
};

export type RemoteAttachmentMetadata = {
Expand Down

0 comments on commit 4665673

Please sign in to comment.