Skip to content

Commit

Permalink
Handle messaging self (zulip#250)
Browse files Browse the repository at this point in the history
Fixes zulip#231
  • Loading branch information
borisyankov authored Jan 29, 2017
1 parent 7c17f4f commit d8faae9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
30 changes: 30 additions & 0 deletions src/chat/__tests__/chatReducers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ describe('chatReducers', () => {
});
});

test('Message sent to self is stored correctly', () => {
const narrowWithSelfStr = JSON.stringify(privateNarrow('[email protected]'));
const initialState = {
messages: {
[homeNarrowStr]: [],
[narrowWithSelfStr]: [],
}
};
const message = {
id: 1,
display_recipient: [{ email: '[email protected]' }]
};
const action = {
type: EVENT_NEW_MESSAGE,
selfEmail: '[email protected]',
message,
};
const expectedState = {
messages: {
[homeNarrowStr]: [message],
[narrowWithSelfStr]: [message],
}
};

const newState = chatReducers(initialState, action);

expect(newState).toEqual(expectedState);
expect(newState).not.toBe(initialState);
});

test('appends stream message to all cached narrows that match', () => {
const initialState = {
messages: {
Expand Down
4 changes: 3 additions & 1 deletion src/message/headers/MessageHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export default class MessageHeader extends React.PureComponent {

if (item.type === 'private' &&
!isPrivateNarrow(narrow) && !isGroupNarrow(narrow) && !isTopicNarrow(narrow)) {
const recipients = item.display_recipient.filter(r => r.email !== auth.email);
const recipients = item.display_recipient.length > 1 ?
item.display_recipient.filter(r => r.email !== auth.email) :
item.display_recipient;
return (
<PrivateMessageHeader
key={`section_${item.id}`}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/__tests__/narrow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ describe('isMessageInNarrow', () => {
expect(isMessageInNarrow(message, narrow, '[email protected]')).toBe(true);
});

test('message to self is in "private" narrow with self', () => {
const message = {
type: 'private',
display_recipient: [
{ email: '[email protected]' },
],
};
const narrow = privateNarrow('[email protected]');

expect(isMessageInNarrow(message, narrow, '[email protected]')).toBe(true);
});

test('message with type "private" is in group narrow if all recipients match ', () => {
const message = {
type: 'private',
Expand Down
11 changes: 6 additions & 5 deletions src/utils/narrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ export const isMessageInNarrow = (message: Message, narrow: Narrow, selfEmail: s
return true;
}

if ((isPrivateNarrow(narrow) || isGroupNarrow(narrow)) &&
normalizeRecipients(message.display_recipient) ===
[...narrow[0].operand.split(','), selfEmail].sort().join(',')
) {
return true;

if (isPrivateNarrow(narrow) || isGroupNarrow(narrow)) {
const normalizedRecipients = normalizeRecipients(message.display_recipient);
const normalizedNarrow = [...narrow[0].operand.split(','), selfEmail].sort().join(',');

return normalizedRecipients === selfEmail || normalizedRecipients === normalizedNarrow;
}

if (isSpecialNarrow(narrow) &&
Expand Down

0 comments on commit d8faae9

Please sign in to comment.