From 30e00abb5cfbaac18fb7782f6fd448bf50e365b3 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 1 Jun 2018 20:28:51 +0300 Subject: [PATCH] no message --- TelegramCore/Account.swift | 1 + TelegramCore/AccountState.swift | 64 +++++++++++- TelegramCore/AccountStateManager.swift | 17 ++++ TelegramCore/Api0.swift | 8 +- TelegramCore/Api2.swift | 105 +++++++++++++++++--- TelegramCore/Api3.swift | 27 +++-- TelegramCore/Authorization.swift | 12 +-- TelegramCore/ChangeAccountPhoneNumber.swift | 4 +- TelegramCore/Serialization.swift | 17 +--- TelegramCore/TermsOfService.swift | 69 ++++++++++--- 10 files changed, 260 insertions(+), 64 deletions(-) diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index 911aa44bf5..94e669a2a4 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -799,6 +799,7 @@ public class Account { } })) self.managedOperationsDisposable.add(managedConfigurationUpdates(postbox: self.postbox, network: self.network).start()) + self.managedOperationsDisposable.add(managedTermsOfServiceUpdates(postbox: self.postbox, network: self.network, stateManager: self.stateManager).start()) self.managedOperationsDisposable.add(managedProxyInfoUpdates(postbox: self.postbox, network: self.network, viewTracker: self.viewTracker).start()) self.managedOperationsDisposable.add(managedLocalizationUpdatesOperations(postbox: self.postbox, network: self.network).start()) self.managedOperationsDisposable.add(managedPendingPeerNotificationSettings(postbox: self.postbox, network: self.network).start()) diff --git a/TelegramCore/AccountState.swift b/TelegramCore/AccountState.swift index 518799425e..5e3e98a6d1 100644 --- a/TelegramCore/AccountState.swift +++ b/TelegramCore/AccountState.swift @@ -96,10 +96,56 @@ private enum UnauthorizedAccountStateContentsValue: Int32 { case awaitingAccountReset = 7 } +public struct UnauthorizedAccountTermsOfService: PostboxCoding, Equatable { + public let id: String + public let text: String + public let entities: [MessageTextEntity] + public let ageConfirmation: Int32? + + init(id: String, text: String, entities: [MessageTextEntity], ageConfirmation: Int32?) { + self.id = id + self.text = text + self.entities = entities + self.ageConfirmation = ageConfirmation + } + + public init(decoder: PostboxDecoder) { + self.id = decoder.decodeStringForKey("id", orElse: "") + self.text = decoder.decodeStringForKey("text", orElse: "") + self.entities = (try? decoder.decodeObjectArrayWithCustomDecoderForKey("entities", decoder: { MessageTextEntity(decoder: $0) })) ?? [] + self.ageConfirmation = decoder.decodeOptionalInt32ForKey("ageConfirmation") + } + + public func encode(_ encoder: PostboxEncoder) { + encoder.encodeString(self.id, forKey: "id") + encoder.encodeString(self.text, forKey: "text") + encoder.encodeObjectArray(self.entities, forKey: "entities") + if let ageConfirmation = self.ageConfirmation { + encoder.encodeInt32(ageConfirmation, forKey: "ageConfirmation") + } else { + encoder.encodeNil(forKey: "ageConfirmation") + } + } +} + +extension UnauthorizedAccountTermsOfService { + init?(apiTermsOfService: Api.help.TermsOfService) { + switch apiTermsOfService { + case let .termsOfService(_, id, text, entities, minAgeConfirm): + let idData: String + switch id { + case let .dataJSON(data): + idData = data + } + self.init(id: idData, text: text, entities: messageTextEntitiesFromApiEntities(entities), ageConfirmation: minAgeConfirm) + } + } +} + public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { case empty case phoneEntry(countryCode: Int32, number: String) - case confirmationCodeEntry(number: String, type: SentAuthorizationCodeType, hash: String, timeout: Int32?, nextType: AuthorizationCodeNextType?) + case confirmationCodeEntry(number: String, type: SentAuthorizationCodeType, hash: String, timeout: Int32?, nextType: AuthorizationCodeNextType?, termsOfService: UnauthorizedAccountTermsOfService?) case passwordEntry(hint: String, number: String?, code: String?) case passwordRecovery(hint: String, number: String?, code: String?, emailPattern: String) case awaitingAccountReset(protectedUntil: Int32, number: String?) @@ -116,7 +162,7 @@ public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { if let value = decoder.decodeOptionalInt32ForKey("nt") { nextType = AuthorizationCodeNextType(rawValue: value) } - self = .confirmationCodeEntry(number: decoder.decodeStringForKey("num", orElse: ""), type: decoder.decodeObjectForKey("t", decoder: { SentAuthorizationCodeType(decoder: $0) }) as! SentAuthorizationCodeType, hash: decoder.decodeStringForKey("h", orElse: ""), timeout: decoder.decodeOptionalInt32ForKey("tm"), nextType: nextType) + self = .confirmationCodeEntry(number: decoder.decodeStringForKey("num", orElse: ""), type: decoder.decodeObjectForKey("t", decoder: { SentAuthorizationCodeType(decoder: $0) }) as! SentAuthorizationCodeType, hash: decoder.decodeStringForKey("h", orElse: ""), timeout: decoder.decodeOptionalInt32ForKey("tm"), nextType: nextType, termsOfService: decoder.decodeObjectForKey("tos", decoder: { UnauthorizedAccountTermsOfService(decoder: $0) }) as? UnauthorizedAccountTermsOfService) case UnauthorizedAccountStateContentsValue.passwordEntry.rawValue: self = .passwordEntry(hint: decoder.decodeStringForKey("h", orElse: ""), number: decoder.decodeOptionalStringForKey("n"), code: decoder.decodeOptionalStringForKey("c")) case UnauthorizedAccountStateContentsValue.passwordRecovery.rawValue: @@ -139,7 +185,7 @@ public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { encoder.encodeInt32(UnauthorizedAccountStateContentsValue.phoneEntry.rawValue, forKey: "v") encoder.encodeInt32(countryCode, forKey: "cc") encoder.encodeString(number, forKey: "n") - case let .confirmationCodeEntry(number, type, hash, timeout, nextType): + case let .confirmationCodeEntry(number, type, hash, timeout, nextType, termsOfService): encoder.encodeInt32(UnauthorizedAccountStateContentsValue.confirmationCodeEntry.rawValue, forKey: "v") encoder.encodeString(number, forKey: "num") encoder.encodeObject(type, forKey: "t") @@ -154,6 +200,11 @@ public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { } else { encoder.encodeNil(forKey: "nt") } + if let termsOfService = termsOfService { + encoder.encodeObject(termsOfService, forKey: "tos") + } else { + encoder.encodeNil(forKey: "tos") + } case let .passwordEntry(hint, number, code): encoder.encodeInt32(UnauthorizedAccountStateContentsValue.passwordEntry.rawValue, forKey: "v") encoder.encodeString(hint, forKey: "h") @@ -213,8 +264,8 @@ public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { } else { return false } - case let .confirmationCodeEntry(lhsNumber, lhsType, lhsHash, lhsTimeout, lhsNextType): - if case let .confirmationCodeEntry(rhsNumber, rhsType, rhsHash, rhsTimeout, rhsNextType) = rhs { + case let .confirmationCodeEntry(lhsNumber, lhsType, lhsHash, lhsTimeout, lhsNextType, lhsTermsOfService): + if case let .confirmationCodeEntry(rhsNumber, rhsType, rhsHash, rhsTimeout, rhsNextType, rhsTermsOfService) = rhs { if lhsNumber != rhsNumber { return false } @@ -230,6 +281,9 @@ public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { if lhsNextType != rhsNextType { return false } + if lhsTermsOfService != rhsTermsOfService { + return false + } return true } else { return false diff --git a/TelegramCore/AccountStateManager.swift b/TelegramCore/AccountStateManager.swift index 1217d13204..7c8680e83e 100644 --- a/TelegramCore/AccountStateManager.swift +++ b/TelegramCore/AccountStateManager.swift @@ -85,6 +85,12 @@ public final class AccountStateManager { return self.displayAlertsPipe.signal() } + private let termsOfServiceUpdateValue = Atomic(value: nil) + private let termsOfServiceUpdatePromise = Promise(nil) + public var termsOfServiceUpdate: Signal { + return self.termsOfServiceUpdatePromise.get() + } + private let appliedIncomingReadMessagesPipe = ValuePipe<[MessageId]>() public var appliedIncomingReadMessages: Signal<[MessageId], NoError> { return self.appliedIncomingReadMessagesPipe.signal() @@ -713,6 +719,17 @@ public final class AccountStateManager { public func getDelayNotificatonsUntil() -> Int32? { return self.delayNotificatonsUntil.with { $0 } } + + func modifyTermsOfServiceUpdate(_ f: @escaping (TermsOfServiceUpdate?) -> (TermsOfServiceUpdate?)) { + self.queue.async { + let current = self.termsOfServiceUpdateValue.with { $0 } + let updated = f(current) + if (current != updated) { + let _ = self.termsOfServiceUpdateValue.swap(updated) + self.termsOfServiceUpdatePromise.set(.single(updated)) + } + } + } } public func messageForNotification(modifier: Modifier, id: MessageId, alwaysReturnMessage: Bool) -> (message: Message?, notify: Bool, sound: PeerMessageSound, displayContents: Bool) { diff --git a/TelegramCore/Api0.swift b/TelegramCore/Api0.swift index d8f680926a..c94661113f 100644 --- a/TelegramCore/Api0.swift +++ b/TelegramCore/Api0.swift @@ -328,7 +328,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-886477832] = { return Api.LabeledPrice.parse_labeledPrice($0) } dict[-438840932] = { return Api.messages.ChatFull.parse_chatFull($0) } dict[-313079300] = { return Api.account.WebAuthorizations.parse_webAuthorizations($0) } - dict[-236044656] = { return Api.help.TermsOfService.parse_termsOfService($0) } + dict[2013922064] = { return Api.help.TermsOfService.parse_termsOfService($0) } dict[1490799288] = { return Api.ReportReason.parse_inputReportReasonSpam($0) } dict[505595789] = { return Api.ReportReason.parse_inputReportReasonViolence($0) } dict[777640226] = { return Api.ReportReason.parse_inputReportReasonPornography($0) } @@ -388,7 +388,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[1951620897] = { return Api.messages.Messages.parse_messagesNotModified($0) } dict[-1022713000] = { return Api.Invoice.parse_invoice($0) } dict[-2122045747] = { return Api.PeerSettings.parse_peerSettings($0) } - dict[1577067778] = { return Api.auth.SentCode.parse_sentCode($0) } + dict[955951967] = { return Api.auth.SentCode.parse_sentCode($0) } dict[480546647] = { return Api.InputChatPhoto.parse_inputChatPhotoEmpty($0) } dict[-1837345356] = { return Api.InputChatPhoto.parse_inputChatUploadedPhoto($0) } dict[-1991004873] = { return Api.InputChatPhoto.parse_inputChatPhoto($0) } @@ -562,6 +562,8 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[1828732223] = { return Api.PhoneCall.parse_phoneCallAccepted($0) } dict[-1660057] = { return Api.PhoneCall.parse_phoneCall($0) } dict[1355435489] = { return Api.PhoneCall.parse_phoneCallDiscarded($0) } + dict[-483352705] = { return Api.help.TermsOfServiceUpdate.parse_termsOfServiceUpdateEmpty($0) } + dict[686618977] = { return Api.help.TermsOfServiceUpdate.parse_termsOfServiceUpdate($0) } dict[-445792507] = { return Api.DialogPeer.parse_dialogPeer($0) } dict[1599050311] = { return Api.ContactLink.parse_contactLinkUnknown($0) } dict[-17968211] = { return Api.ContactLink.parse_contactLinkNone($0) } @@ -1047,6 +1049,8 @@ public struct Api { _1.serialize(buffer, boxed) case let _1 as Api.PhoneCall: _1.serialize(buffer, boxed) + case let _1 as Api.help.TermsOfServiceUpdate: + _1.serialize(buffer, boxed) case let _1 as Api.DialogPeer: _1.serialize(buffer, boxed) case let _1 as Api.ContactLink: diff --git a/TelegramCore/Api2.swift b/TelegramCore/Api2.swift index 809ccb354c..4c795d069b 100644 --- a/TelegramCore/Api2.swift +++ b/TelegramCore/Api2.swift @@ -559,19 +559,20 @@ public struct auth { } public enum SentCode { - case sentCode(flags: Int32, type: Api.auth.SentCodeType, phoneCodeHash: String, nextType: Api.auth.CodeType?, timeout: Int32?) + case sentCode(flags: Int32, type: Api.auth.SentCodeType, phoneCodeHash: String, nextType: Api.auth.CodeType?, timeout: Int32?, termsOfService: Api.help.TermsOfService?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .sentCode(let flags, let type, let phoneCodeHash, let nextType, let timeout): + case .sentCode(let flags, let type, let phoneCodeHash, let nextType, let timeout, let termsOfService): if boxed { - buffer.appendInt32(1577067778) + buffer.appendInt32(955951967) } serializeInt32(flags, buffer: buffer, boxed: false) type.serialize(buffer, true) serializeString(phoneCodeHash, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 1) != 0 {nextType!.serialize(buffer, true)} if Int(flags) & Int(1 << 2) != 0 {serializeInt32(timeout!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 3) != 0 {termsOfService!.serialize(buffer, true)} break } } @@ -591,13 +592,18 @@ public struct auth { } } var _5: Int32? if Int(_1!) & Int(1 << 2) != 0 {_5 = reader.readInt32() } + var _6: Api.help.TermsOfService? + if Int(_1!) & Int(1 << 3) != 0 {if let signature = reader.readInt32() { + _6 = Api.parse(reader, signature: signature) as? Api.help.TermsOfService + } } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = _3 != nil let _c4 = (Int(_1!) & Int(1 << 1) == 0) || _4 != nil let _c5 = (Int(_1!) & Int(1 << 2) == 0) || _5 != nil - if _c1 && _c2 && _c3 && _c4 && _c5 { - return Api.auth.SentCode.sentCode(flags: _1!, type: _2!, phoneCodeHash: _3!, nextType: _4, timeout: _5) + let _c6 = (Int(_1!) & Int(1 << 3) == 0) || _6 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 { + return Api.auth.SentCode.sentCode(flags: _1!, type: _2!, phoneCodeHash: _3!, nextType: _4, timeout: _5, termsOfService: _6) } else { return nil @@ -1273,25 +1279,49 @@ public struct help { } public enum TermsOfService { - case termsOfService(text: String) + case termsOfService(flags: Int32, id: Api.DataJSON, text: String, entities: [Api.MessageEntity], minAgeConfirm: Int32?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .termsOfService(let text): + case .termsOfService(let flags, let id, let text, let entities, let minAgeConfirm): if boxed { - buffer.appendInt32(-236044656) + buffer.appendInt32(2013922064) } + serializeInt32(flags, buffer: buffer, boxed: false) + id.serialize(buffer, true) serializeString(text, buffer: buffer, boxed: false) + buffer.appendInt32(481674261) + buffer.appendInt32(Int32(entities.count)) + for item in entities { + item.serialize(buffer, true) + } + if Int(flags) & Int(1 << 1) != 0 {serializeInt32(minAgeConfirm!, buffer: buffer, boxed: false)} break } } static func parse_termsOfService(_ reader: BufferReader) -> TermsOfService? { - var _1: String? - _1 = parseString(reader) + var _1: Int32? + _1 = reader.readInt32() + var _2: Api.DataJSON? + if let signature = reader.readInt32() { + _2 = Api.parse(reader, signature: signature) as? Api.DataJSON + } + var _3: String? + _3 = parseString(reader) + var _4: [Api.MessageEntity]? + if let _ = reader.readInt32() { + _4 = Api.parseVector(reader, elementSignature: 0, elementType: Api.MessageEntity.self) + } + var _5: Int32? + if Int(_1!) & Int(1 << 1) != 0 {_5 = reader.readInt32() } let _c1 = _1 != nil - if _c1 { - return Api.help.TermsOfService.termsOfService(text: _1!) + let _c2 = _2 != nil + let _c3 = _3 != nil + let _c4 = _4 != nil + let _c5 = (Int(_1!) & Int(1 << 1) == 0) || _5 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 { + return Api.help.TermsOfService.termsOfService(flags: _1!, id: _2!, text: _3!, entities: _4!, minAgeConfirm: _5) } else { return nil @@ -1412,6 +1442,57 @@ public struct help { } } + public enum TermsOfServiceUpdate { + case termsOfServiceUpdateEmpty(expires: Int32) + case termsOfServiceUpdate(expires: Int32, termsOfService: Api.help.TermsOfService) + + public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { + switch self { + case .termsOfServiceUpdateEmpty(let expires): + if boxed { + buffer.appendInt32(-483352705) + } + serializeInt32(expires, buffer: buffer, boxed: false) + break + case .termsOfServiceUpdate(let expires, let termsOfService): + if boxed { + buffer.appendInt32(686618977) + } + serializeInt32(expires, buffer: buffer, boxed: false) + termsOfService.serialize(buffer, true) + break + } + } + + static func parse_termsOfServiceUpdateEmpty(_ reader: BufferReader) -> TermsOfServiceUpdate? { + var _1: Int32? + _1 = reader.readInt32() + let _c1 = _1 != nil + if _c1 { + return Api.help.TermsOfServiceUpdate.termsOfServiceUpdateEmpty(expires: _1!) + } + else { + return nil + } + } + static func parse_termsOfServiceUpdate(_ reader: BufferReader) -> TermsOfServiceUpdate? { + var _1: Int32? + _1 = reader.readInt32() + var _2: Api.help.TermsOfService? + if let signature = reader.readInt32() { + _2 = Api.parse(reader, signature: signature) as? Api.help.TermsOfService + } + let _c1 = _1 != nil + let _c2 = _2 != nil + if _c1 && _c2 { + return Api.help.TermsOfServiceUpdate.termsOfServiceUpdate(expires: _1!, termsOfService: _2!) + } + else { + return nil + } + } + + } } } public extension Api { diff --git a/TelegramCore/Api3.swift b/TelegramCore/Api3.swift index 082a424d50..02375a6d7c 100644 --- a/TelegramCore/Api3.swift +++ b/TelegramCore/Api3.swift @@ -3176,16 +3176,29 @@ public extension Api { }) } - public static func getTermsOfService(countryIso2: String, langCode: String) -> (CustomStringConvertible, Buffer, DeserializeFunctionResponse) { + public static func getTermsOfServiceUpdate() -> (CustomStringConvertible, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() - buffer.appendInt32(-92706236) - serializeString(countryIso2, buffer: buffer, boxed: false) - serializeString(langCode, buffer: buffer, boxed: false) - return (FunctionDescription({return "(help.getTermsOfService countryIso2: \(countryIso2), langCode: \(langCode))"}), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.help.TermsOfService? in + buffer.appendInt32(749019089) + + return (FunctionDescription({return "(help.getTermsOfServiceUpdate )"}), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.help.TermsOfServiceUpdate? in let reader = BufferReader(buffer) - var result: Api.help.TermsOfService? + var result: Api.help.TermsOfServiceUpdate? if let signature = reader.readInt32() { - result = Api.parse(reader, signature: signature) as? Api.help.TermsOfService + result = Api.parse(reader, signature: signature) as? Api.help.TermsOfServiceUpdate + } + return result + }) + } + + public static func acceptTermsOfService(id: Api.DataJSON) -> (CustomStringConvertible, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(-294455398) + id.serialize(buffer, true) + return (FunctionDescription({return "(help.acceptTermsOfService id: \(id))"}), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Bool? in + let reader = BufferReader(buffer) + var result: Api.Bool? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.Bool } return result }) diff --git a/TelegramCore/Authorization.swift b/TelegramCore/Authorization.swift index 962ff6e8d4..40994b5658 100644 --- a/TelegramCore/Authorization.swift +++ b/TelegramCore/Authorization.swift @@ -58,13 +58,13 @@ public func sendAuthorizationCode(account: UnauthorizedAccount, phoneNumber: Str |> mapToSignal { (sentCode, account) -> Signal in return account.postbox.modify { modifier -> UnauthorizedAccount in switch sentCode { - case let .sentCode(_, type, phoneCodeHash, nextType, timeout): + case let .sentCode(_, type, phoneCodeHash, nextType, timeout, termsOfService): var parsedNextType: AuthorizationCodeNextType? if let nextType = nextType { parsedNextType = AuthorizationCodeNextType(apiType: nextType) } - modifier.setState(UnauthorizedAccountState(masterDatacenterId: account.masterDatacenterId, contents: .confirmationCodeEntry(number: phoneNumber, type: SentAuthorizationCodeType(apiType: type), hash: phoneCodeHash, timeout: timeout, nextType: parsedNextType))) + modifier.setState(UnauthorizedAccountState(masterDatacenterId: account.masterDatacenterId, contents: .confirmationCodeEntry(number: phoneNumber, type: SentAuthorizationCodeType(apiType: type), hash: phoneCodeHash, timeout: timeout, nextType: parsedNextType, termsOfService: termsOfService.flatMap(UnauthorizedAccountTermsOfService.init(apiTermsOfService:))))) } return account } |> mapError { _ -> AuthorizationCodeRequestError in return .generic } @@ -75,7 +75,7 @@ public func resendAuthorizationCode(account: UnauthorizedAccount) -> Signal Signal in if let state = modifier.getState() as? UnauthorizedAccountState { switch state.contents { - case let .confirmationCodeEntry(number, _, hash, _, nextType): + case let .confirmationCodeEntry(number, _, hash, _, nextType, _): if nextType != nil { return account.network.request(Api.functions.auth.resendCode(phoneNumber: number, phoneCodeHash: hash), automaticFloodWait: false) |> mapError { error -> AuthorizationCodeRequestError in @@ -94,14 +94,14 @@ public func resendAuthorizationCode(account: UnauthorizedAccount) -> Signal mapToSignal { sentCode -> Signal in return account.postbox.modify { modifier -> Void in switch sentCode { - case let .sentCode(_, type, phoneCodeHash, nextType, timeout): + case let .sentCode(_, type, phoneCodeHash, nextType, timeout, termsOfService): var parsedNextType: AuthorizationCodeNextType? if let nextType = nextType { parsedNextType = AuthorizationCodeNextType(apiType: nextType) } - modifier.setState(UnauthorizedAccountState(masterDatacenterId: account.masterDatacenterId, contents: .confirmationCodeEntry(number: number, type: SentAuthorizationCodeType(apiType: type), hash: phoneCodeHash, timeout: timeout, nextType: parsedNextType))) + modifier.setState(UnauthorizedAccountState(masterDatacenterId: account.masterDatacenterId, contents: .confirmationCodeEntry(number: number, type: SentAuthorizationCodeType(apiType: type), hash: phoneCodeHash, timeout: timeout, nextType: parsedNextType, termsOfService: termsOfService.flatMap(UnauthorizedAccountTermsOfService.init(apiTermsOfService:))))) } } |> mapError { _ -> AuthorizationCodeRequestError in return .generic } @@ -138,7 +138,7 @@ public func authorizeWithCode(account: UnauthorizedAccount, code: String) -> Sig return account.postbox.modify { modifier -> Signal in if let state = modifier.getState() as? UnauthorizedAccountState { switch state.contents { - case let .confirmationCodeEntry(number, _, hash, _, _): + case let .confirmationCodeEntry(number, _, hash, _, _, _): return account.network.request(Api.functions.auth.signIn(phoneNumber: number, phoneCodeHash: hash, phoneCode: code), automaticFloodWait: false) |> map { authorization in return .authorization(authorization) } |> `catch` { error -> Signal in diff --git a/TelegramCore/ChangeAccountPhoneNumber.swift b/TelegramCore/ChangeAccountPhoneNumber.swift index e1a3a9c00c..cfdb2eabba 100644 --- a/TelegramCore/ChangeAccountPhoneNumber.swift +++ b/TelegramCore/ChangeAccountPhoneNumber.swift @@ -54,7 +54,7 @@ public func requestChangeAccountPhoneNumberVerification(account: Account, phoneN } |> map { sentCode -> ChangeAccountPhoneNumberData in switch sentCode { - case let .sentCode(_, type, phoneCodeHash, nextType, timeout): + case let .sentCode(_, type, phoneCodeHash, nextType, timeout, _): var parsedNextType: AuthorizationCodeNextType? if let nextType = nextType { parsedNextType = AuthorizationCodeNextType(apiType: nextType) @@ -79,7 +79,7 @@ public func requestNextChangeAccountPhoneNumberVerification(account: Account, ph } |> map { sentCode -> ChangeAccountPhoneNumberData in switch sentCode { - case let .sentCode(_, type, phoneCodeHash, nextType, timeout): + case let .sentCode(_, type, phoneCodeHash, nextType, timeout, _): var parsedNextType: AuthorizationCodeNextType? if let nextType = nextType { parsedNextType = AuthorizationCodeNextType(apiType: nextType) diff --git a/TelegramCore/Serialization.swift b/TelegramCore/Serialization.swift index 9d55a40832..c3ec7ce807 100644 --- a/TelegramCore/Serialization.swift +++ b/TelegramCore/Serialization.swift @@ -93,7 +93,7 @@ public class BoxedMessage: NSObject { public class Serialization: NSObject, MTSerialization { public func currentLayer() -> UInt { - return 79 + return 80 } public func parseMessage(_ data: Data!) -> Any! { @@ -153,21 +153,6 @@ public class Serialization: NSObject, MTSerialization { } } - public func requestDatacenterVerificationData(_ data: AutoreleasingUnsafeMutablePointer) -> MTDatacenterVerificationDataParser! { - let (_, buffer, parser) = Api.functions.help.getConfig() - data.pointee = buffer.makeData() as NSData - return { response -> MTDatacenterVerificationData? in - if let config = parser.parse(Buffer(data: response)) { - switch config { - case let .config(config): - return MTDatacenterVerificationData(datacenterId: Int(config.thisDc), isTestingEnvironment: config.testMode == .boolTrue) - } - - } - return nil - } - } - public func requestNoop(_ data: AutoreleasingUnsafeMutablePointer!) -> MTRequestNoopParser! { let (_, buffer, parser) = Api.functions.help.test() data.pointee = buffer.makeData() as NSData diff --git a/TelegramCore/TermsOfService.swift b/TelegramCore/TermsOfService.swift index 5b30140f5c..eb02dea8ba 100644 --- a/TelegramCore/TermsOfService.swift +++ b/TelegramCore/TermsOfService.swift @@ -9,21 +9,62 @@ import SwiftSignalKit import MtProtoKitDynamic #endif -public func requestTermsOfService(network: Network, countryCode: String) -> Signal { - let langCode: String - if let langPackCode = network.context.apiEnvironment.langPackCode, !langPackCode.isEmpty { - langCode = langPackCode - } else { - langCode = "en" +public struct TermsOfServiceUpdate: Equatable { + public let id: String + public let text: String + public let entities: [MessageTextEntity] + public let ageConfirmation: Int32? + + init(id: String, text: String, entities: [MessageTextEntity], ageConfirmation: Int32?) { + self.id = id + self.text = text + self.entities = entities + self.ageConfirmation = ageConfirmation } - return network.request(Api.functions.help.getTermsOfService(countryIso2: countryCode, langCode: langCode)) - |> `catch` { _ -> Signal in - return .single(.termsOfService(text: "")) - } - |> map { result -> String in - switch result { - case let .termsOfService(text): - return text +} + +extension TermsOfServiceUpdate { + init?(apiTermsOfService: Api.help.TermsOfService) { + switch apiTermsOfService { + case let .termsOfService(_, id, text, entities, minAgeConfirm): + let idData: String + switch id { + case let .dataJSON(data): + idData = data + } + self.init(id: idData, text: text, entities: messageTextEntitiesFromApiEntities(entities), ageConfirmation: minAgeConfirm) } } } + +public func acceptTermsOfService(account: Account, id: String) -> Signal { + return account.network.request(Api.functions.help.acceptTermsOfService(id: .dataJSON(data: id))) + |> `catch` { _ -> Signal in + return .complete() + } + |> mapToSignal { [weak account] _ -> Signal in + account?.stateManager.modifyTermsOfServiceUpdate({ _ in nil }) + return .complete() + } +} + +func managedTermsOfServiceUpdates(postbox: Postbox, network: Network, stateManager: AccountStateManager) -> Signal { + let poll = network.request(Api.functions.help.getTermsOfServiceUpdate()) + |> retryRequest + |> mapToSignal { [weak stateManager] result -> Signal in + var updated: TermsOfServiceUpdate? + switch result { + case let .termsOfServiceUpdate(_, termsOfService): + updated = TermsOfServiceUpdate(apiTermsOfService: termsOfService) + case let .termsOfServiceUpdateEmpty(expires): + break + } + stateManager?.modifyTermsOfServiceUpdate { _ in + return updated + } + return .complete() + } + + return (poll |> then(.complete() |> delay(1.0 * 60.0 * 60.0, queue: Queue.concurrentDefaultQueue()))) |> restart +} +