@ -108,12 +108,57 @@ public final class OpenGroupManager: NSObject {
// MARK: - A d d i n g & R e m o v i n g
private static func port ( for server : String , serverUrl : URL ) -> String {
if let port : Int = serverUrl . port {
return " : \( port ) "
}
let components : [ String ] = server . components ( separatedBy : " : " )
guard
let port : String = components . last ,
(
port != components . first &&
! port . starts ( with : " // " )
)
else { return " " }
return " : \( port ) "
}
public static func isSessionRunOpenGroup ( server : String ) -> Bool {
guard let serverUrl : URL = URL ( string : server . lowercased ( ) ) else { return false }
let serverPort : String = OpenGroupManager . port ( for : server , serverUrl : serverUrl )
let serverHost : String = serverUrl . host
. defaulting (
to : server
. lowercased ( )
. replacingOccurrences ( of : serverPort , with : " " )
)
let options : Set < String > = Set ( [
OpenGroupAPI . legacyDefaultServerIP ,
OpenGroupAPI . defaultServer
. replacingOccurrences ( of : " http:// " , with : " " )
. replacingOccurrences ( of : " https:// " , with : " " )
] )
return options . contains ( serverHost )
}
public func hasExistingOpenGroup ( _ db : Database , roomToken : String , server : String , publicKey : String , dependencies : OGMDependencies = OGMDependencies ( ) ) -> Bool {
guard let serverUrl : URL = URL ( string : server . lowercased ( ) ) else { return false }
let serverHost : String = ( serverUrl . host ? ? server . lowercased ( ) )
let serverPort : String = ( serverUrl . port . map { " : \( $0 ) " } ? ? " " )
let defaultServerHost : String = OpenGroupAPI . defaultServer . substring ( from : " https:// " . count )
let serverPort : String = OpenGroupManager . port ( for : server , serverUrl : serverUrl )
let serverHost : String = serverUrl . host
. defaulting (
to : server
. lowercased ( )
. replacingOccurrences ( of : serverPort , with : " " )
)
let defaultServerHost : String = OpenGroupAPI . defaultServer
. replacingOccurrences ( of : " http:// " , with : " " )
. replacingOccurrences ( of : " https:// " , with : " " )
var serverOptions : Set < String > = Set ( [
server . lowercased ( ) ,
" \( serverHost ) \( serverPort ) " ,
@ -121,12 +166,12 @@ public final class OpenGroupManager: NSObject {
" https:// \( serverHost ) \( serverPort ) "
] )
if serverHost = = OpenGroupAPI . legacyDefaultServerIP {
// I f t h e s e r v e r i s r u n b y S e s s i o n t h e n i n c l u d e a l l c o n f i g u r a t i o n s i n c a s e o n e o f t h e a l t e r n a t e c o n f i g u r a t i o n s
// w a s u s e d
if OpenGroupManager . isSessionRunOpenGroup ( server : server ) {
serverOptions . insert ( defaultServerHost )
serverOptions . insert ( " http:// \( defaultServerHost ) " )
serverOptions . insert ( OpenGroupAPI . defaultServer )
}
else if serverHost = = defaultServerHost {
serverOptions . insert ( " https:// \( defaultServerHost ) " )
serverOptions . insert ( OpenGroupAPI . legacyDefaultServerIP )
serverOptions . insert ( " http:// \( OpenGroupAPI . legacyDefaultServerIP ) " )
serverOptions . insert ( " https:// \( OpenGroupAPI . legacyDefaultServerIP ) " )
@ -158,7 +203,14 @@ public final class OpenGroupManager: NSObject {
}
// S t o r e t h e o p e n g r o u p i n f o r m a t i o n
let threadId : String = OpenGroup . idFor ( roomToken : roomToken , server : server )
let targetServer : String = {
guard OpenGroupManager . isSessionRunOpenGroup ( server : server ) else {
return server . lowercased ( )
}
return OpenGroupAPI . defaultServer
} ( )
let threadId : String = OpenGroup . idFor ( roomToken : roomToken , server : targetServer )
// O p t i o n a l l y t r y t o i n s e r t a n e w v e r s i o n o f t h e O p e n G r o u p ( i t w i l l f a i l i f t h e r e i s a l r e a d y a n
// i n a c t i v e o n e b u t t h a t w o n ' t m a t t e r a s w e t h e n a c t i v a t e i t
@ -167,14 +219,14 @@ public final class OpenGroupManager: NSObject {
if ( try ? OpenGroup . exists ( db , id : threadId ) ) = = false {
try ? OpenGroup
. fetchOrCreate ( db , server : s erver, roomToken : roomToken , publicKey : publicKey )
. fetchOrCreate ( db , server : targetS erver, roomToken : roomToken , publicKey : publicKey )
. save ( db )
}
// S e t t h e g r o u p t o a c t i v e a n d r e s e t t h e s e q u e n c e N u m b e r ( h a n d l e g r o u p s w h i c h h a v e
// b e e n d e a c t i v a t e d )
_ = try ? OpenGroup
. filter ( id : OpenGroup . idFor ( roomToken : roomToken , server : s erver) )
. filter ( id : OpenGroup . idFor ( roomToken : roomToken , server : targetS erver) )
. updateAll (
db ,
OpenGroup . Columns . isActive . set ( to : true ) ,
@ -195,7 +247,7 @@ public final class OpenGroupManager: NSObject {
. capabilitiesAndRoom (
db ,
for : roomToken ,
on : s erver,
on : targetS erver,
authenticated : false ,
using : dependencies
)
@ -206,7 +258,7 @@ public final class OpenGroupManager: NSObject {
OpenGroupManager . handleCapabilities (
db ,
capabilities : response . capabilities . data ,
on : s erver
on : targetS erver
)
// T h e n t h e r o o m
@ -215,7 +267,7 @@ public final class OpenGroupManager: NSObject {
pollInfo : OpenGroupAPI . RoomPollInfo ( room : response . room . data ) ,
publicKey : publicKey ,
for : roomToken ,
on : s erver,
on : targetS erver,
dependencies : dependencies
) {
seal . fulfill ( ( ) )