Merge pull request #593 from mpretty-cyro/fix/duplicate-open-groups-from-url-differences

Fix duplicate open groups from url differences
This commit is contained in:
RyanZhao 2022-03-21 14:16:40 +11:00 committed by GitHub
commit c7bf20dd85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 3 deletions

View File

@ -18,6 +18,7 @@ public final class OpenGroupAPIV2 : NSObject {
}()
// MARK: Settings
public static let legacyDefaultServerDNS = "open.getsession.org"
public static let defaultServer = "http://116.203.70.33"
public static let defaultServerPublicKey = "a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f488aaa93da7991238"

View File

@ -29,12 +29,53 @@ public final class OpenGroupManagerV2 : NSObject {
}
// MARK: Adding & Removing
public func hasExistingOpenGroup(room: String, server: String, publicKey: String, using transaction: YapDatabaseReadWriteTransaction) -> Bool {
let schemeFreeServer: String = (server.starts(with: "https://") ? server.substring(from: "https://".count) : server.substring(from: "http://".count))
let schemeFreeDefaultServer: String = OpenGroupAPIV2.defaultServer.substring(from: "http://".count)
var serverOptions: Set<String> = Set([
schemeFreeServer,
"http://\(schemeFreeServer)",
"https://\(schemeFreeServer)"
])
if schemeFreeServer == OpenGroupAPIV2.legacyDefaultServerDNS {
let defaultServerOptions: Set<String> = Set([
schemeFreeDefaultServer,
OpenGroupAPIV2.defaultServer,
"https://\(schemeFreeDefaultServer)"
])
serverOptions = serverOptions.union(defaultServerOptions)
}
else if schemeFreeServer == schemeFreeDefaultServer {
let legacyServerOptions: Set<String> = Set([
OpenGroupAPIV2.legacyDefaultServerDNS,
"http://\(OpenGroupAPIV2.legacyDefaultServerDNS)",
"https://\(OpenGroupAPIV2.legacyDefaultServerDNS)"
])
serverOptions = serverOptions.union(legacyServerOptions)
}
// First check if there is no poller for the specified server
if serverOptions.first(where: { OpenGroupManagerV2.shared.pollers[$0] != nil }) == nil {
return false
}
// Then check if there is an existing open group thread
let hasExistingThread: Bool = serverOptions.contains(where: { serverName in
let groupId: Data = LKGroupUtilities.getEncodedOpenGroupIDAsData("\(serverName).\(room)")
return (TSGroupThread.fetch(groupId: groupId, transaction: transaction) != nil)
})
return hasExistingThread
}
public func add(room: String, server: String, publicKey: String, using transaction: Any) -> Promise<Void> {
// If we are currently polling for this server and already have a TSGroupThread for this room the do nothing
let transaction = transaction as! YapDatabaseReadWriteTransaction
let groupId: Data = LKGroupUtilities.getEncodedOpenGroupIDAsData("\(server).\(room)")
if OpenGroupManagerV2.shared.pollers[server] != nil && TSGroupThread.fetch(groupId: groupId, transaction: transaction) != nil {
if hasExistingOpenGroup(room: room, server: server, publicKey: publicKey, using: transaction) {
SNLog("Ignoring join open group attempt (already joined)")
return Promise.value(())
}