diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/FetchImage.m b/Telegram/NotificationService/NotificationServiceObjC/Sources/FetchImage.m index 4407b0c46c..58a85bdce1 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/FetchImage.m +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/FetchImage.m @@ -154,7 +154,19 @@ dispatch_block_t fetchImage(BuildConfig *buildConfig, AccountProxyConnection * _ for (NSNumber *datacenterId in account.datacenters) { AccountDatacenterInfo *info = account.datacenters[datacenterId]; - [context updateAuthInfoForDatacenterWithId:[datacenterId intValue] authInfo:[[MTDatacenterAuthInfo alloc] initWithAuthKey:info.masterKey.data authKeyId:info.masterKey.keyId saltSet:@[] authKeyAttributes:@{}] selector:MTDatacenterAuthInfoSelectorPersistent]; + MTDatacenterAuthInfo *authInfo = [[MTDatacenterAuthInfo alloc] initWithAuthKey:info.masterKey.data authKeyId:info.masterKey.keyId saltSet:@[] authKeyAttributes:@{}]; + + [context updateAuthInfoForDatacenterWithId:[datacenterId intValue] authInfo:authInfo selector:MTDatacenterAuthInfoSelectorPersistent]; + + if (info.ephemeralMainKey != nil) { + MTDatacenterAuthInfo *ephemeralMainAuthInfo = [[MTDatacenterAuthInfo alloc] initWithAuthKey:info.ephemeralMainKey.data authKeyId:info.ephemeralMainKey.keyId saltSet:@[] authKeyAttributes:@{}]; + [context updateAuthInfoForDatacenterWithId:[datacenterId intValue] authInfo:ephemeralMainAuthInfo selector:MTDatacenterAuthInfoSelectorEphemeralMain]; + } + + if (info.ephemeralMediaKey != nil) { + MTDatacenterAuthInfo *ephemeralMediaAuthInfo = [[MTDatacenterAuthInfo alloc] initWithAuthKey:info.ephemeralMediaKey.data authKeyId:info.ephemeralMediaKey.keyId saltSet:@[] authKeyAttributes:@{}]; + [context updateAuthInfoForDatacenterWithId:[datacenterId intValue] authInfo:ephemeralMediaAuthInfo selector:MTDatacenterAuthInfoSelectorEphemeralMedia]; + } } MTProto *mtProto = [[MTProto alloc] initWithContext:context datacenterId:datacenterId usageCalculationInfo:nil requiredAuthToken:nil authTokenMasterDatacenterId:0]; diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/NotificationService.m b/Telegram/NotificationService/NotificationServiceObjC/Sources/NotificationService.m index c86437eccc..1a0471f219 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/NotificationService.m +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/NotificationService.m @@ -157,7 +157,7 @@ static void reportMemory() { - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { if (_rootPath == nil) { - _bestAttemptContent = request.content; + _bestAttemptContent = (UNMutableNotificationContent *)[request.content mutableCopy]; [self completeWithBestAttemptContent]; return; } diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m b/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m index c855db593b..833f475bef 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m @@ -3,7 +3,7 @@ @implementation Serialization - (NSUInteger)currentLayer { - return 122; + return 124; } - (id _Nullable)parseMessage:(NSData * _Nullable)data { diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.h b/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.h index a5fb775e47..578bd01cf3 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.h +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.h @@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN @interface AccountDatacenterInfo: NSObject @property (nonatomic, strong, readonly) AccountDatacenterKey *masterKey; +@property (nonatomic, strong, readonly) AccountDatacenterKey *ephemeralMainKey; +@property (nonatomic, strong, readonly) AccountDatacenterKey *ephemeralMediaKey; @property (nonatomic, strong, readonly) NSArray *addressList; @end diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.m b/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.m index 424aa3aefc..1e08d8300a 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.m +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/StoredAccountInfos.m @@ -116,10 +116,12 @@ @implementation AccountDatacenterInfo -- (instancetype)initWithMasterKey:(AccountDatacenterKey *)masterKey addressList:(NSArray *)addressList { +- (instancetype)initWithMasterKey:(AccountDatacenterKey *)masterKey ephemeralMainKey:(AccountDatacenterKey * _Nullable)ephemeralMainKey ephemeralMediaKey:(AccountDatacenterKey * _Nullable)ephemeralMediaKey addressList:(NSArray *)addressList { self = [super init]; if (self != nil) { _masterKey = masterKey; + _ephemeralMainKey = ephemeralMainKey; + _ephemeralMediaKey = ephemeralMediaKey; _addressList = addressList; } return self; @@ -135,6 +137,18 @@ return nil; } + NSDictionary *ephemeralMainKeyDict = dict[@"ephemeralMainKey"]; + AccountDatacenterKey *ephemeralMainKey = nil; + if ([ephemeralMainKeyDict isKindOfClass:[NSDictionary class]]) { + ephemeralMainKey = [AccountDatacenterKey parse:ephemeralMainKeyDict]; + } + + NSDictionary *ephemeralMediaKeyDict = dict[@"ephemeralMediaKey"]; + AccountDatacenterKey *ephemeralMediaKey = nil; + if ([ephemeralMediaKeyDict isKindOfClass:[NSDictionary class]]) { + ephemeralMediaKey = [AccountDatacenterKey parse:ephemeralMediaKeyDict]; + } + NSArray *addressListArray = dict[@"addressList"]; if (![addressListArray isKindOfClass:[NSArray class]]) { return nil; @@ -152,7 +166,7 @@ [addressList addObject:address]; } - return [[AccountDatacenterInfo alloc] initWithMasterKey:masterKey addressList:addressList]; + return [[AccountDatacenterInfo alloc] initWithMasterKey:masterKey ephemeralMainKey:ephemeralMainKey ephemeralMediaKey:ephemeralMediaKey addressList:addressList]; } @end diff --git a/submodules/LightweightAccountData/Sources/SharedAccountInfo.swift b/submodules/LightweightAccountData/Sources/SharedAccountInfo.swift index 846aada6a6..6b1c25ff05 100644 --- a/submodules/LightweightAccountData/Sources/SharedAccountInfo.swift +++ b/submodules/LightweightAccountData/Sources/SharedAccountInfo.swift @@ -36,10 +36,14 @@ public struct AccountDatacenterAddress: Codable { public struct AccountDatacenterInfo: Codable { public let masterKey: AccountDatacenterKey + public let ephemeralMainKey: AccountDatacenterKey? + public let ephemeralMediaKey: AccountDatacenterKey? public let addressList: [AccountDatacenterAddress] - public init(masterKey: AccountDatacenterKey, addressList: [AccountDatacenterAddress]) { + public init(masterKey: AccountDatacenterKey, ephemeralMainKey: AccountDatacenterKey?, ephemeralMediaKey: AccountDatacenterKey?, addressList: [AccountDatacenterAddress]) { self.masterKey = masterKey + self.ephemeralMainKey = ephemeralMainKey + self.ephemeralMediaKey = ephemeralMediaKey self.addressList = addressList } } diff --git a/submodules/TelegramUI/Sources/AppDelegate.swift b/submodules/TelegramUI/Sources/AppDelegate.swift index 9adaad4deb..e4b387d2d8 100644 --- a/submodules/TelegramUI/Sources/AppDelegate.swift +++ b/submodules/TelegramUI/Sources/AppDelegate.swift @@ -31,6 +31,7 @@ import PresentationDataUtils import TelegramIntents import AccountUtils import CoreSpotlight +import LightweightAccountData #if canImport(BackgroundTasks) import BackgroundTasks @@ -809,8 +810,13 @@ final class SharedApplicationContext { |> map { _, accounts, _ -> [Account] in return accounts.map({ $0.1 }) } - let _ = (sharedAccountInfos(accountManager: sharedContext.accountManager, accounts: rawAccounts) - |> deliverOn(Queue())).start(next: { infos in + let storeQueue = Queue() + let _ = ( + sharedAccountInfos(accountManager: sharedContext.accountManager, accounts: rawAccounts) + |> then(Signal.complete() |> delay(10.0, queue: storeQueue)) + |> restart + |> deliverOn(storeQueue) + ).start(next: { infos in storeAccountsData(rootPath: rootPath, accounts: infos) }) diff --git a/submodules/TelegramUI/Sources/ManageSharedAccountInfo.swift b/submodules/TelegramUI/Sources/ManageSharedAccountInfo.swift index 396a494187..a18d839225 100644 --- a/submodules/TelegramUI/Sources/ManageSharedAccountInfo.swift +++ b/submodules/TelegramUI/Sources/ManageSharedAccountInfo.swift @@ -29,7 +29,23 @@ private func accountInfo(account: Account) -> Signal let secret: Data? = address.secret addressList.append(AccountDatacenterAddress(host: host, port: Int32(address.port), isMedia: address.preferForMedia, secret: secret)) } - datacenters[Int32(id)] = AccountDatacenterInfo(masterKey: AccountDatacenterKey(id: authInfo.authKeyId, data: authKey), addressList: addressList) + + var ephemeralMainKey: AccountDatacenterKey? + if let ephemeralMainAuthInfo = context.authInfoForDatacenter(withId: id, selector: .ephemeralMain), let ephemeralAuthKey = ephemeralMainAuthInfo.authKey { + ephemeralMainKey = AccountDatacenterKey(id: ephemeralMainAuthInfo.authKeyId, data: ephemeralAuthKey) + } + + var ephemeralMediaKey: AccountDatacenterKey? + if let ephemeralMediaAuthInfo = context.authInfoForDatacenter(withId: id, selector: .ephemeralMedia), let ephemeralAuthKey = ephemeralMediaAuthInfo.authKey { + ephemeralMediaKey = AccountDatacenterKey(id: ephemeralMediaAuthInfo.authKeyId, data: ephemeralAuthKey) + } + + datacenters[Int32(id)] = AccountDatacenterInfo( + masterKey: AccountDatacenterKey(id: authInfo.authKeyId, data: authKey), + ephemeralMainKey: ephemeralMainKey, + ephemeralMediaKey: ephemeralMediaKey, + addressList: addressList + ) } } } @@ -38,12 +54,20 @@ private func accountInfo(account: Account) -> Signal return combineLatest(peerName, notificationKey) |> map { peerName, notificationKey -> StoredAccountInfo in - return StoredAccountInfo(id: account.id.int64, primaryId: primaryDatacenterId, isTestingEnvironment: account.testingEnvironment, peerName: peerName, datacenters: datacenters, notificationKey: AccountNotificationKey(id: notificationKey.id, data: notificationKey.data)) + return StoredAccountInfo( + id: account.id.int64, + primaryId: primaryDatacenterId, + isTestingEnvironment: account.testingEnvironment, + peerName: peerName, + datacenters: datacenters, + notificationKey: AccountNotificationKey(id: notificationKey.id, data: notificationKey.data) + ) } } func sharedAccountInfos(accountManager: AccountManager, accounts: Signal<[Account], NoError>) -> Signal { return combineLatest(accountManager.sharedData(keys: [SharedDataKeys.proxySettings]), accounts) + |> take(1) |> mapToSignal { sharedData, accounts -> Signal in let proxySettings = sharedData.entries[SharedDataKeys.proxySettings] as? ProxySettings let proxy = proxySettings?.effectiveActiveServer.flatMap { proxyServer -> AccountProxyConnection? in