One instance per push
This commit is contained in:
parent
afe50aed7e
commit
d0ad1967b7
|
@ -16,7 +16,7 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
return
|
||||
}
|
||||
|
||||
rocketchat = RocketChat.instanceForServer(server: data.host.removeTrailingSlash())
|
||||
rocketchat = RocketChat(server: data.host.removeTrailingSlash())
|
||||
|
||||
// If the notification has the content on the payload, show it
|
||||
if data.notificationType != .messageIdOnly {
|
||||
|
@ -35,6 +35,10 @@ class NotificationService: UNNotificationServiceExtension {
|
|||
}
|
||||
}
|
||||
|
||||
override func serviceExtensionTimeWillExpire() {
|
||||
rocketchat = nil
|
||||
}
|
||||
|
||||
func processPayload(payload: Payload) {
|
||||
// If is a encrypted message
|
||||
if payload.messageType == .e2e {
|
||||
|
|
|
@ -32,7 +32,7 @@ class ReplyNotification: RNNotificationEventHandler {
|
|||
if let data = (notification["ejson"] as? String)?.data(using: .utf8) {
|
||||
if let payload = try? JSONDecoder().decode(Payload.self, from: data), let rid = payload.rid {
|
||||
if let msg = (response as? UNTextInputNotificationResponse)?.userText {
|
||||
let rocketchat = RocketChat.instanceForServer(server: payload.host.removeTrailingSlash())
|
||||
let rocketchat = RocketChat(server: payload.host.removeTrailingSlash())
|
||||
let backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
|
||||
rocketchat.sendMessage(rid: rid, message: msg, threadIdentifier: payload.tmid) { response in
|
||||
guard let response = response, response.success else {
|
||||
|
|
|
@ -34,19 +34,21 @@ final class API {
|
|||
final let credentials: Credentials?
|
||||
final let decoder = JSONDecoder()
|
||||
|
||||
static var instances: [Server: API] = [:]
|
||||
|
||||
convenience init?(server: Server) {
|
||||
guard let server = URL(string: server.removeTrailingSlash()) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.init(server: server)
|
||||
guard let credentials = Storage().getCredentials(server: server.absoluteString) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.init(server: server, credentials: credentials)
|
||||
}
|
||||
|
||||
init(server: URL) {
|
||||
init(server: URL, credentials: Credentials) {
|
||||
self.server = server
|
||||
self.credentials = Storage.shared.getCredentials(server: server.absoluteString)
|
||||
self.credentials = credentials
|
||||
}
|
||||
|
||||
func fetch<T: Request>(request: T, retry: Retry? = nil, completion: @escaping((APIResponse<T.ResponseType>) -> Void)) {
|
||||
|
|
|
@ -37,8 +37,9 @@ final class Encryption {
|
|||
private final let encoder = JSONEncoder()
|
||||
|
||||
init(server: String, rid: String) {
|
||||
self.privateKey = Storage.shared.getPrivateKey(server: server)
|
||||
self.credentials = Storage.shared.getCredentials(server: server)
|
||||
let storage = Storage()
|
||||
self.privateKey = storage.getPrivateKey(server: server)
|
||||
self.credentials = storage.getCredentials(server: server)
|
||||
self.server = server
|
||||
self.rid = rid
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ final class RocketChat {
|
|||
let server: Server
|
||||
let api: API?
|
||||
|
||||
static var instances: [Server: RocketChat] = [:]
|
||||
var encryptionInstances: [RoomId: Encryption] = [:]
|
||||
|
||||
static private var queue = DispatchQueue(label: "chat.rocket.instanceQueue")
|
||||
|
@ -26,18 +25,6 @@ final class RocketChat {
|
|||
self.api = API(server: server)
|
||||
}
|
||||
|
||||
static func instanceForServer(server: Server) -> RocketChat {
|
||||
queue.sync {
|
||||
if let rocketchat = instances[server] {
|
||||
return rocketchat
|
||||
}
|
||||
|
||||
let rocketchat = RocketChat(server: server)
|
||||
instances[server] = rocketchat
|
||||
return rocketchat
|
||||
}
|
||||
}
|
||||
|
||||
func getPushWithId(_ msgId: String, completion: @escaping((Notification?) -> Void)) {
|
||||
api?.fetch(request: PushRequest(msgId: msgId), retry: Retry(retries: 4)) { response in
|
||||
switch response {
|
||||
|
|
|
@ -14,8 +14,6 @@ struct Credentials {
|
|||
}
|
||||
|
||||
class Storage {
|
||||
static let shared = Storage()
|
||||
|
||||
final var mmkv: MMKV? = nil
|
||||
|
||||
init() {
|
||||
|
|
Loading…
Reference in New Issue