Skip to content

Commit

Permalink
Improve incoming call reject logic (#1059)
Browse files Browse the repository at this point in the history
* Improve call reject logic in CallService

* Refactor accept method to align with new reject method

* Improve comments

---------

Co-authored-by: Aleksandar Apostolov <[email protected]>
  • Loading branch information
liviu-timar and aleksandar-apostolov authored Apr 10, 2024
1 parent 3cae8bf commit c074e00
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,19 @@ internal class CallService : Service() {
logger.i { "Received event in service: $event" }
when (event) {
is CallAcceptedEvent -> {
handleCallAcceptedOnAnotherDevice(
stopServiceIfCallAcceptedByMeOnAnotherDevice(
acceptedByUserId = event.user.id,
myUserId = streamVideo.userId,
callRingingState = call.state.ringingState.value,
)
}

is CallRejectedEvent -> {
// When call is rejected by the caller
stopService()
stopServiceIfCallRejectedByMeOrCaller(
rejectedByUserId = event.user.id,
myUserId = streamVideo.userId,
createdByUserId = call.state.createdBy.value?.id,
)
}

is CallEndedEvent -> {
Expand Down Expand Up @@ -390,16 +394,21 @@ internal class CallService : Service() {
}
}

private fun handleCallAcceptedOnAnotherDevice(acceptedByUserId: String, callRingingState: RingingState) {
val myUserId = StreamVideo.instanceOrNull()?.userId

// If call was accepted by me, but current device is still ringing, it means the call was accepted on another device
private fun stopServiceIfCallAcceptedByMeOnAnotherDevice(acceptedByUserId: String, myUserId: String, callRingingState: RingingState) {
// If incoming call was accepted by me, but current device is still ringing, it means the call was accepted on another device
if (acceptedByUserId == myUserId && callRingingState is RingingState.Incoming) {
// So stop ringing on this device
stopService()
}
}

private fun stopServiceIfCallRejectedByMeOrCaller(rejectedByUserId: String, myUserId: String, createdByUserId: String?) {
// If incoming call is rejected by me (even on another device) OR cancelled by the caller, stop the service
if (rejectedByUserId == myUserId || rejectedByUserId == createdByUserId) {
stopService()
}
}

@OptIn(DelicateCoroutinesApi::class)
private fun initializeCallAndSocket(
streamVideo: StreamVideo,
Expand Down

0 comments on commit c074e00

Please sign in to comment.