mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-25 15:01:38 +00:00
Merge branch 'master' of https://github.com/peter-iakovlev/TelegramCore
Conflicts: TelegramCore/Account.swift
This commit is contained in:
commit
b95fac4112
@ -172,6 +172,7 @@ public func generateAccountId() -> AccountId {
|
|||||||
|
|
||||||
public class UnauthorizedAccount {
|
public class UnauthorizedAccount {
|
||||||
public let id: AccountId
|
public let id: AccountId
|
||||||
|
public let basePath: String
|
||||||
public let postbox: Postbox
|
public let postbox: Postbox
|
||||||
public let network: Network
|
public let network: Network
|
||||||
|
|
||||||
@ -179,8 +180,9 @@ public class UnauthorizedAccount {
|
|||||||
return Int32(self.network.mtProto.datacenterId)
|
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.id = id
|
||||||
|
self.basePath = basePath
|
||||||
self.postbox = postbox
|
self.postbox = postbox
|
||||||
self.network = network
|
self.network = network
|
||||||
}
|
}
|
||||||
@ -198,9 +200,9 @@ public class UnauthorizedAccount {
|
|||||||
postbox.removeKeychainEntryForKey(key)
|
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
|
|> 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
|
return
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
func accountNetworkUsageInfoPath(basePath: String) -> String {
|
||||||
|
return basePath + "/network-usage"
|
||||||
|
}
|
||||||
|
|
||||||
public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal<Either<UnauthorizedAccount, Account>, NoError> {
|
public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal<Either<UnauthorizedAccount, Account>, NoError> {
|
||||||
return Signal<(Postbox, AccountState?), NoError> { subscriber in
|
return Signal<(String, Postbox, AccountState?), NoError> { subscriber in
|
||||||
let _ = declaredEncodables
|
let _ = declaredEncodables
|
||||||
|
|
||||||
let path = "\(appGroupPath)/account\(id.stringValue)"
|
let path = "\(appGroupPath)/account\(id.stringValue)"
|
||||||
@ -259,12 +265,12 @@ public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal<Eithe
|
|||||||
|
|
||||||
let postbox = Postbox(basePath: path + "/postbox", globalMessageIdsNamespace: Namespaces.Message.Cloud, seedConfiguration: seedConfiguration)
|
let postbox = Postbox(basePath: path + "/postbox", globalMessageIdsNamespace: Namespaces.Message.Cloud, seedConfiguration: seedConfiguration)
|
||||||
return (postbox.state() |> take(1) |> map { accountState in
|
return (postbox.state() |> take(1) |> map { accountState in
|
||||||
return (postbox, accountState as? AccountState)
|
return (path, postbox, accountState as? AccountState)
|
||||||
}).start(next: { pair in
|
}).start(next: { args in
|
||||||
subscriber.putNext(pair)
|
subscriber.putNext(args)
|
||||||
subscriber.putCompletion()
|
subscriber.putCompletion()
|
||||||
})
|
})
|
||||||
} |> mapToSignal { (postbox, accountState) -> Signal<Either<UnauthorizedAccount, Account>, NoError> in
|
} |> mapToSignal { (basePath, postbox, accountState) -> Signal<Either<UnauthorizedAccount, Account>, NoError> in
|
||||||
let keychain = Keychain(get: { key in
|
let keychain = Keychain(get: { key in
|
||||||
return postbox.keychainEntryForKey(key)
|
return postbox.keychainEntryForKey(key)
|
||||||
}, set: { (key, data) in
|
}, set: { (key, data) in
|
||||||
@ -276,23 +282,23 @@ public func accountWithId(_ id: AccountId, appGroupPath: String) -> Signal<Eithe
|
|||||||
if let accountState = accountState {
|
if let accountState = accountState {
|
||||||
switch accountState {
|
switch accountState {
|
||||||
case let unauthorizedState as UnauthorizedAccountState:
|
case let unauthorizedState as UnauthorizedAccountState:
|
||||||
return initializedNetwork(datacenterId: Int(unauthorizedState.masterDatacenterId), keychain: keychain)
|
return initializedNetwork(datacenterId: Int(unauthorizedState.masterDatacenterId), keychain: keychain, networkUsageInfoPath: accountNetworkUsageInfoPath(basePath: basePath))
|
||||||
|> map { network -> Either<UnauthorizedAccount, Account> in
|
|> map { network -> Either<UnauthorizedAccount, Account> 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:
|
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<UnauthorizedAccount, Account> in
|
|> map { network -> Either<UnauthorizedAccount, Account> 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 _:
|
case _:
|
||||||
assertionFailure("Unexpected accountState \(accountState)")
|
assertionFailure("Unexpected accountState \(accountState)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return initializedNetwork(datacenterId: 2, keychain: keychain)
|
return initializedNetwork(datacenterId: 2, keychain: keychain, networkUsageInfoPath: accountNetworkUsageInfoPath(basePath: basePath))
|
||||||
|> map { network -> Either<UnauthorizedAccount, Account> in
|
|> map { network -> Either<UnauthorizedAccount, Account> 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 class Account {
|
||||||
public let id: AccountId
|
public let id: AccountId
|
||||||
|
public let basePath: String
|
||||||
public let postbox: Postbox
|
public let postbox: Postbox
|
||||||
public let network: Network
|
public let network: Network
|
||||||
public let peerId: PeerId
|
public let peerId: PeerId
|
||||||
@ -377,10 +384,9 @@ public class Account {
|
|||||||
public let shouldBeServiceTaskMaster = Promise<AccountServiceTaskMasterMode>()
|
public let shouldBeServiceTaskMaster = Promise<AccountServiceTaskMasterMode>()
|
||||||
public let shouldKeepOnlinePresence = Promise<Bool>()
|
public let shouldKeepOnlinePresence = Promise<Bool>()
|
||||||
|
|
||||||
public var typing:TMProcessable!
|
public init(id: AccountId, basePath: String, postbox: Postbox, network: Network, peerId: PeerId) {
|
||||||
|
|
||||||
public init(id: AccountId, postbox: Postbox, network: Network, peerId: PeerId) {
|
|
||||||
self.id = id
|
self.id = id
|
||||||
|
self.basePath = basePath
|
||||||
self.postbox = postbox
|
self.postbox = postbox
|
||||||
self.network = network
|
self.network = network
|
||||||
self.peerId = peerId
|
self.peerId = peerId
|
||||||
@ -494,8 +500,6 @@ public class Account {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.updatedPresenceDisposable.set(updatedPresence.start())
|
self.updatedPresenceDisposable.set(updatedPresence.start())
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@ -505,6 +509,20 @@ public class Account {
|
|||||||
self.managedServiceViewsDisposable.dispose()
|
self.managedServiceViewsDisposable.dispose()
|
||||||
self.updatedPresenceDisposable.dispose()
|
self.updatedPresenceDisposable.dispose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func currentNetworkStats() -> Signal<MTNetworkUsageManagerStats, NoError> {
|
||||||
|
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<CachedMediaResourceRepresentationResult, NoError>)? = nil) {
|
public func setupAccount(_ account: Account, fetchCachedResourceRepresentation: ((_ account: Account, _ resource: MediaResource, _ resourceData: MediaResourceData, _ representation: CachedMediaResourceRepresentation) -> Signal<CachedMediaResourceRepresentationResult, NoError>)? = nil) {
|
||||||
|
@ -263,7 +263,7 @@ public final class AccountViewTracker {
|
|||||||
|
|
||||||
public func aroundUnreadMessageHistoryViewForPeerId(_ peerId: PeerId, count: Int, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> {
|
public func aroundUnreadMessageHistoryViewForPeerId(_ peerId: PeerId, count: Int, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> {
|
||||||
if let account = self.account {
|
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)
|
return wrappedMessageHistorySignal(signal)
|
||||||
} else {
|
} else {
|
||||||
return .never()
|
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> {
|
public func aroundIdMessageHistoryViewForPeerId(_ peerId: PeerId, count: Int, messageId: MessageId, tagMask: MessageTags? = nil) -> Signal<(MessageHistoryView, ViewUpdateType, InitialMessageHistoryData?), NoError> {
|
||||||
if let account = self.account {
|
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)
|
return wrappedMessageHistorySignal(signal)
|
||||||
} else {
|
} else {
|
||||||
return .never()
|
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> {
|
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 {
|
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)
|
return wrappedMessageHistorySignal(signal)
|
||||||
} else {
|
} else {
|
||||||
return .never()
|
return .never()
|
||||||
|
@ -19,7 +19,7 @@ class Download {
|
|||||||
self.datacenterId = datacenterId
|
self.datacenterId = datacenterId
|
||||||
self.context = context
|
self.context = context
|
||||||
|
|
||||||
self.mtProto = MTProto(context: self.context, datacenterId: datacenterId)
|
self.mtProto = MTProto(context: self.context, datacenterId: datacenterId, usageCalculationInfo: nil)
|
||||||
if datacenterId != masterDatacenterId {
|
if datacenterId != masterDatacenterId {
|
||||||
self.mtProto.authTokenMasterDatacenterId = masterDatacenterId
|
self.mtProto.authTokenMasterDatacenterId = masterDatacenterId
|
||||||
self.mtProto.requiredAuthToken = Int(datacenterId) as NSNumber
|
self.mtProto.requiredAuthToken = Int(datacenterId) as NSNumber
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -94,7 +94,7 @@ private var registeredLoggingFunctions: Void = {
|
|||||||
registerLoggingFunctions()
|
registerLoggingFunctions()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func initializedNetwork(datacenterId: Int, keychain: Keychain) -> Signal<Network, NoError> {
|
func initializedNetwork(datacenterId: Int, keychain: Keychain, networkUsageInfoPath: String?) -> Signal<Network, NoError> {
|
||||||
return Signal { subscriber in
|
return Signal { subscriber in
|
||||||
Queue.concurrentDefaultQueue().async {
|
Queue.concurrentDefaultQueue().async {
|
||||||
let _ = registeredLoggingFunctions
|
let _ = registeredLoggingFunctions
|
||||||
@ -126,7 +126,7 @@ func initializedNetwork(datacenterId: Int, keychain: Keychain) -> Signal<Network
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.keychain = keychain
|
context.keychain = keychain
|
||||||
let mtProto = MTProto(context: context, datacenterId: datacenterId)!
|
let mtProto = MTProto(context: context, datacenterId: datacenterId, usageCalculationInfo: networkUsageInfoPath == nil ? nil : MTNetworkUsageCalculationInfo(filePath: networkUsageInfoPath))!
|
||||||
|
|
||||||
let connectionStatus = Promise<ConnectionStatus>(.WaitingForNetwork)
|
let connectionStatus = Promise<ConnectionStatus>(.WaitingForNetwork)
|
||||||
|
|
||||||
|
@ -165,6 +165,7 @@ public struct ReplyMarkupMessageFlags: OptionSet {
|
|||||||
public static let personal = ReplyMarkupMessageFlags(rawValue: 1 << 1)
|
public static let personal = ReplyMarkupMessageFlags(rawValue: 1 << 1)
|
||||||
public static let setupReply = ReplyMarkupMessageFlags(rawValue: 1 << 2)
|
public static let setupReply = ReplyMarkupMessageFlags(rawValue: 1 << 2)
|
||||||
public static let inline = ReplyMarkupMessageFlags(rawValue: 1 << 3)
|
public static let inline = ReplyMarkupMessageFlags(rawValue: 1 << 3)
|
||||||
|
public static let fit = ReplyMarkupMessageFlags(rawValue: 1 << 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ReplyMarkupMessageAttribute: MessageAttribute, Equatable {
|
public class ReplyMarkupMessageAttribute: MessageAttribute, Equatable {
|
||||||
@ -231,6 +232,9 @@ extension ReplyMarkupMessageAttribute {
|
|||||||
switch apiMarkup {
|
switch apiMarkup {
|
||||||
case let .replyKeyboardMarkup(markupFlags, apiRows):
|
case let .replyKeyboardMarkup(markupFlags, apiRows):
|
||||||
rows = apiRows.map { ReplyMarkupRow(apiRow: $0) }
|
rows = apiRows.map { ReplyMarkupRow(apiRow: $0) }
|
||||||
|
if (markupFlags & (1 << 0)) != 0 {
|
||||||
|
flags.insert(.fit)
|
||||||
|
}
|
||||||
if (markupFlags & (1 << 1)) != 0 {
|
if (markupFlags & (1 << 1)) != 0 {
|
||||||
flags.insert(.once)
|
flags.insert(.once)
|
||||||
}
|
}
|
||||||
@ -247,6 +251,7 @@ extension ReplyMarkupMessageAttribute {
|
|||||||
if (forceReplyFlags & (1 << 2)) != 0 {
|
if (forceReplyFlags & (1 << 2)) != 0 {
|
||||||
flags.insert(.personal)
|
flags.insert(.personal)
|
||||||
}
|
}
|
||||||
|
flags.insert(.setupReply)
|
||||||
case let .replyKeyboardHide(hideFlags):
|
case let .replyKeyboardHide(hideFlags):
|
||||||
if (hideFlags & (1 << 2)) != 0 {
|
if (hideFlags & (1 << 2)) != 0 {
|
||||||
flags.insert(.personal)
|
flags.insert(.personal)
|
||||||
|
@ -353,16 +353,21 @@ extension StoreMessage {
|
|||||||
attributes.append(TextEntitiesMessageAttribute(entities: messageTextEntitiesFromApiEntities(entities)))
|
attributes.append(TextEntitiesMessageAttribute(entities: messageTextEntitiesFromApiEntities(entities)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var storeFlags = StoreMessageFlags()
|
||||||
|
|
||||||
if let replyMarkup = replyMarkup {
|
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 {
|
if (flags & (1 << 1)) == 0 {
|
||||||
let _ = storeFlags.insert(.Incoming)
|
storeFlags.insert(.Incoming)
|
||||||
}
|
}
|
||||||
if (flags & (1 << 4)) != 0 {
|
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)
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user