Fix call reporting

This commit is contained in:
Ali 2021-11-15 19:24:47 +04:00
parent 5b285a8b66
commit 78f4adf784
4 changed files with 116 additions and 115 deletions

View File

@ -729,6 +729,7 @@ private final class NotificationServiceHandler {
var accessHash: Int64 var accessHash: Int64
var fromId: PeerId var fromId: PeerId
var updates: String var updates: String
var accountId: Int64
} }
var callData: CallData? var callData: CallData?
@ -761,7 +762,8 @@ private final class NotificationServiceHandler {
id: callId, id: callId,
accessHash: callAccessHash, accessHash: callAccessHash,
fromId: peerId, fromId: peerId,
updates: updates updates: updates,
accountId: recordId.int64
) )
} }
} }
@ -908,7 +910,8 @@ private final class NotificationServiceHandler {
"call_id": "\(callData.id)", "call_id": "\(callData.id)",
"call_ah": "\(callData.accessHash)", "call_ah": "\(callData.accessHash)",
"from_id": "\(callData.fromId.id._internalGetInt64Value())", "from_id": "\(callData.fromId.id._internalGetInt64Value())",
"updates": callData.updates "updates": callData.updates,
"accountId": "\(callData.accountId)"
] ]
Logger.shared.log("NotificationService \(episode)", "Will report voip notification") Logger.shared.log("NotificationService \(episode)", "Will report voip notification")
let content = NotificationContent() let content = NotificationContent()

View File

@ -9,7 +9,13 @@ import SwiftSignalKit
import AppBundle import AppBundle
import AccountContext import AccountContext
private var sharedProviderDelegate: AnyObject? private let sharedProviderDelegate: AnyObject? = {
if #available(iOSApplicationExtension 10.0, iOS 10.0, *) {
return CallKitProviderDelegate()
} else {
return nil
}
}()
public final class CallKitIntegration { public final class CallKitIntegration {
public static var isAvailable: Bool { public static var isAvailable: Bool {
@ -42,9 +48,6 @@ public final class CallKitIntegration {
audioSessionActivationChanged: @escaping (Bool) -> Void audioSessionActivationChanged: @escaping (Bool) -> Void
) { ) {
if #available(iOSApplicationExtension 10.0, iOS 10.0, *) { if #available(iOSApplicationExtension 10.0, iOS 10.0, *) {
if sharedProviderDelegate == nil {
sharedProviderDelegate = CallKitProviderDelegate()
}
(sharedProviderDelegate as? CallKitProviderDelegate)?.setup(audioSessionActivePromise: self.audioSessionActivePromise, startCall: startCall, answerCall: answerCall, endCall: endCall, setCallMuted: setCallMuted, audioSessionActivationChanged: audioSessionActivationChanged) (sharedProviderDelegate as? CallKitProviderDelegate)?.setup(audioSessionActivePromise: self.audioSessionActivePromise, startCall: startCall, answerCall: answerCall, endCall: endCall, setCallMuted: setCallMuted, audioSessionActivationChanged: audioSessionActivationChanged)
} }
} }

View File

@ -710,6 +710,13 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
telegramUIDeclareEncodables() telegramUIDeclareEncodables()
initializeAccountManagement() initializeAccountManagement()
let pushRegistry = PKPushRegistry(queue: .main)
if #available(iOS 9.0, *) {
pushRegistry.desiredPushTypes = Set([.voIP])
}
self.pushRegistry = pushRegistry
pushRegistry.delegate = self
self.accountManagerState = extractAccountManagerState(records: accountManager._internalAccountRecordsSync()) self.accountManagerState = extractAccountManagerState(records: accountManager._internalAccountRecordsSync())
let _ = (accountManager.accountRecords() let _ = (accountManager.accountRecords()
@ -1167,13 +1174,6 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
} }
}) })
let pushRegistry = PKPushRegistry(queue: .main)
if #available(iOS 9.0, *) {
pushRegistry.desiredPushTypes = Set([.voIP])
}
self.pushRegistry = pushRegistry
pushRegistry.delegate = self
self.resetBadge() self.resetBadge()
if #available(iOS 9.1, *) { if #available(iOS 9.1, *) {
@ -1433,68 +1433,87 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
} }
public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) { public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
Logger.shared.log("App \(self.episodeId) PushRegistry", "pushRegistry didReceiveIncomingPushWith \(payload.dictionaryPayload)")
self.pushRegistryImpl(registry, didReceiveIncomingPushWith: payload, for: type, completion: completion) self.pushRegistryImpl(registry, didReceiveIncomingPushWith: payload, for: type, completion: completion)
} }
public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) { public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
Logger.shared.log("App \(self.episodeId) PushRegistry", "pushRegistry didReceiveIncomingPushWith \(payload.dictionaryPayload)")
self.pushRegistryImpl(registry, didReceiveIncomingPushWith: payload, for: type, completion: {}) self.pushRegistryImpl(registry, didReceiveIncomingPushWith: payload, for: type, completion: {})
} }
private func pushRegistryImpl(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) { private func pushRegistryImpl(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
Logger.shared.log("App \(self.episodeId) PushRegistry", "pushRegistry didReceiveIncomingPushWith") Logger.shared.log("App \(self.episodeId) PushRegistry", "pushRegistry processing push notification")
guard var encryptedPayload = payload.dictionaryPayload["p"] as? String else { let decryptedPayloadAndAccountId: ([AnyHashable: Any], AccountRecordId)?
Logger.shared.log("App \(self.episodeId) PushRegistry", "encryptedPayload is nil")
completion() if let accountIdString = payload.dictionaryPayload["accountId"] as? String, let accountId = Int64(accountIdString) {
return decryptedPayloadAndAccountId = (payload.dictionaryPayload, AccountRecordId(rawValue: accountId))
} } else {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+") guard var encryptedPayload = payload.dictionaryPayload["p"] as? String else {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/") Logger.shared.log("App \(self.episodeId) PushRegistry", "encryptedPayload is nil")
while encryptedPayload.count % 4 != 0 { completion()
encryptedPayload.append("=") return
} }
guard let payloadData = Data(base64Encoded: encryptedPayload) else { encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+")
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decode encryptedPayload") encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/")
completion() while encryptedPayload.count % 4 != 0 {
return encryptedPayload.append("=")
} }
guard let keyId = notificationPayloadKeyId(data: payloadData) else { guard let payloadData = Data(base64Encoded: encryptedPayload) else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't parse payload key id") Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decode encryptedPayload")
completion() completion()
return return
} }
guard let accountManagerState = self.accountManagerState else { guard let keyId = notificationPayloadKeyId(data: payloadData) else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "accountManagerState is nil") Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't parse payload key id")
completion() completion()
return return
} }
guard let accountManagerState = self.accountManagerState else {
var maybeAccountId: AccountRecordId? Logger.shared.log("App \(self.episodeId) PushRegistry", "accountManagerState is nil")
var maybeNotificationKey: MasterNotificationKey? completion()
return
for key in accountManagerState.notificationKeys {
if key.id == keyId {
maybeAccountId = key.accountId
maybeNotificationKey = MasterNotificationKey(id: key.id, data: key.key)
break
} }
}
guard let accountId = maybeAccountId, let notificationKey = maybeNotificationKey else { var maybeAccountId: AccountRecordId?
Logger.shared.log("App \(self.episodeId) PushRegistry", "accountId or notificationKey is nil") var maybeNotificationKey: MasterNotificationKey?
completion()
return for key in accountManagerState.notificationKeys {
} if key.id == keyId {
guard let decryptedPayload = decryptedNotificationPayload(key: notificationKey, data: payloadData) else { maybeAccountId = key.accountId
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decrypt payload") maybeNotificationKey = MasterNotificationKey(id: key.id, data: key.key)
completion() break
return }
} }
guard let payloadJson = try? JSONSerialization.jsonObject(with: decryptedPayload, options: []) as? [String: Any] else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decode payload json") guard let accountId = maybeAccountId, let notificationKey = maybeNotificationKey else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "accountId or notificationKey is nil")
completion()
return
}
guard let decryptedPayload = decryptedNotificationPayload(key: notificationKey, data: payloadData) else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decrypt payload")
completion()
return
}
guard let payloadJson = try? JSONSerialization.jsonObject(with: decryptedPayload, options: []) as? [AnyHashable: Any] else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decode payload json")
completion()
return
}
decryptedPayloadAndAccountId = (payloadJson, accountId)
}
guard let (payloadJson, accountId) = decryptedPayloadAndAccountId else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "decryptedPayloadAndAccountId is nil")
completion() completion()
return return
} }
guard var updateString = payloadJson["updates"] as? String else { guard var updateString = payloadJson["updates"] as? String else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "updates is nil") Logger.shared.log("App \(self.episodeId) PushRegistry", "updates is nil")
completion() completion()
@ -1543,56 +1562,23 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
} }
} }
) )
let _ = accountId
let _ = (self.sharedContextPromise.get() let _ = (self.sharedContextPromise.get()
|> take(1) |> take(1)
|> deliverOnMainQueue).start(next: { sharedApplicationContext in |> deliverOnMainQueue).start(next: { sharedApplicationContext in
if var encryptedPayload = payload.dictionaryPayload["p"] as? String { let _ = (sharedApplicationContext.sharedContext.activeAccountContexts
encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+") |> take(1)
encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/") |> deliverOnMainQueue).start(next: { activeAccounts in
while encryptedPayload.count % 4 != 0 { for (_, context, _) in activeAccounts.accounts {
encryptedPayload.append("=") if context.account.id == accountId {
} context.account.stateManager.processIncomingCallUpdate(data: updateData, completion: { _ in
if let data = Data(base64Encoded: encryptedPayload) {
let _ = (sharedApplicationContext.sharedContext.activeAccountContexts
|> take(1)
|> mapToSignal { activeAccounts -> Signal<[(Account, MasterNotificationKey)], NoError> in
return combineLatest(activeAccounts.accounts.map { context -> Signal<(Account, MasterNotificationKey), NoError> in
return masterNotificationsKey(account: context.1.account, ignoreDisabled: true)
|> map { key -> (Account, MasterNotificationKey) in
return (context.1.account, key)
}
}) })
break
} }
|> deliverOnMainQueue).start(next: { accountsAndKeys in
var accountAndDecryptedPayload: (Account, Data)?
for (account, key) in accountsAndKeys {
if let decryptedData = decryptedNotificationPayload(key: key, data: data) {
accountAndDecryptedPayload = (account, decryptedData)
break
}
}
if let (account, decryptedData) = accountAndDecryptedPayload {
if let decryptedDict = (try? JSONSerialization.jsonObject(with: decryptedData, options: [])) as? [AnyHashable: Any] {
if var updateString = decryptedDict["updates"] as? String {
updateString = updateString.replacingOccurrences(of: "-", with: "+")
updateString = updateString.replacingOccurrences(of: "_", with: "/")
while updateString.count % 4 != 0 {
updateString.append("=")
}
if let updateData = Data(base64Encoded: updateString) {
account.stateManager.processIncomingCallUpdate(data: updateData, completion: { _ in
})
}
}
}
}
})
} }
} })
sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0) sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0)
if case PKPushType.voIP = type { if case PKPushType.voIP = type {

View File

@ -172,19 +172,28 @@ public final class SharedNotificationManager {
} }
var decryptedNotifications: [(Account, Bool, [AnyHashable: Any])] = [] var decryptedNotifications: [(Account, Bool, [AnyHashable: Any])] = []
for notification in self.notifications { for notification in self.notifications {
if var encryptedPayload = notification.dict["p"] as? String { if let accountIdString = notification.dict["accountId"] as? String, let accountId = Int64(accountIdString) {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+") inner: for (account, isCurrent, _) in accountsAndKeys {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/") if account.id.int64 == accountId {
while encryptedPayload.count % 4 != 0 { decryptedNotifications.append((account, isCurrent, notification.dict))
encryptedPayload.append("=") break inner
}
} }
if let data = Data(base64Encoded: encryptedPayload) { } else {
inner: for (account, isCurrent, key) in accountsAndKeys { if var encryptedPayload = notification.dict["p"] as? String {
if let decryptedData = decryptedNotificationPayload(key: key, data: data) { encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+")
if let decryptedDict = (try? JSONSerialization.jsonObject(with: decryptedData, options: [])) as? [AnyHashable: Any] { encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/")
decryptedNotifications.append((account, isCurrent, decryptedDict)) while encryptedPayload.count % 4 != 0 {
encryptedPayload.append("=")
}
if let data = Data(base64Encoded: encryptedPayload) {
inner: for (account, isCurrent, key) in accountsAndKeys {
if let decryptedData = decryptedNotificationPayload(key: key, data: data) {
if let decryptedDict = (try? JSONSerialization.jsonObject(with: decryptedData, options: [])) as? [AnyHashable: Any] {
decryptedNotifications.append((account, isCurrent, decryptedDict))
}
break inner
} }
break inner
} }
} }
} }