Prevent the user from joining duplicate open groups due to url differences

Updated the OpenGroupManagerV2 to handle HTTP scheme variants and official host variants when checking if the user is already in an open group
This commit is contained in:
Morgan Pretty 2022-03-21 13:52:18 +11:00
parent d3e9fff893
commit d3b54b84d0
2 changed files with 43 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,51 @@ 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 an existing poller for the given server options
guard serverOptions.first(where: { OpenGroupManagerV2.shared.pollers[$0] != nil }) == nil else { return true }
// 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(())
}