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

@ -711,6 +711,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()
|> deliverOnMainQueue).start(next: { view in |> deliverOnMainQueue).start(next: { view in
@ -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,16 +1433,25 @@ 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")
let decryptedPayloadAndAccountId: ([AnyHashable: Any], AccountRecordId)?
if let accountIdString = payload.dictionaryPayload["accountId"] as? String, let accountId = Int64(accountIdString) {
decryptedPayloadAndAccountId = (payload.dictionaryPayload, AccountRecordId(rawValue: accountId))
} else {
guard var encryptedPayload = payload.dictionaryPayload["p"] as? String else { guard var encryptedPayload = payload.dictionaryPayload["p"] as? String else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "encryptedPayload is nil") Logger.shared.log("App \(self.episodeId) PushRegistry", "encryptedPayload is nil")
completion() completion()
@ -1490,11 +1499,21 @@ private func extractAccountManagerState(records: AccountRecordsView<TelegramAcco
completion() completion()
return return
} }
guard let payloadJson = try? JSONSerialization.jsonObject(with: decryptedPayload, options: []) as? [String: Any] else { 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") Logger.shared.log("App \(self.episodeId) PushRegistry", "Couldn't decode payload json")
completion() completion()
return return
} }
decryptedPayloadAndAccountId = (payloadJson, accountId)
}
guard let (payloadJson, accountId) = decryptedPayloadAndAccountId else {
Logger.shared.log("App \(self.episodeId) PushRegistry", "decryptedPayloadAndAccountId is nil")
completion()
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()
@ -1544,55 +1563,22 @@ 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 {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+")
encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/")
while encryptedPayload.count % 4 != 0 {
encryptedPayload.append("=")
}
if let data = Data(base64Encoded: encryptedPayload) {
let _ = (sharedApplicationContext.sharedContext.activeAccountContexts let _ = (sharedApplicationContext.sharedContext.activeAccountContexts
|> take(1) |> take(1)
|> mapToSignal { activeAccounts -> Signal<[(Account, MasterNotificationKey)], NoError> in |> deliverOnMainQueue).start(next: { activeAccounts in
return combineLatest(activeAccounts.accounts.map { context -> Signal<(Account, MasterNotificationKey), NoError> in for (_, context, _) in activeAccounts.accounts {
return masterNotificationsKey(account: context.1.account, ignoreDisabled: true) if context.account.id == accountId {
|> map { key -> (Account, MasterNotificationKey) in context.account.stateManager.processIncomingCallUpdate(data: updateData, completion: { _ in
return (context.1.account, key)
}
}) })
}
|> 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 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,6 +172,14 @@ 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 let accountIdString = notification.dict["accountId"] as? String, let accountId = Int64(accountIdString) {
inner: for (account, isCurrent, _) in accountsAndKeys {
if account.id.int64 == accountId {
decryptedNotifications.append((account, isCurrent, notification.dict))
break inner
}
}
} else {
if var encryptedPayload = notification.dict["p"] as? String { if var encryptedPayload = notification.dict["p"] as? String {
encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+") encryptedPayload = encryptedPayload.replacingOccurrences(of: "-", with: "+")
encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/") encryptedPayload = encryptedPayload.replacingOccurrences(of: "_", with: "/")
@ -190,6 +198,7 @@ public final class SharedNotificationManager {
} }
} }
} }
}
self.notifications.removeAll() self.notifications.removeAll()
for (account, isCurrent, payload) in decryptedNotifications { for (account, isCurrent, payload) in decryptedNotifications {