Swiftgram/submodules/TelegramCore/TelegramCore/SecretChatLayerNegotiation.swift
Peter 5c1613d104 Add 'submodules/TelegramCore/' from commit '9561227540acef69894e6546395ab223a6233600'
git-subtree-dir: submodules/TelegramCore
git-subtree-mainline: 971273e8f8f49a47f14b251d2f35e3445a61fc3f
git-subtree-split: 9561227540acef69894e6546395ab223a6233600
2019-06-11 18:59:08 +01:00

64 lines
3.0 KiB
Swift

import Foundation
#if os(macOS)
import PostboxMac
import SwiftSignalKitMac
#else
import Postbox
import SwiftSignalKit
#endif
private let topSupportedLayer: SecretChatSequenceBasedLayer = .layer73
func secretChatCommonSupportedLayer(remoteLayer: Int32) -> SecretChatSequenceBasedLayer {
switch remoteLayer {
case 46:
return .layer46
case 73:
return .layer73
default:
return topSupportedLayer
}
}
func secretChatAddReportCurrentLayerSupportOperationAndUpdateRequestedLayer(transaction: Transaction, peerId: PeerId, state: SecretChatState) -> SecretChatState {
switch state.embeddedState {
case .basicLayer:
var updatedState = state
updatedState = addSecretChatOutgoingOperation(transaction: transaction, peerId: peerId, operation: .reportLayerSupport(layer: .layer8, actionGloballyUniqueId: arc4random64(), layerSupport: topSupportedLayer.rawValue), state: updatedState)
return updatedState
case let .sequenceBasedLayer(sequenceState):
var updatedState = state
updatedState = addSecretChatOutgoingOperation(transaction: transaction, peerId: peerId, operation: .reportLayerSupport(layer: sequenceState.layerNegotiationState.activeLayer.secretChatLayer, actionGloballyUniqueId: arc4random64(), layerSupport: topSupportedLayer.rawValue), state: updatedState)
updatedState = updatedState.withUpdatedEmbeddedState(.sequenceBasedLayer(sequenceState.withUpdatedLayerNegotiationState(sequenceState.layerNegotiationState.withUpdatedLocallyRequestedLayer(topSupportedLayer.rawValue))))
return updatedState
default:
return state
}
}
func secretChatCheckLayerNegotiationIfNeeded(transaction: Transaction, peerId: PeerId, state: SecretChatState) -> SecretChatState {
switch state.embeddedState {
case let .sequenceBasedLayer(sequenceState):
if sequenceState.layerNegotiationState.activeLayer != topSupportedLayer {
var updatedState = state
if let remotelyRequestedLayer = sequenceState.layerNegotiationState.remotelyRequestedLayer {
let updatedSequenceState = sequenceState.withUpdatedLayerNegotiationState(sequenceState.layerNegotiationState.withUpdatedActiveLayer(secretChatCommonSupportedLayer(remoteLayer: remotelyRequestedLayer)))
updatedState = updatedState.withUpdatedEmbeddedState(.sequenceBasedLayer(updatedSequenceState))
}
if (sequenceState.layerNegotiationState.locallyRequestedLayer ?? 0) < topSupportedLayer.rawValue {
updatedState = secretChatAddReportCurrentLayerSupportOperationAndUpdateRequestedLayer(transaction: transaction, peerId: peerId, state: updatedState)
}
return updatedState
} else {
return state
}
case .basicLayer:
return state
default:
return state
}
}