Skip to content

Commit

Permalink
One instance per push
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolmello committed Feb 27, 2024
1 parent f56f19a commit b9f8bbb
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 24 deletions.
6 changes: 5 additions & 1 deletion ios/NotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ios/ReplyNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions ios/Shared/RocketChat/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 3 additions & 2 deletions ios/Shared/RocketChat/Encryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 0 additions & 13 deletions ios/Shared/RocketChat/RocketChat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions ios/Shared/RocketChat/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ struct Credentials {
}

class Storage {
static let shared = Storage()

final var mmkv: MMKV? = nil

init() {
Expand Down

0 comments on commit b9f8bbb

Please sign in to comment.