Skip to content

Commit

Permalink
Merge pull request #48 from xmtp/np/ios-push-code
Browse files Browse the repository at this point in the history
iOS Push server
  • Loading branch information
nplasterer authored Jun 2, 2023
2 parents bd825f0 + ec32120 commit 3fd91b3
Showing 1 changed file with 32 additions and 10 deletions.
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

0 comments on commit 3fd91b3

Please sign in to comment.