Improve handling of invalid conversations

This commit is contained in:
trevor-signal 2023-05-19 09:28:59 -04:00 committed by GitHub
parent 4404429ec2
commit 36432b3a3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 6 deletions

View file

@ -5459,6 +5459,16 @@ export function reducer(
const { payload } = action; const { payload } = action;
const { conversationId, messageId, switchToAssociatedView } = payload; const { conversationId, messageId, switchToAssociatedView } = payload;
let conversation: ConversationType | undefined;
if (conversationId) {
conversation = getOwn(state.conversationLookup, conversationId);
if (!conversation) {
log.error(`Unknown conversation selected, id: [${conversationId}]`);
return state;
}
}
const nextState = { const nextState = {
...omit(state, 'contactSpoofingReview'), ...omit(state, 'contactSpoofingReview'),
selectedConversationId: conversationId, selectedConversationId: conversationId,
@ -5466,11 +5476,7 @@ export function reducer(
targetedMessageSource: TargetedMessageSource.NavigateToMessage, targetedMessageSource: TargetedMessageSource.NavigateToMessage,
}; };
if (switchToAssociatedView && conversationId) { if (switchToAssociatedView && conversation) {
const conversation = getOwn(state.conversationLookup, conversationId);
if (!conversation) {
return nextState;
}
return { return {
...omit(nextState, 'composer', 'selectedMessageIds'), ...omit(nextState, 'composer', 'selectedMessageIds'),
showArchived: Boolean(conversation.isArchived), showArchived: Boolean(conversation.isArchived),

View file

@ -372,7 +372,7 @@ describe('both/state/ducks/conversations', () => {
} }
describe('showConversation', () => { describe('showConversation', () => {
it('selects a conversation id', () => { it('does not select a conversation if it does not exist', () => {
const state = { const state = {
...getEmptyState(), ...getEmptyState(),
}; };
@ -385,14 +385,44 @@ describe('both/state/ducks/conversations', () => {
const action = dispatch.getCall(0).args[0]; const action = dispatch.getCall(0).args[0];
const nextState = reducer(state, action); const nextState = reducer(state, action);
assert.isUndefined(nextState.selectedConversationId);
assert.isUndefined(nextState.targetedMessage);
});
it('selects a conversation id', () => {
const conversation = getDefaultConversation({
id: 'abc123',
});
const state = {
...getEmptyState(),
conversationLookup: {
[conversation.id]: conversation,
},
};
const dispatch = sinon.spy();
showConversation({ conversationId: 'abc123' })(
dispatch,
getEmptyRootState,
null
);
const action = dispatch.getCall(0).args[0];
const nextState = reducer(state, action);
assert.equal(nextState.selectedConversationId, 'abc123'); assert.equal(nextState.selectedConversationId, 'abc123');
assert.isUndefined(nextState.targetedMessage); assert.isUndefined(nextState.targetedMessage);
}); });
it('selects a conversation and a message', () => { it('selects a conversation and a message', () => {
const conversation = getDefaultConversation({
id: 'abc123',
});
const state = { const state = {
...getEmptyState(), ...getEmptyState(),
conversationLookup: {
[conversation.id]: conversation,
},
}; };
const dispatch = sinon.spy(); const dispatch = sinon.spy();
showConversation({ showConversation({
conversationId: 'abc123', conversationId: 'abc123',