Skip to content

Commit

Permalink
Improve handling of invalid conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-signal committed May 19, 2023
1 parent 4404429 commit 36432b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ts/state/ducks/conversations.ts
Expand Up @@ -5459,18 +5459,24 @@ export function reducer(
const { payload } = action;
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 = {
...omit(state, 'contactSpoofingReview'),
selectedConversationId: conversationId,
targetedMessage: messageId,
targetedMessageSource: TargetedMessageSource.NavigateToMessage,
};

if (switchToAssociatedView && conversationId) {
const conversation = getOwn(state.conversationLookup, conversationId);
if (!conversation) {
return nextState;
}
if (switchToAssociatedView && conversation) {
return {
...omit(nextState, 'composer', 'selectedMessageIds'),
showArchived: Boolean(conversation.isArchived),
Expand Down
30 changes: 30 additions & 0 deletions ts/test-electron/state/ducks/conversations_test.ts
Expand Up @@ -372,9 +372,32 @@ describe('both/state/ducks/conversations', () => {
}

describe('showConversation', () => {
it('does not select a conversation if it does not exist', () => {
const state = {
...getEmptyState(),
};
const dispatch = sinon.spy();
showConversation({ conversationId: 'abc123' })(
dispatch,
getEmptyRootState,
null
);
const action = dispatch.getCall(0).args[0];
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' })(
Expand All @@ -390,9 +413,16 @@ describe('both/state/ducks/conversations', () => {
});

it('selects a conversation and a message', () => {
const conversation = getDefaultConversation({
id: 'abc123',
});
const state = {
...getEmptyState(),
conversationLookup: {
[conversation.id]: conversation,
},
};

const dispatch = sinon.spy();
showConversation({
conversationId: 'abc123',
Expand Down

0 comments on commit 36432b3

Please sign in to comment.