Force sync upon creating a closed group or joining an open group

pull/333/head
nielsandriesse 4 years ago
parent 947287e42f
commit b8d9334d19

@ -260,9 +260,9 @@ final class EditClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelega
Storage.write(with: { [weak self] transaction in Storage.write(with: { [weak self] transaction in
do { do {
if !members.contains(getUserHexEncodedPublicKey()) { if !members.contains(getUserHexEncodedPublicKey()) {
try MessageSender.leaveV2(groupPublicKey, using: transaction) try MessageSender.leave(groupPublicKey, using: transaction)
} else { } else {
try MessageSender.updateV2(groupPublicKey, with: members, name: name, transaction: transaction) try MessageSender.update(groupPublicKey, with: members, name: name, transaction: transaction)
} }
} catch { } catch {
DispatchQueue.main.async { DispatchQueue.main.async {

@ -170,9 +170,11 @@ final class NewClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelegat
ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in ModalActivityIndicatorViewController.present(fromViewController: navigationController!, canCancel: false) { [weak self] _ in
var promise: Promise<TSGroupThread>! var promise: Promise<TSGroupThread>!
Storage.writeSync { transaction in Storage.writeSync { transaction in
promise = MessageSender.createV2ClosedGroup(name: name, members: selectedContacts, transaction: transaction) promise = MessageSender.createClosedGroup(name: name, members: selectedContacts, transaction: transaction)
} }
let _ = promise.done(on: DispatchQueue.main) { thread in let _ = promise.done(on: DispatchQueue.main) { thread in
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.forceSyncConfigurationNowIfNeeded().retainUntilComplete() // FIXME: It's probably cleaner to do this inside createClosedGroup(...)
self?.presentingViewController?.dismiss(animated: true, completion: nil) self?.presentingViewController?.dismiss(animated: true, completion: nil)
SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false) SignalApp.shared().presentConversation(for: thread, action: .compose, animated: false)
} }

@ -420,7 +420,7 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIScrol
let groupID = thread.groupModel.groupId let groupID = thread.groupModel.groupId
let groupPublicKey = LKGroupUtilities.getDecodedGroupID(groupID) let groupPublicKey = LKGroupUtilities.getDecodedGroupID(groupID)
do { do {
try MessageSender.leaveV2(groupPublicKey, using: transaction) try MessageSender.leave(groupPublicKey, using: transaction)
} catch { } catch {
// TODO: Handle // TODO: Handle
} }

@ -5,7 +5,6 @@ extension AppDelegate {
@objc(syncConfigurationIfNeeded) @objc(syncConfigurationIfNeeded)
func syncConfigurationIfNeeded() { func syncConfigurationIfNeeded() {
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
guard userDefaults[.isUsingMultiDevice] else { return }
let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast let lastSync = userDefaults[.lastConfigurationSync] ?? .distantPast
guard Date().timeIntervalSince(lastSync) > 2 * 24 * 60 * 60 else { return } // Sync every 2 days guard Date().timeIntervalSince(lastSync) > 2 * 24 * 60 * 60 else { return } // Sync every 2 days
let configurationMessage = ConfigurationMessage.getCurrent() let configurationMessage = ConfigurationMessage.getCurrent()
@ -17,8 +16,6 @@ extension AppDelegate {
} }
func forceSyncConfigurationNowIfNeeded() -> Promise<Void> { func forceSyncConfigurationNowIfNeeded() -> Promise<Void> {
let userDefaults = UserDefaults.standard
guard userDefaults[.isUsingMultiDevice] else { return Promise.value(()) }
let configurationMessage = ConfigurationMessage.getCurrent() let configurationMessage = ConfigurationMessage.getCurrent()
let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey()) let destination = Message.Destination.contact(publicKey: getUserHexEncodedPublicKey())
let (promise, seal) = Promise<Void>.pending() let (promise, seal) = Promise<Void>.pending()

@ -135,6 +135,8 @@ final class JoinPublicChatVC : BaseVC, UIPageViewControllerDataSource, UIPageVie
Storage.shared.write { transaction in Storage.shared.write { transaction in
OpenGroupManager.shared.addOpenGroup(with: urlAsString, using: transaction) OpenGroupManager.shared.addOpenGroup(with: urlAsString, using: transaction)
.done(on: DispatchQueue.main) { [weak self] _ in .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) self?.presentingViewController!.dismiss(animated: true, completion: nil)
} }
.catch(on: DispatchQueue.main) { [weak self] error in .catch(on: DispatchQueue.main) { [weak self] error in

@ -3,7 +3,7 @@ import SessionProtocolKit
extension MessageSender { extension MessageSender {
public static func createV2ClosedGroup(name: String, members: Set<String>, transaction: YapDatabaseReadWriteTransaction) -> Promise<TSGroupThread> { public static func createClosedGroup(name: String, members: Set<String>, transaction: YapDatabaseReadWriteTransaction) -> Promise<TSGroupThread> {
// Prepare // Prepare
var members = members var members = members
let userPublicKey = getUserHexEncodedPublicKey() let userPublicKey = getUserHexEncodedPublicKey()
@ -46,7 +46,7 @@ extension MessageSender {
return when(fulfilled: promises).map2 { thread } return when(fulfilled: promises).map2 { thread }
} }
public static func updateV2(_ groupPublicKey: String, with members: Set<String>, name: String, transaction: YapDatabaseReadWriteTransaction) throws { public static func update(_ groupPublicKey: String, with members: Set<String>, name: String, transaction: YapDatabaseReadWriteTransaction) throws {
// Prepare // Prepare
let userPublicKey = getUserHexEncodedPublicKey() let userPublicKey = getUserHexEncodedPublicKey()
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
@ -117,7 +117,7 @@ extension MessageSender {
} }
@objc(leaveClosedGroupWithPublicKey:using:error:) @objc(leaveClosedGroupWithPublicKey:using:error:)
public static func leaveV2(_ groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws { public static func leave(_ groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
let threadID = TSGroupThread.threadId(fromGroupId: groupID) let threadID = TSGroupThread.threadId(fromGroupId: groupID)
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
@ -133,7 +133,7 @@ extension MessageSender {
} else { } else {
newMembers = [] // If the admin leaves the group is destroyed newMembers = [] // If the admin leaves the group is destroyed
} }
return try updateV2(groupPublicKey, with: newMembers, name: group.groupName!, transaction: transaction) return try update(groupPublicKey, with: newMembers, name: group.groupName!, transaction: transaction)
} }
public static func generateAndSendNewEncryptionKeyPair(for groupPublicKey: String, to targetMembers: Set<String>, using transaction: Any) throws { public static func generateAndSendNewEncryptionKeyPair(for groupPublicKey: String, to targetMembers: Set<String>, using transaction: Any) throws {

Loading…
Cancel
Save