From 59411a3c2e4ac1628e97dfcdcb2b60faa3dc9f4a Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 22 Dec 2016 03:20:19 +0300 Subject: [PATCH] no message --- TelegramCore/Account.swift | 54 +++++++++++++------ TelegramCore/AccountViewTracker.swift | 6 +-- TelegramCore/Download.swift | 2 +- TelegramCore/MessageUtils.swift | 34 ++++++++++++ TelegramCore/Network.swift | 4 +- .../ReplyMarkupMessageAttribute.swift | 5 ++ TelegramCore/StoreMessage_Telegram.swift | 13 +++-- 7 files changed, 91 insertions(+), 27 deletions(-) diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index ada0d09455..80be92e4c4 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -172,6 +172,7 @@ public func generateAccountId() -> AccountId { public class UnauthorizedAccount { public let id: AccountId + public let basePath: String public let postbox: Postbox public let network: Network @@ -179,8 +180,9 @@ public class UnauthorizedAccount { return Int32(self.network.mtProto.datacenterId) } - init(id: AccountId, postbox: Postbox, network: Network) { + init(id: AccountId, basePath: String, postbox: Postbox, network: Network) { self.id = id + self.basePath = basePath self.postbox = postbox self.network = network } @@ -198,9 +200,9 @@ public class UnauthorizedAccount { postbox.removeKeychainEntryForKey(key) }) - return initializedNetwork(datacenterId: Int(masterDatacenterId), keychain: keychain) + return initializedNetwork(datacenterId: Int(masterDatacenterId), keychain: keychain, networkUsageInfoPath: accountNetworkUsageInfoPath(basePath: self.basePath)) |> map { network in - return UnauthorizedAccount(id: self.id, postbox: self.postbox, network: network) + return UnauthorizedAccount(id: self.id, basePath: self.basePath, postbox: self.postbox, network: network) } } } @@ -249,8 +251,12 @@ private var declaredEncodables: Void = { return }() +func accountNetworkUsageInfoPath(basePath: String) -> String { + return basePath + "/network-usage" +} + public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal, NoError> { - return Signal<(Postbox, AccountState?), NoError> { subscriber in + return Signal<(String, Postbox, AccountState?), NoError> { subscriber in let _ = declaredEncodables let path = "\(appGroupPath)/account\(id.stringValue)" @@ -259,12 +265,12 @@ public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal take(1) |> map { accountState in - return (postbox, accountState as? AccountState) - }).start(next: { pair in - subscriber.putNext(pair) + return (path, postbox, accountState as? AccountState) + }).start(next: { args in + subscriber.putNext(args) subscriber.putCompletion() }) - } |> mapToSignal { (postbox, accountState) -> Signal, NoError> in + } |> mapToSignal { (basePath, postbox, accountState) -> Signal, NoError> in let keychain = Keychain(get: { key in return postbox.keychainEntryForKey(key) }, set: { (key, data) in @@ -276,23 +282,23 @@ public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal map { network -> Either in - .left(value: UnauthorizedAccount(id: id, postbox: postbox, network: network)) + .left(value: UnauthorizedAccount(id: id, basePath: basePath, postbox: postbox, network: network)) } case let authorizedState as AuthorizedAccountState: - return initializedNetwork(datacenterId: Int(authorizedState.masterDatacenterId), keychain: keychain) + return initializedNetwork(datacenterId: Int(authorizedState.masterDatacenterId), keychain: keychain, networkUsageInfoPath: accountNetworkUsageInfoPath(basePath: basePath)) |> map { network -> Either in - return .right(value: Account(id: id, postbox: postbox, network: network, peerId: authorizedState.peerId)) + return .right(value: Account(id: id, basePath: basePath, postbox: postbox, network: network, peerId: authorizedState.peerId)) } case _: assertionFailure("Unexpected accountState \(accountState)") } } - return initializedNetwork(datacenterId: 2, keychain: keychain) + return initializedNetwork(datacenterId: 2, keychain: keychain, networkUsageInfoPath: accountNetworkUsageInfoPath(basePath: basePath)) |> map { network -> Either in - return .left(value: UnauthorizedAccount(id: id, postbox: postbox, network: network)) + return .left(value: UnauthorizedAccount(id: id, basePath: basePath, postbox: postbox, network: network)) } } } @@ -349,6 +355,7 @@ public enum AccountServiceTaskMasterMode { public class Account { public let id: AccountId + public let basePath: String public let postbox: Postbox public let network: Network public let peerId: PeerId @@ -377,8 +384,9 @@ public class Account { public let shouldBeServiceTaskMaster = Promise() public let shouldKeepOnlinePresence = Promise() - public init(id: AccountId, postbox: Postbox, network: Network, peerId: PeerId) { + public init(id: AccountId, basePath: String, postbox: Postbox, network: Network, peerId: PeerId) { self.id = id + self.basePath = basePath self.postbox = postbox self.network = network self.peerId = peerId @@ -492,8 +500,6 @@ public class Account { } } self.updatedPresenceDisposable.set(updatedPresence.start()) - - } deinit { @@ -503,6 +509,20 @@ public class Account { self.managedServiceViewsDisposable.dispose() self.updatedPresenceDisposable.dispose() } + + public func currentNetworkStats() -> Signal { + return Signal { subscriber in + let manager = MTNetworkUsageManager(info: MTNetworkUsageCalculationInfo(filePath: accountNetworkUsageInfoPath(basePath: self.basePath)))! + manager.currentStats().start(next: { next in + if let stats = next as? MTNetworkUsageManagerStats { + subscriber.putNext(stats) + } + subscriber.putCompletion() + }, error: nil, completed: nil) + + return EmptyDisposable + } + } } public func setupAccount(_ account: Account, fetchCachedResourceRepresentation: ((_ account: Account, _ resource: MediaResource, _ resourceData: MediaResourceData, _ representation: CachedMediaResourceRepresentation) -> Signal)? = nil) { diff --git a/TelegramCore/AccountViewTracker.swift b/TelegramCore/AccountViewTracker.swift index a6b1aa7f0b..aac6861a8a 100644 --- a/TelegramCore/AccountViewTracker.swift +++ b/TelegramCore/AccountViewTracker.swift @@ -263,7 +263,7 @@ public final class AccountViewTracker { public func aroundUnreadMessageHistoryViewForPeerId(_ peerId: PeerId, count: Int, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> { if let account = self.account { - let signal = account.postbox.aroundUnreadMessageHistoryViewForPeerId(peerId, count: count, tagMask: tagMask) + let signal = account.postbox.aroundUnreadMessageHistoryViewForPeerId(peerId, count: count, topTaggedMessageIdNamespaces: [Namespaces.Message.Cloud], tagMask: tagMask) return wrappedMessageHistorySignal(signal) } else { return .never() @@ -272,7 +272,7 @@ public final class AccountViewTracker { public func aroundIdMessageHistoryViewForPeerId(_ peerId: PeerId, count: Int, messageId: MessageId, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> { if let account = self.account { - let signal = account.postbox.aroundIdMessageHistoryViewForPeerId(peerId, count: count, messageId: messageId, tagMask: tagMask) + let signal = account.postbox.aroundIdMessageHistoryViewForPeerId(peerId, count: count, messageId: messageId, topTaggedMessageIdNamespaces: [Namespaces.Message.Cloud], tagMask: tagMask) return wrappedMessageHistorySignal(signal) } else { return .never() @@ -281,7 +281,7 @@ public final class AccountViewTracker { public func aroundMessageHistoryViewForPeerId(_ peerId: PeerId, index: MessageIndex, count: Int, anchorIndex: MessageIndex, fixedCombinedReadState: CombinedPeerReadState?, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> { if let account = self.account { - let signal = account.postbox.aroundMessageHistoryViewForPeerId(peerId, index: index, count: count, anchorIndex: anchorIndex, fixedCombinedReadState: fixedCombinedReadState, tagMask: tagMask) + let signal = account.postbox.aroundMessageHistoryViewForPeerId(peerId, index: index, count: count, anchorIndex: anchorIndex, fixedCombinedReadState: fixedCombinedReadState, topTaggedMessageIdNamespaces: [Namespaces.Message.Cloud], tagMask: tagMask) return wrappedMessageHistorySignal(signal) } else { return .never() diff --git a/TelegramCore/Download.swift b/TelegramCore/Download.swift index 95e3e005ef..5eec0cc4a7 100644 --- a/TelegramCore/Download.swift +++ b/TelegramCore/Download.swift @@ -19,7 +19,7 @@ class Download { self.datacenterId = datacenterId self.context = context - self.mtProto = MTProto(context: self.context, datacenterId: datacenterId) + self.mtProto = MTProto(context: self.context, datacenterId: datacenterId, usageCalculationInfo: nil) if datacenterId != masterDatacenterId { self.mtProto.authTokenMasterDatacenterId = masterDatacenterId self.mtProto.requiredAuthToken = Int(datacenterId) as NSNumber diff --git a/TelegramCore/MessageUtils.swift b/TelegramCore/MessageUtils.swift index 07f06fc1ff..68090282df 100644 --- a/TelegramCore/MessageUtils.swift +++ b/TelegramCore/MessageUtils.swift @@ -16,3 +16,37 @@ public extension Message { } } } + +public extension Message { + var visibleButtonKeyboardMarkup: ReplyMarkupMessageAttribute? { + for attribute in self.attributes { + if let attribute = attribute as? ReplyMarkupMessageAttribute { + if !attribute.flags.contains(.inline) && !attribute.rows.isEmpty { + if attribute.flags.contains(.personal) { + if !self.flags.contains(.Personal) { + return nil + } + } + return attribute + } + } + } + return nil + } + + var requestsSetupReply: Bool { + for attribute in self.attributes { + if let attribute = attribute as? ReplyMarkupMessageAttribute { + if !attribute.flags.contains(.inline) { + if attribute.flags.contains(.personal) { + if !self.flags.contains(.Personal) { + return false + } + } + return attribute.flags.contains(.setupReply) + } + } + } + return false + } +} diff --git a/TelegramCore/Network.swift b/TelegramCore/Network.swift index e5563bfa21..05bed7b5e9 100644 --- a/TelegramCore/Network.swift +++ b/TelegramCore/Network.swift @@ -94,7 +94,7 @@ private var registeredLoggingFunctions: Void = { registerLoggingFunctions() }() -func initializedNetwork(datacenterId: Int, keychain: Keychain) -> Signal { +func initializedNetwork(datacenterId: Int, keychain: Keychain, networkUsageInfoPath: String?) -> Signal { return Signal { subscriber in Queue.concurrentDefaultQueue().async { let _ = registeredLoggingFunctions @@ -126,7 +126,7 @@ func initializedNetwork(datacenterId: Int, keychain: Keychain) -> Signal(.WaitingForNetwork) diff --git a/TelegramCore/ReplyMarkupMessageAttribute.swift b/TelegramCore/ReplyMarkupMessageAttribute.swift index f7e596c39d..e5ea3d4664 100644 --- a/TelegramCore/ReplyMarkupMessageAttribute.swift +++ b/TelegramCore/ReplyMarkupMessageAttribute.swift @@ -165,6 +165,7 @@ public struct ReplyMarkupMessageFlags: OptionSet { public static let personal = ReplyMarkupMessageFlags(rawValue: 1 << 1) public static let setupReply = ReplyMarkupMessageFlags(rawValue: 1 << 2) public static let inline = ReplyMarkupMessageFlags(rawValue: 1 << 3) + public static let fit = ReplyMarkupMessageFlags(rawValue: 1 << 4) } public class ReplyMarkupMessageAttribute: MessageAttribute, Equatable { @@ -231,6 +232,9 @@ extension ReplyMarkupMessageAttribute { switch apiMarkup { case let .replyKeyboardMarkup(markupFlags, apiRows): rows = apiRows.map { ReplyMarkupRow(apiRow: $0) } + if (markupFlags & (1 << 0)) != 0 { + flags.insert(.fit) + } if (markupFlags & (1 << 1)) != 0 { flags.insert(.once) } @@ -247,6 +251,7 @@ extension ReplyMarkupMessageAttribute { if (forceReplyFlags & (1 << 2)) != 0 { flags.insert(.personal) } + flags.insert(.setupReply) case let .replyKeyboardHide(hideFlags): if (hideFlags & (1 << 2)) != 0 { flags.insert(.personal) diff --git a/TelegramCore/StoreMessage_Telegram.swift b/TelegramCore/StoreMessage_Telegram.swift index 135e0c3bc8..19862bc0c4 100644 --- a/TelegramCore/StoreMessage_Telegram.swift +++ b/TelegramCore/StoreMessage_Telegram.swift @@ -353,16 +353,21 @@ extension StoreMessage { attributes.append(TextEntitiesMessageAttribute(entities: messageTextEntitiesFromApiEntities(entities))) } + var storeFlags = StoreMessageFlags() + if let replyMarkup = replyMarkup { - attributes.append(ReplyMarkupMessageAttribute(apiMarkup: replyMarkup)) + let parsedReplyMarkup = ReplyMarkupMessageAttribute(apiMarkup: replyMarkup) + attributes.append(parsedReplyMarkup) + if !parsedReplyMarkup.flags.contains(.inline) { + storeFlags.insert(.TopIndexable) + } } - var storeFlags = StoreMessageFlags() if (flags & (1 << 1)) == 0 { - let _ = storeFlags.insert(.Incoming) + storeFlags.insert(.Incoming) } if (flags & (1 << 4)) != 0 { - let _ = storeFlags.insert(.Personal) + storeFlags.insert(.Personal) } self.init(id: MessageId(peerId: peerId, namespace: Namespaces.Message.Cloud, id: id), timestamp: date, flags: storeFlags, tags: tagsForStoreMessage(medias), forwardInfo: forwardInfo, authorId: authorId, text: messageText, attributes: attributes, media: medias)