Skip to content

Commit

Permalink
Fetch realm_filters and its updates (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunall17 authored and borisyankov committed Aug 1, 2017
1 parent 5cda051 commit e2999d7
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/actionConstants.js
Expand Up @@ -26,6 +26,7 @@ export const INIT_STREAMS = 'INIT_STREAMS';
export const INIT_SUBSCRIPTIONS = 'INIT_SUBSCRIPTIONS';
export const INIT_USERS = 'INIT_USERS';
export const INIT_REALM_EMOJI = 'INIT_REALM_EMOJI';
export const INIT_REALM_FILTER = 'INIT_REALM_FILTER';

export const EVENT_REGISTERED = 'EVENT_REGISTERED';
export const EVENT_NEW_MESSAGE = 'EVENT_NEW_MESSAGE';
Expand Down Expand Up @@ -62,6 +63,7 @@ export const SAVE_TOKEN_PUSH = 'SAVE_TOKEN_PUSH';
export const DELETE_TOKEN_PUSH = 'DELETE_TOKEN_PUSH';

export const EVENT_REALM_EMOJI_UPDATE = 'EVENT_REALM_EMOJI_UPDATE';
export const EVENT_REALM_FILTER_UPDATE = 'EVENT_REALM_FILTER_UPDATE';

export const EVENT_UPDATE_DISPLAY_SETTINGS = 'EVENT_UPDATE_DISPLAY_SETTINGS';
export const EVENT_UPDATE_GLOBAL_NOTIFICATIONS_SETTINGS =
Expand Down
5 changes: 5 additions & 0 deletions src/api/getRealmFilters.js
@@ -0,0 +1,5 @@
/* @flow */
import type { Auth } from '../types';
import { apiGet } from './apiFetch';

export default async (auth: Auth) => apiGet(auth, 'realm/filters', res => res.filters);
1 change: 1 addition & 0 deletions src/api/index.js
Expand Up @@ -22,3 +22,4 @@ export { default as subscriptionRemove } from './subscriptionRemove';
export { default as sendMessage } from './sendMessage';
export { default as uploadFile } from './uploadFile';
export { default as getSingleMessage } from './getSingleMessage';
export { default as getRealmFilters } from './getRealmFilters';
1 change: 1 addition & 0 deletions src/api/registerForEvents.js
Expand Up @@ -11,6 +11,7 @@ export default (auth: Auth) =>
'presence',
'reaction',
'realm_emoji',
'realm_filters',
'realm_user',
'stream',
'subscription',
Expand Down
6 changes: 6 additions & 0 deletions src/events/eventToAction.js
Expand Up @@ -25,6 +25,7 @@ import {
EVENT_REALM_EMOJI_UPDATE,
EVENT_UPDATE_GLOBAL_NOTIFICATIONS_SETTINGS,
EVENT_UPDATE_DISPLAY_SETTINGS,
EVENT_REALM_FILTER_UPDATE,
} from '../actionConstants';

import { getUserById } from '../selectors';
Expand Down Expand Up @@ -141,6 +142,11 @@ export default (state: GlobalState, event: Object) => {
...event,
type: EVENT_REALM_EMOJI_UPDATE,
};
case 'realm_filters':
return {
...event,
type: EVENT_REALM_FILTER_UPDATE,
};
case 'update_global_notifications':
return {
...event,
Expand Down
34 changes: 34 additions & 0 deletions src/realm/__tests__/realmReducers-test.js
Expand Up @@ -7,6 +7,7 @@ import {
EVENT_UPDATE_DISPLAY_SETTINGS,
SAVE_TOKEN_PUSH,
DELETE_TOKEN_PUSH,
EVENT_REALM_FILTER_UPDATE,
} from '../../actionConstants';

describe('realmReducers', () => {
Expand All @@ -22,6 +23,7 @@ describe('realmReducers', () => {
twentyFourHourTime: false,
pushToken: '',
emoji: {},
filter: [],
};

const actualState = realmReducers(initialState, action);
Expand Down Expand Up @@ -114,6 +116,7 @@ describe('realmReducers', () => {
twentyFourHourTime: false,
pushToken: 'key',
emoji: {},
filter: [],
});

const action = deepFreeze({
Expand All @@ -134,6 +137,37 @@ describe('realmReducers', () => {
customEmoji1: {},
customEmoji2: {},
},
filter: [],
};

const newState = realmReducers(prevState, action);

expect(newState).toEqual(expectedState);
});
});

describe('EVENT_REALM_FILTER_UPDATE', () => {
test('update state to new realm_filter', () => {
const prevState = deepFreeze({
twentyFourHourTime: false,
pushToken: 'key',
emoji: {},
filter: [],
});

const action = deepFreeze({
eventId: 4,
id: 4,
op: 'update',
realm_filters: [['#(?P<id>[0-9]+)', 'https://github.com/zulip/zulip/issues/%(id)s', 2]],
type: EVENT_REALM_FILTER_UPDATE,
});

const expectedState = {
twentyFourHourTime: false,
pushToken: 'key',
emoji: {},
filter: [['#(?P<id>[0-9]+)', 'https://github.com/zulip/zulip/issues/%(id)s', 2]],
};

const newState = realmReducers(prevState, action);
Expand Down
14 changes: 12 additions & 2 deletions src/realm/realmActions.js
Expand Up @@ -2,11 +2,19 @@
import type { GetState, Dispatch, Action } from '../types';
import { specialNarrow } from '../utils/narrow';
import { tryUntilSuccessful } from '../utils/async';
import { getSubscriptions, getMessages, getStreams, getUsers, getRealmEmojis } from '../api';
import {
getSubscriptions,
getMessages,
getStreams,
getUsers,
getRealmEmojis,
getRealmFilters,
} from '../api';
import { refreshNotificationToken } from '../utils/notifications';
import { messageFetchSuccess } from '../message/messagesActions';
import { initSubscriptions } from '../subscriptions/subscriptionsActions';
import { initRealmEmojis } from '../emoji/realmEmojiActions';
import { initRealmFilters } from './realmFilterActions';
import { initStreams } from '../streams/streamsActions';
import { initUsers } from '../users/usersActions';
import { getAuth, getActiveNarrow } from '../selectors';
Expand Down Expand Up @@ -39,18 +47,20 @@ export const fetchRestOfInitialData = (pushToken: string): Action => async (
getState: GetState,
) => {
const auth = getAuth(getState());
const [streams, users, messages, realmEmoji] = await Promise.all([
const [streams, users, messages, realmEmoji, realmFilter] = await Promise.all([
await tryUntilSuccessful(() => getStreams(auth)),
await tryUntilSuccessful(() => getUsers(auth)),
await tryUntilSuccessful(() =>
getMessages(auth, Number.MAX_SAFE_INTEGER, 100, 0, specialNarrow('private')),
),
await tryUntilSuccessful(() => getRealmEmojis(auth)),
await tryUntilSuccessful(() => getRealmFilters(auth)),
]);
dispatch(messageFetchSuccess(messages, specialNarrow('private')));
dispatch(initStreams(streams));
dispatch(initUsers(users));
dispatch(initRealmEmojis(realmEmoji));
dispatch(initRealmFilters(realmFilter));
if (auth.apiKey !== '' && pushToken === '') {
refreshNotificationToken();
}
Expand Down
12 changes: 12 additions & 0 deletions src/realm/realmFilterActions.js
@@ -0,0 +1,12 @@
/* @flow */
import type { Auth, Dispatch } from '../types';
import { getRealmFilters } from '../api';
import { INIT_REALM_FILTER } from '../actionConstants';

export const initRealmFilters = (filters: Object) => ({
type: INIT_REALM_FILTER,
filters,
});

export const fetchRealmFilters = (auth: Auth) => async (dispatch: Dispatch) =>
dispatch(initRealmFilters(await getRealmFilters(auth)));
15 changes: 15 additions & 0 deletions src/realm/realmReducers.js
Expand Up @@ -10,13 +10,16 @@ import {
EVENT_UPDATE_DISPLAY_SETTINGS,
SAVE_TOKEN_PUSH,
DELETE_TOKEN_PUSH,
INIT_REALM_FILTER,
EVENT_REALM_FILTER_UPDATE,
} from '../actionConstants';

// Initial state
const initialState = {
twentyFourHourTime: false,
pushToken: '',
emoji: {},
filter: [],
};

const reducer = (state: RealmState = initialState, action: Action): RealmState => {
Expand Down Expand Up @@ -52,6 +55,18 @@ const reducer = (state: RealmState = initialState, action: Action): RealmState =
...state,
emoji: action.emojis,
};
case INIT_REALM_FILTER: {
return {
...state,
filter: action.filters,
};
}
case EVENT_REALM_FILTER_UPDATE: {
return {
...state,
filter: action.realm_filters,
};
}
case EVENT_REALM_EMOJI_UPDATE:
return {
...state,
Expand Down

0 comments on commit e2999d7

Please sign in to comment.