Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid narrow. #1527

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`messageActions doNarrow if newNarrow stream is not valid, do nothing 1`] = `Array []`;

exports[`messageActions doNarrow if newNarrow user is deactivated, do nothing 1`] = `Array []`;

exports[`messageActions doNarrow when messages in new narrow, only action to switch narrow is dispatched 1`] = `
Array [
Object {
57 changes: 56 additions & 1 deletion src/message/__tests__/messageActions-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mockStore from 'redux-mock-store'; // eslint-disable-line

import { doNarrow } from '../messagesActions';
import { streamNarrow, homeNarrow } from '../../utils/narrow';
import { streamNarrow, homeNarrow, privateNarrow } from '../../utils/narrow';

const narrow = streamNarrow('some stream');
const streamNarrowStr = JSON.stringify(narrow);
@@ -20,6 +20,11 @@ describe('messageActions', () => {
narrow: homeNarrow,
messages: {},
},
streams: [
{
name: 'some stream',
},
],
});

store.dispatch(doNarrow(narrow));
@@ -38,6 +43,11 @@ describe('messageActions', () => {
narrow: homeNarrow,
messages: {},
},
streams: [
{
name: 'some stream',
},
],
});

store.dispatch(doNarrow(narrow));
@@ -53,10 +63,55 @@ describe('messageActions', () => {
[streamNarrowStr]: [{ id: 1 }],
},
},
streams: [
{
name: 'some stream',
},
],
});

store.dispatch(doNarrow(narrow));
expect(store.getActions()).toMatchSnapshot();
});

test('if newNarrow stream is not valid, do nothing', () => {
const store = mockStore({
caughtUp: {},
chat: {
narrow: homeNarrow,
messages: {
[streamNarrowStr]: [{ id: 1 }],
},
},
streams: [
{
name: 'some updated stream',
},
],
});

store.dispatch(doNarrow(narrow));
expect(store.getActions()).toMatchSnapshot();
});

test('if newNarrow user is deactivated, do nothing', () => {
const store = mockStore({
caughtUp: {},
chat: {
narrow: homeNarrow,
messages: {
[streamNarrowStr]: [{ id: 1 }],
},
},
users: [
{
email: 'ab@a.com',
},
],
});

store.dispatch(doNarrow(privateNarrow('a@a.com')));
expect(store.getActions()).toMatchSnapshot();
});
});
});
11 changes: 10 additions & 1 deletion src/message/messagesActions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* @flow */
import type { Action, Narrow, Dispatch, GetState } from '../types';
import { NULL_CAUGHTUP } from '../nullObjects';
import { getAuth, getUsers, getAllMessages } from '../selectors';
import { getAuth, getUsers, getAllMessages, getStreams } from '../selectors';
import { SWITCH_NARROW } from '../actionConstants';
import { getMessageIdFromLink, getNarrowFromLink, isUrlInAppLink, getFullUrl } from '../utils/url';
import openLink from '../utils/openLink';
import { fetchMessagesAtFirstUnread } from './fetchActions';
import { validateNarrow } from '../utils/narrow';
// import showToast from '../utils/showToast';

export const switchNarrow = (narrow: Narrow): Action => ({
type: SWITCH_NARROW,
@@ -16,6 +18,13 @@ export const doNarrow = (newNarrow: Narrow, anchor: number = Number.MAX_SAFE_INT
dispatch: Dispatch,
getState: GetState,
) => {
const isValidNarrow = validateNarrow(newNarrow, getStreams(getState()), getUsers(getState()));

if (!isValidNarrow) {
// show message to user that narrow is outdated now
// showToast('Invalid narrow');
return;
}
dispatch(switchNarrow(newNarrow));

const anyMessagesInNewNarrow = JSON.stringify(newNarrow) in getAllMessages(getState());
13 changes: 12 additions & 1 deletion src/utils/narrow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow */
import type { Narrow, Message } from '../types';
import type { Narrow, Message, Stream, User } from '../types';
import { normalizeRecipients } from './message';

export const homeNarrow: Narrow = [];
@@ -137,3 +137,14 @@ export const getNarrowFromMessage = (message: Message, email: string) => {

return streamNarrow(message.display_recipient);
};

export const validateNarrow = (narrow: Narrow, streams: Stream[], users: User[]): boolean => {
if (isStreamOrTopicNarrow(narrow)) {
// check if stream is not outdated
return streams && streams.find(s => s.name === narrow[0].operand) !== undefined;
} else if (isPrivateNarrow(narrow)) {
// check user account is not deactivited
return users && users.find(u => u.email === narrow[0].operand) !== undefined;
}
return isSpecialNarrow(narrow) || isHomeNarrow(narrow);
};