From f364ee3907b83ea14550c07800b4b4529bcdea1a Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Wed, 13 Jan 2021 16:10:06 +1100 Subject: [PATCH] WIP --- Session/Meta/AppDelegate.swift | 3 +- Session/Open Groups/JoinPublicChatVC.swift | 9 +++--- .../To Do/OpenGroupManager.swift | 2 +- .../To Do/PublicChatManager.swift | 32 +++++++++---------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index e23f8c834..960435ec7 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -13,13 +13,14 @@ extension AppDelegate { let job = MessageSendJob(message: configurationMessage, destination: destination) JobQueue.shared.add(job, using: transaction) } + userDefaults[.lastConfigurationSync] = Date() } func forceSyncConfigurationNowIfNeeded() -> Promise { let configurationMessage = ConfigurationMessage.getCurrent() let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey()) let (promise, seal) = Promise.pending() - Storage.write { transaction in + Storage.writeSync { transaction in MessageSender.send(configurationMessage, to: destination, using: transaction).done { seal.fulfill(()) }.catch { _ in diff --git a/Session/Open Groups/JoinPublicChatVC.swift b/Session/Open Groups/JoinPublicChatVC.swift index 203820574..16ecdb6bf 100644 --- a/Session/Open Groups/JoinPublicChatVC.swift +++ b/Session/Open Groups/JoinPublicChatVC.swift @@ -132,11 +132,9 @@ final class JoinPublicChatVC : BaseVC, UIPageViewControllerDataSource, UIPageVie } isJoining = true ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in - Storage.shared.write { transaction in + Storage.shared.write(with: { transaction in OpenGroupManager.shared.addOpenGroup(with: urlAsString, using: transaction) .done(on: DispatchQueue.main) { [weak self] _ in - let appDelegate = UIApplication.shared.delegate as! AppDelegate - appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete() // FIXME: It's probably cleaner to do this inside addOpenGroup(...) self?.presentingViewController!.dismiss(animated: true, completion: nil) } .catch(on: DispatchQueue.main) { [weak self] error in @@ -150,7 +148,10 @@ final class JoinPublicChatVC : BaseVC, UIPageViewControllerDataSource, UIPageVie self?.isJoining = false self?.showError(title: title, message: message) } - } + }, completion: { + let appDelegate = UIApplication.shared.delegate as! AppDelegate + appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete() // FIXME: It's probably cleaner to do this inside addOpenGroup(...) + }) } } diff --git a/SignalUtilitiesKit/To Do/OpenGroupManager.swift b/SignalUtilitiesKit/To Do/OpenGroupManager.swift index 1c230aedb..241067f52 100644 --- a/SignalUtilitiesKit/To Do/OpenGroupManager.swift +++ b/SignalUtilitiesKit/To Do/OpenGroupManager.swift @@ -30,7 +30,7 @@ public final class OpenGroupManager : OpenGroupManagerProtocol { let transaction = transaction as! YapDatabaseReadWriteTransaction transaction.removeObject(forKey: "\(urlAsString).\(channelID)", inCollection: Storage.lastMessageServerIDCollection) transaction.removeObject(forKey: "\(urlAsString).\(channelID)", inCollection: Storage.lastDeletionServerIDCollection) - return PublicChatManager.shared.addChat(server: urlAsString, channel: channelID).done(on: DispatchQueue.main) { _ in + return PublicChatManager.shared.addChat(server: urlAsString, channel: channelID, using: transaction).done(on: DispatchQueue.main) { _ in let _ = OpenGroupAPI.setDisplayName(to: displayName, on: urlAsString) let _ = OpenGroupAPI.setProfilePictureURL(to: profilePictureURL, using: profileKey, on: urlAsString) let _ = OpenGroupAPI.join(channelID, on: urlAsString) diff --git a/SignalUtilitiesKit/To Do/PublicChatManager.swift b/SignalUtilitiesKit/To Do/PublicChatManager.swift index 624ea7509..94e1e45b1 100644 --- a/SignalUtilitiesKit/To Do/PublicChatManager.swift +++ b/SignalUtilitiesKit/To Do/PublicChatManager.swift @@ -48,43 +48,43 @@ public final class PublicChatManager : NSObject { isPolling = false } - public func addChat(server: String, channel: UInt64) -> Promise { + public func addChat(server: String, channel: UInt64, using transaction: YapDatabaseReadWriteTransaction) -> Promise { if let existingChat = getChat(server: server, channel: channel) { - if let newChat = self.addChat(server: server, channel: channel, name: existingChat.displayName) { + if let newChat = self.addChat(server: server, channel: channel, name: existingChat.displayName, using: transaction) { return Promise.value(newChat) } else { return Promise(error: Error.chatCreationFailed) } } return OpenGroupAPI.getInfo(for: channel, on: server).map2 { channelInfo -> OpenGroup in - guard let chat = self.addChat(server: server, channel: channel, name: channelInfo.displayName) else { throw Error.chatCreationFailed } + guard let chat = self.addChat(server: server, channel: channel, name: channelInfo.displayName, using: transaction) else { throw Error.chatCreationFailed } return chat } } @discardableResult - @objc(addChatWithServer:channel:name:) - public func addChat(server: String, channel: UInt64, name: String) -> OpenGroup? { + @objc(addChatWithServer:channel:name:using:) + public func addChat(server: String, channel: UInt64, name: String, using transaction: YapDatabaseReadWriteTransaction) -> OpenGroup? { guard let chat = OpenGroup(channel: channel, server: server, displayName: name, isDeletable: true) else { return nil } let model = TSGroupModel(title: chat.displayName, memberIds: [userHexEncodedPublicKey!, chat.server], image: nil, groupId: LKGroupUtilities.getEncodedOpenGroupIDAsData(chat.id), groupType: .openGroup, adminIds: []) // Store the group chat mapping - Storage.writeSync { transaction in - let thread = TSGroupThread.getOrCreateThread(with: model, transaction: transaction) - - // Save the group chat - Storage.shared.setOpenGroup(chat, for: thread.uniqueId!, using: transaction) - } - + let thread = TSGroupThread.getOrCreateThread(with: model, transaction: transaction) + + // Save the group chat + Storage.shared.setOpenGroup(chat, for: thread.uniqueId!, using: transaction) + // Update chats and pollers - self.refreshChatsAndPollers() + transaction.addCompletionQueue(DispatchQueue.main) { + self.refreshChatsAndPollers() + } return chat } - @objc(addChatWithServer:channel:) - public func objc_addChat(server: String, channel: UInt64) -> AnyPromise { - return AnyPromise.from(addChat(server: server, channel: channel)) + @objc(addChatWithServer:channel:using:) + public func objc_addChat(server: String, channel: UInt64, using transaction: YapDatabaseReadWriteTransaction) -> AnyPromise { + return AnyPromise.from(addChat(server: server, channel: channel, using: transaction)) } @objc func refreshChatsAndPollers() {