MSC4075 Use expirationTS to define the call ringing window (#4652)

* Listen to call decline to stop ringing when declined from other device

* MSC4075 Use expirationTS to define the call ringing window

* Implement ElementCallService tests.

* Update acknowledgements.
This commit is contained in:
Valere Fedronic
2025-11-12 13:59:09 +01:00
committed by GitHub
parent 91dabf96eb
commit cd411b57e0
15 changed files with 485 additions and 19 deletions
@@ -0,0 +1,17 @@
//
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import CallKit
// sourcery: AutoMockable
protocol CXProviderProtocol {
func setDelegate(_ delegate: CXProviderDelegate?, queue: DispatchQueue?)
func reportNewIncomingCall(with uuid: UUID, update: CXCallUpdate, completion: @escaping (Error?) -> Void)
func reportCall(with uuid: UUID, endedAt: Date?, reason: CXCallEndedReason)
}
extension CXProvider: CXProviderProtocol { }
@@ -14,6 +14,12 @@ import MatrixRustSDK
import PushKit
import UIKit
// Keep this class testable
struct TimeProvider {
var clock: any Clock<Duration>
var now: () -> Date
}
class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDelegate, CXProviderDelegate {
private struct CallID: Equatable {
let callKitID: UUID
@@ -23,20 +29,8 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
private let pushRegistry: PKPushRegistry
private let callController = CXCallController()
private let callProvider: CXProvider = {
let configuration = CXProviderConfiguration()
configuration.supportsVideo = true
configuration.includesCallsInRecents = true
if let callKitIcon = UIImage(named: "images/app-logo") {
configuration.iconTemplateImageData = callKitIcon.pngData()
}
// https://stackoverflow.com/a/46077628/730924
configuration.supportedHandleTypes = [.generic]
return CXProvider(configuration: configuration)
}()
private let callProvider: CXProviderProtocol
private let timeProvider: TimeProvider
private weak var clientProxy: ClientProxyProtocol? {
didSet {
@@ -72,15 +66,34 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
private var declineListenerHandle: TaskHandle?
override init() {
init(callProvider: CXProviderProtocol? = nil, timeProvider: TimeProvider? = nil) {
pushRegistry = PKPushRegistry(queue: nil)
self.timeProvider = timeProvider ?? TimeProvider(clock: ContinuousClock(), now: Date.init)
if let callProvider {
self.callProvider = callProvider
} else {
let configuration = CXProviderConfiguration()
configuration.supportsVideo = true
configuration.includesCallsInRecents = true
if let callKitIcon = UIImage(named: "images/app-logo") {
configuration.iconTemplateImageData = callKitIcon.pngData()
}
// https://stackoverflow.com/a/46077628/730924
configuration.supportedHandleTypes = [.generic]
self.callProvider = CXProvider(configuration: configuration)
}
super.init()
pushRegistry.delegate = self
pushRegistry.desiredPushTypes = [.voIP]
callProvider.setDelegate(self, queue: nil)
self.callProvider.setDelegate(self, queue: nil)
}
func setClientProxy(_ clientProxy: any ClientProxyProtocol) {
@@ -164,6 +177,20 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
let callID = CallID(callKitID: UUID(), roomID: roomID, rtcNotificationID: rtcNotificationID)
incomingCallID = callID
guard let expirationDate = (payload.dictionaryPayload[ElementCallServiceNotificationKey.expirationDate.rawValue] as? Date) else {
MXLog.error("Something went wrong, missing expiration timestamp for incoming voip call: \(payload)")
return
}
let nowDate = timeProvider.now()
guard nowDate < expirationDate else {
MXLog.warning("Call expired for room \(roomID), ignoring incoming push")
return
}
let ringDuration: Duration = .seconds(min(expirationDate.timeIntervalSince1970 - nowDate.timeIntervalSince1970, 90))
let roomDisplayName = payload.dictionaryPayload[ElementCallServiceNotificationKey.roomDisplayName.rawValue] as? String
let update = CXCallUpdate()
@@ -183,7 +210,7 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe
}
endUnansweredCallTask = Task { [weak self] in
try? await Task.sleep(for: .seconds(90))
try? await self?.timeProvider.clock.sleep(for: ringDuration)
guard let self, !Task.isCancelled else {
return
@@ -14,6 +14,8 @@ enum ElementCallServiceNotificationKey: String {
/// When an incoming call is set to ring, there will be a `m.rtc.notification`event (MSC4075).
/// Keep the notification event id as it is needed to decline calls (MSC4310).
case rtcNotifyEventID
/// The Date at which the incoming call should stop ringing.
case expirationDate
}
let ElementCallServiceNotificationDiscardDelta = 15.0