Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(session): prevent duplication of session participants #1201

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/client/src/store/CallState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,13 +1047,33 @@ export class CallState {
return session;
}
const { participants, participants_count_by_role } = session;
const { user } = event.participant;
const { user, user_session_id } = event.participant;
// It could happen that the backend delivers the same participant more than once.
// Once with the call.session_started event and once again with the
// call.session_participant_joined event. In this case,
// we should update the existing participant and prevent duplicating it.
let shouldInsertParticipant = true;
const updatedParticipants = participants.map((p) => {
if (p.user_session_id === user_session_id) {
shouldInsertParticipant = false;
return event.participant;
}
return p;
});
if (shouldInsertParticipant) {
// this is a new array, we can safely push the new participant
updatedParticipants.push(event.participant);
}

// If we are updating an existing participant, we don't want to increment
// the participant_by_role count.
const increment = shouldInsertParticipant ? 1 : 0;
return {
...session,
participants: [...participants, event.participant],
participants: updatedParticipants,
participants_count_by_role: {
...participants_count_by_role,
[user.role]: (participants_count_by_role[user.role] || 0) + 1,
[user.role]: (participants_count_by_role[user.role] || 0) + increment,
},
};
});
Expand Down
50 changes: 50 additions & 0 deletions packages/client/src/store/__tests__/CallState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,56 @@ describe('CallState', () => {
},
});
});

it('should update existing participant', () => {
const state = new CallState();
state.updateFromCallResponse({
// @ts-ignore
session: {
participants: [
{
// @ts-ignore
user: {
id: 'user-id',
role: 'user',
},
user_session_id: '123',
},
],
participants_count_by_role: {
user: 1,
},
},
});
state.updateFromEvent({
type: 'call.session_participant_joined',
participant: {
// @ts-ignore
user: {
id: 'user-id',
role: 'user',
name: 'Updated user',
},
user_session_id: '123',
},
});
expect(state.session).toEqual({
participants: [
{
// @ts-ignore
user: {
id: 'user-id',
role: 'user',
name: 'Updated user',
},
user_session_id: '123',
},
],
participants_count_by_role: {
user: 1,
},
});
});
});
});
});
Loading