diff --git a/app/products/calls/actions/calls.test.ts b/app/products/calls/actions/calls.test.ts index 492a55320d..a279e4aa6e 100644 --- a/app/products/calls/actions/calls.test.ts +++ b/app/products/calls/actions/calls.test.ts @@ -19,6 +19,7 @@ import { useCallsState, useChannelsWithCalls, useCurrentCall, + userJoinedCall, } from '@calls/state'; import { Call, @@ -88,6 +89,7 @@ const addFakeCall = (serverUrl: string, channelId: string) => { ownerId: 'xohi8cki9787fgiryne716u84o', } as Call; act(() => { + State.setCallsState(serverUrl, {serverUrl, myUserId: 'myUserId', calls: {}, enabled: {}}); State.callStarted(serverUrl, call); }); }; @@ -138,6 +140,7 @@ describe('Actions.Calls', () => { let response: { data?: string }; await act(async () => { response = await CallsActions.joinCall('server1', 'channel-id'); + userJoinedCall('server1', 'channel-id', 'myUserId'); }); assert.equal(response!.data, 'channel-id'); @@ -161,6 +164,7 @@ describe('Actions.Calls', () => { let response: { data?: string }; await act(async () => { response = await CallsActions.joinCall('server1', 'channel-id'); + userJoinedCall('server1', 'channel-id', 'myUserId'); }); assert.equal(response!.data, 'channel-id'); assert.equal((result.current[1] as CurrentCall | null)?.channelId, 'channel-id'); @@ -188,6 +192,7 @@ describe('Actions.Calls', () => { let response: { data?: string }; await act(async () => { response = await CallsActions.joinCall('server1', 'channel-id'); + userJoinedCall('server1', 'channel-id', 'myUserId'); }); assert.equal(response!.data, 'channel-id'); assert.equal((result.current[1] as CurrentCall | null)?.channelId, 'channel-id'); @@ -214,6 +219,7 @@ describe('Actions.Calls', () => { let response: { data?: string }; await act(async () => { response = await CallsActions.joinCall('server1', 'channel-id'); + userJoinedCall('server1', 'channel-id', 'myUserId'); }); assert.equal(response!.data, 'channel-id'); assert.equal((result.current[1] as CurrentCall | null)?.channelId, 'channel-id'); diff --git a/app/products/calls/actions/calls.ts b/app/products/calls/actions/calls.ts index da917560b1..9f9d27b12e 100644 --- a/app/products/calls/actions/calls.ts +++ b/app/products/calls/actions/calls.ts @@ -7,7 +7,6 @@ import {forceLogoutIfNecessary} from '@actions/remote/session'; import {fetchUsersByIds} from '@actions/remote/user'; import { getCallsConfig, getCallsState, - myselfJoinedCall, myselfLeftCall, setCalls, setChannelEnabled, @@ -201,7 +200,6 @@ export const joinCall = async (serverUrl: string, channelId: string): Promise<{ try { await connection.waitForReady(); - myselfJoinedCall(serverUrl, channelId); return {data: channelId}; } catch (e) { connection.disconnect(); diff --git a/app/products/calls/state/actions.test.ts b/app/products/calls/state/actions.test.ts index 6f2c15f85e..a6cfb1190d 100644 --- a/app/products/calls/state/actions.test.ts +++ b/app/products/calls/state/actions.test.ts @@ -24,7 +24,6 @@ import { setCallScreenOn, setCallScreenOff, setRaisedHand, - myselfJoinedCall, myselfLeftCall, setChannelEnabled, setScreenShareURL, @@ -460,12 +459,25 @@ describe('useCallsState', () => { myUserId: 'myUserId', calls: {'channel-1': call1, 'channel-2': call2}, }; + const newCall1 = { + ...call1, + participants: { + ...call1.participants, + myUserId: {id: 'myUserId', muted: true, raisedHand: 0}, + }, + }; + const expectedCallsState = { + ...initialCallsState, + calls: {...initialCallsState.calls, + 'channel-1': newCall1, + }, + }; const expectedCurrentCallState = { serverUrl: 'server1', myUserId: 'myUserId', screenShareURL: '', speakerphoneOn: false, - ...call1, + ...newCall1, } as CurrentCall; // setup @@ -477,10 +489,14 @@ describe('useCallsState', () => { assert.deepEqual(result.current[1], null); // test - act(() => myselfJoinedCall('server1', 'channel-1')); - assert.deepEqual(result.current[0], initialCallsState); + act(() => userJoinedCall('server1', 'channel-1', 'myUserId')); + assert.deepEqual(result.current[0], expectedCallsState); assert.deepEqual(result.current[1], expectedCurrentCallState); - act(() => myselfLeftCall()); + + act(() => { + myselfLeftCall(); + userLeftCall('server1', 'channel-1', 'myUserId'); + }); assert.deepEqual(result.current[0], initialCallsState); assert.deepEqual(result.current[1], null); }); @@ -543,13 +559,14 @@ describe('useCallsState', () => { assert.deepEqual(result.current[1], null); // test joining a call and setting url: - act(() => myselfJoinedCall('server1', 'channel-1')); + act(() => userJoinedCall('server1', 'channel-1', 'myUserId')); assert.deepEqual((result.current[1] as CurrentCall | null)?.screenShareURL, ''); act(() => setScreenShareURL('testUrl')); assert.deepEqual((result.current[1] as CurrentCall | null)?.screenShareURL, 'testUrl'); act(() => { myselfLeftCall(); + userLeftCall('server1', 'channel-1', 'myUserId'); setScreenShareURL('test'); }); assert.deepEqual(result.current[0], initialCallsState); @@ -562,6 +579,19 @@ describe('useCallsState', () => { myUserId: 'myUserId', calls: {'channel-1': call1, 'channel-2': call2}, }; + const newCall1 = { + ...call1, + participants: { + ...call1.participants, + myUserId: {id: 'myUserId', muted: true, raisedHand: 0}, + }, + }; + const expectedCallsState = { + ...initialCallsState, + calls: {...initialCallsState.calls, + 'channel-1': newCall1, + }, + }; // setup const {result} = renderHook(() => { @@ -572,18 +602,18 @@ describe('useCallsState', () => { assert.deepEqual(result.current[1], null); // test - act(() => myselfJoinedCall('server1', 'channel-1')); + act(() => userJoinedCall('server1', 'channel-1', 'myUserId')); assert.deepEqual((result.current[1] as CurrentCall | null)?.speakerphoneOn, false); act(() => setSpeakerPhone(true)); assert.deepEqual((result.current[1] as CurrentCall | null)?.speakerphoneOn, true); act(() => setSpeakerPhone(false)); assert.deepEqual((result.current[1] as CurrentCall | null)?.speakerphoneOn, false); - assert.deepEqual(result.current[0], initialCallsState); + assert.deepEqual(result.current[0], expectedCallsState); act(() => { myselfLeftCall(); setSpeakerPhone(true); }); - assert.deepEqual(result.current[0], initialCallsState); + assert.deepEqual(result.current[0], expectedCallsState); assert.deepEqual(result.current[1], null); }); diff --git a/app/products/calls/state/actions.ts b/app/products/calls/state/actions.ts index 35a16a702d..91e49f4e78 100644 --- a/app/products/calls/state/actions.ts +++ b/app/products/calls/state/actions.ts @@ -45,15 +45,25 @@ export const userJoinedCall = (serverUrl: string, channelId: string, userId: str // Did the user join the current call? If so, update that too. const currentCall = getCurrentCall(); - if (!currentCall || currentCall.channelId !== channelId) { - return; + if (currentCall && currentCall.channelId === channelId) { + const nextCall2 = { + ...currentCall, + participants: {...currentCall.participants, [userId]: nextCall.participants[userId]}, + }; + setCurrentCall(nextCall2); } - const nextCall2 = { - ...currentCall, - participants: {...currentCall.participants, [userId]: nextCall.participants[userId]}, - }; - setCurrentCall(nextCall2); + // Was it me that joined the call? + if (callsState.myUserId === userId) { + setCurrentCall({ + ...nextCall, + participants: {...nextCall.participants}, + serverUrl, + myUserId: userId, + screenShareURL: '', + speakerphoneOn: false, + }); + } }; export const userLeftCall = (serverUrl: string, channelId: string, userId: string) => { @@ -95,21 +105,6 @@ export const userLeftCall = (serverUrl: string, channelId: string, userId: strin setCurrentCall(nextCall2); }; -export const myselfJoinedCall = (serverUrl: string, channelId: string) => { - const callsState = getCallsState(serverUrl); - - const participants = callsState.calls[channelId]?.participants || {}; - setCurrentCall({ - ...callsState.calls[channelId], - serverUrl, - myUserId: callsState.myUserId, - participants, - channelId, - screenShareURL: '', - speakerphoneOn: false, - }); -}; - export const myselfLeftCall = () => { setCurrentCall(null); }; @@ -122,18 +117,6 @@ export const callStarted = (serverUrl: string, call: Call) => { const nextChannelsWithCalls = {...getChannelsWithCalls(serverUrl), [call.channelId]: true}; setChannelsWithCalls(serverUrl, nextChannelsWithCalls); - - // Was it the current call? If so, we started it, and need to fill in the currentCall's details. - const currentCall = getCurrentCall(); - if (!currentCall || currentCall.channelId !== call.channelId) { - return; - } - - const nextCurrentCall = { - ...currentCall, - ...call, - }; - setCurrentCall(nextCurrentCall); }; export const callEnded = (serverUrl: string, channelId: string) => {