[MM-51997] Fix potential errors when accessing calls store (#22960)

* Fix potential errors when accessing calls store

* Fix typecheck
This commit is contained in:
Claudio Costa 2023-04-20 08:40:09 -06:00 committed by GitHub
parent c98c43456e
commit b5b4749da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 3 deletions

View File

@ -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) {

View File

@ -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);
});
});

View File

@ -74,7 +74,7 @@ export function getUsers(state: GlobalState): IDMappedObjects<UserProfile> {
export function getCalls(state: GlobalState): Record<string, UserProfile[]> {
// 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 {