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

iOS Push server #48

Merged
merged 3 commits into from
Jun 2, 2023
Merged
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
42 changes: 32 additions & 10 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class XMTPModule: Module {
var subscriptions: [String: Task<Void, Never>] = [:]

enum Error: Swift.Error {
case noClient, conversationNotFound(String)
case noClient, conversationNotFound(String), noMessage
}

public func definition() -> ModuleDefinition {
Expand Down Expand Up @@ -224,18 +224,40 @@ public class XMTPModule: Module {
AsyncFunction("unsubscribeFromMessages") { (clientAddress: String, topic: String, conversationID: String?) in
try await unsubscribeFromMessages(clientAddress: clientAddress, topic: topic, conversationID: conversationID)
}

AsyncFunction("registerPushToken") { (pushServer: String, token: String) in
XMTPPush.shared.setPushServer(pushServer)
do {
try await XMTPPush.shared.register(token: token)
} catch {
print("Error registering: \(error)")
}
}

AsyncFunction("subscribePushTopics") { (topics: [String]) in
do {
try await XMTPPush.shared.subscribe(topics: topics)
} catch {
print("Error subscribing: \(error)")
}
}

Function("registerPushToken") { (pushServer: String, token: String) in
// TODO
}
AsyncFunction("decodeMessage") { (clientAddress: String, topic: String, encryptedMessage: String, conversationID: String?) -> String in
guard let encryptedMessageData = Data(base64Encoded: Data(encryptedMessage.utf8))else {
throw Error.noMessage
}

Function("subscribePushTopics") { (topics: [String]) in
// TODO
}
let envelope = XMTP.Envelope.with { envelope in
envelope.message = encryptedMessageData
envelope.contentTopic = topic
}

AsyncFunction("decodeMessage") { (clientAddress: String, topic: String, encryptedMessage: String, conversationID: String?) in
// TODO
}
guard let conversation = try await findConversation(clientAddress: clientAddress, topic: topic, conversationID: conversationID) else {
throw Error.conversationNotFound("no conversation found for \(topic)")
}
let decodedMessage = try conversation.decode(envelope)
return try DecodedMessageWrapper.encode(decodedMessage)
}
}

//
Expand Down