From 0b984cc3eb25bea60a94e810dbff9f718c679157 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Thu, 8 Aug 2024 14:10:04 +0300 Subject: [PATCH] Fix #3133 - Cancel ElementCall rining once answered elsewhere - expose `activeRoomCallParticipants` and use them to detect if the user has joined the call from a different device --- .../Mocks/Generated/GeneratedMocks.swift | 1 + .../ElementCall/ElementCallService.swift | 19 ++++++++++++++----- .../Sources/Services/Room/RoomProxy.swift | 4 ++++ .../Services/Room/RoomProxyProtocol.swift | 3 +++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift index dc4c8680b..21d701720 100644 --- a/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift +++ b/ElementX/Sources/Mocks/Generated/GeneratedMocks.swift @@ -8359,6 +8359,7 @@ class RoomProxyMock: RoomProxyProtocol { set(value) { underlyingHasOngoingCall = value } } var underlyingHasOngoingCall: Bool! + var activeRoomCallParticipants: [String] = [] var canonicalAlias: String? var ownUserID: String { get { return underlyingOwnUserID } diff --git a/ElementX/Sources/Services/ElementCall/ElementCallService.swift b/ElementX/Sources/Services/ElementCall/ElementCallService.swift index 2d993ab98..2390a3d0a 100644 --- a/ElementX/Sources/Services/ElementCall/ElementCallService.swift +++ b/ElementX/Sources/Services/ElementCall/ElementCallService.swift @@ -288,22 +288,31 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe roomProxy .actionsPublisher - .map { action in + .map { action -> (Bool, [String]) in switch action { case .roomInfoUpdate: - return roomProxy.hasOngoingCall + return (roomProxy.hasOngoingCall, roomProxy.activeRoomCallParticipants) } } - .removeDuplicates() + .removeDuplicates { $0 == $1 } .dropFirst(isCallOngoing ? 0 : 1) - .sink { [weak self] hasOngoingCall in + .sink { [weak self] hasOngoingCall, activeRoomCallParticipants in guard let self else { return } + let participants: [String] = activeRoomCallParticipants + if !hasOngoingCall { - MXLog.info("Call has been cancelled") + MXLog.info("Call cancelled by remote") + cancellables.removeAll() endUnansweredCallTask?.cancel() callProvider.reportCall(with: incomingCallID.callKitID, endedAt: nil, reason: .remoteEnded) + } else if participants.contains(roomProxy.ownUserID) { + MXLog.info("Call anwered elsewhere") + + cancellables.removeAll() + endUnansweredCallTask?.cancel() + callProvider.reportCall(with: incomingCallID.callKitID, endedAt: nil, reason: .answeredElsewhere) } } .store(in: &cancellables) diff --git a/ElementX/Sources/Services/Room/RoomProxy.swift b/ElementX/Sources/Services/Room/RoomProxy.swift index 7067d175f..349ec8a7d 100644 --- a/ElementX/Sources/Services/Room/RoomProxy.swift +++ b/ElementX/Sources/Services/Room/RoomProxy.swift @@ -123,6 +123,10 @@ class RoomProxy: RoomProxyProtocol { room.hasActiveRoomCall() } + var activeRoomCallParticipants: [String] { + room.activeRoomCallParticipants() + } + var canonicalAlias: String? { room.canonicalAlias() } diff --git a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift index df42c9241..6f95a01d2 100644 --- a/ElementX/Sources/Services/Room/RoomProxyProtocol.swift +++ b/ElementX/Sources/Services/Room/RoomProxyProtocol.swift @@ -41,7 +41,10 @@ protocol RoomProxyProtocol { var pinnedEventIDs: Set { get async } var membership: Membership { get } var inviter: RoomMemberProxyProtocol? { get async } + var hasOngoingCall: Bool { get } + var activeRoomCallParticipants: [String] { get } + var canonicalAlias: String? { get } var ownUserID: String { get }