From b5b4749da531b518fb8bf5adb1373ab6b127d6c3 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Thu, 20 Apr 2023 08:40:09 -0600 Subject: [PATCH] [MM-51997] Fix potential errors when accessing calls store (#22960) * Fix potential errors when accessing calls store * Fix typecheck --- .../src/components/profile_popover/index.ts | 4 +- .../profile_popover/profile_popover.test.tsx | 50 +++++++++++++++++++ .../src/selectors/entities/common.ts | 2 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/webapp/channels/src/components/profile_popover/index.ts b/webapp/channels/src/components/profile_popover/index.ts index 0af4f863f1..86c77f0203 100644 --- a/webapp/channels/src/components/profile_popover/index.ts +++ b/webapp/channels/src/components/profile_popover/index.ts @@ -50,12 +50,12 @@ function getDefaultChannelId(state: GlobalState) { return selectedPost.exists ? selectedPost.channel_id : getCurrentChannelId(state); } -function checkUserInCall(state: GlobalState, userId: string) { +export function checkUserInCall(state: GlobalState, userId: string) { let isUserInCall = false; const calls = getCalls(state); Object.keys(calls).forEach((channelId) => { - const usersInCall = calls[channelId]; + const usersInCall = calls[channelId] || []; for (const user of usersInCall) { if (user.id === userId) { diff --git a/webapp/channels/src/components/profile_popover/profile_popover.test.tsx b/webapp/channels/src/components/profile_popover/profile_popover.test.tsx index 8e3a42c8cd..75eab71c80 100644 --- a/webapp/channels/src/components/profile_popover/profile_popover.test.tsx +++ b/webapp/channels/src/components/profile_popover/profile_popover.test.tsx @@ -9,6 +9,7 @@ import {General} from 'mattermost-redux/constants'; import {CustomStatusDuration} from '@mattermost/types/users'; import ProfilePopover from 'components/profile_popover/profile_popover'; +import {checkUserInCall} from 'components/profile_popover'; import Pluggable from 'plugins/pluggable'; @@ -284,3 +285,52 @@ describe('components/ProfilePopover', () => { expect(wrapper).toMatchSnapshot(); }); }); + +describe('checkUserInCall', () => { + test('missing state', () => { + expect(checkUserInCall({ + 'plugins-com.mattermost.calls': {}, + } as any, 'userA')).toBe(false); + }); + + test('call state missing', () => { + expect(checkUserInCall({ + 'plugins-com.mattermost.calls': { + voiceConnectedProfiles: { + channelID: null, + }, + }, + } as any, 'userA')).toBe(false); + }); + + test('user not in call', () => { + expect(checkUserInCall({ + 'plugins-com.mattermost.calls': { + voiceConnectedProfiles: { + channelID: [ + { + id: 'userB', + }, + ], + }, + }, + } as any, 'userA')).toBe(false); + }); + + test('user in call', () => { + expect(checkUserInCall({ + 'plugins-com.mattermost.calls': { + voiceConnectedProfiles: { + channelID: [ + { + id: 'userB', + }, + { + id: 'userA', + }, + ], + }, + }, + } as any, 'userA')).toBe(true); + }); +}); diff --git a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/common.ts b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/common.ts index b26a42976c..df49a50724 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/common.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/selectors/entities/common.ts @@ -74,7 +74,7 @@ export function getUsers(state: GlobalState): IDMappedObjects { export function getCalls(state: GlobalState): Record { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - return state[CALLS_PLUGIN].voiceConnectedProfiles; + return state[CALLS_PLUGIN].voiceConnectedProfiles || {}; } export function getCallsConfig(state: GlobalState): CallsConfig {