Skip to content

Commit

Permalink
Do not show outbox if not caught up (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
borisyankov authored Oct 6, 2017
1 parent e09648c commit b23628a
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/caughtup/caughtUpReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
ACCOUNT_SWITCH,
MESSAGE_FETCH_COMPLETE,
} from '../actionConstants';
import { NULL_CAUGHTUP } from '../nullObjects';
import { NULL_CAUGHTUP, NULL_OBJECT } from '../nullObjects';

const initialState: CaughtUpState = {};
const initialState: CaughtUpState = NULL_OBJECT;

export default (state: CaughtUpState = initialState, action: Action) => {
switch (action.type) {
Expand Down
4 changes: 2 additions & 2 deletions src/caughtup/caughtUpSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import { createSelector } from 'reselect';

import type { GlobalState } from '../types';
import { NULL_CAUGHTUP } from '../nullObjects';
import { NULL_CAUGHTUP, NULL_OBJECT } from '../nullObjects';
import { getActiveNarrowString } from '../directSelectors';

export const getCaughtUp = (state: GlobalState): Object => state.caughtUp;
export const getCaughtUp = (state: GlobalState): Object => state.caughtUp || NULL_OBJECT;

export const getCaughtUpForActiveNarrow = createSelector(
getCaughtUp,
Expand Down
31 changes: 29 additions & 2 deletions src/chat/__tests__/chatSelectors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('getMessagesInActiveNarrow', () => {
expect(anchor).toBe(state.chat.messages['[]']);
});

test('combine messages & outbox in same narrow', () => {
test('combine messages and outbox in same narrow', () => {
const state = deepFreeze({
chat: {
narrow: homeNarrow,
Expand All @@ -42,6 +42,9 @@ describe('getMessagesInActiveNarrow', () => {
timestamp: 12,
},
],
caughtUp: {
[homeNarrowStr]: { older: false, newer: true },
},
});

const anchor = getMessagesInActiveNarrow(state);
Expand All @@ -60,7 +63,31 @@ describe('getMessagesInActiveNarrow', () => {
expect(anchor).toEqual(expectedState);
});

test('do not combine messages & outbox in different narrow', () => {
test('do not combine messages and outbox if not caught up', () => {
const state = deepFreeze({
chat: {
narrow: homeNarrow,
messages: {
[homeNarrowStr]: [{ id: 123 }],
},
},
outbox: [
{
email: '[email protected]',
narrow: homeNarrow,
parsedContent: '<p>Hello</p>',
sender_full_name: 'donald',
timestamp: 12,
},
],
});

const anchor = getMessagesInActiveNarrow(state);

expect(anchor).toBe(state.chat.messages[homeNarrowStr]);
});

test('do not combine messages and outbox in different narrow', () => {
const state = deepFreeze({
chat: {
narrow: privateNarrow('[email protected]'),
Expand Down
6 changes: 3 additions & 3 deletions src/chat/chatReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
EVENT_REACTION_REMOVE,
EVENT_UPDATE_MESSAGE,
} from '../actionConstants';
import { homeNarrow, isMessageInNarrow, getNarrowFromMessage } from '../utils/narrow';
import { homeNarrow, isMessageInNarrow } from '../utils/narrow';
import chatUpdater from './chatUpdater';
import { getMessagesById } from '../selectors';
import { NULL_ARRAY } from '../nullObjects';
import { NULL_ARRAY, NULL_OBJECT } from '../nullObjects';

const initialState: ChatState = {
narrow: homeNarrow,
messages: {},
messages: NULL_OBJECT,
};

export default (state: ChatState = initialState, action: Action) => {
Expand Down
25 changes: 17 additions & 8 deletions src/chat/chatSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getStreams,
getOutbox,
} from '../directSelectors';
import { getCaughtUpForActiveNarrow } from '../caughtup/caughtUpSelectors';
import {
isAllPrivateNarrow,
isPrivateOrGroupNarrow,
Expand All @@ -27,16 +28,24 @@ const getMessagesFromChatState = state =>

export const outboxMessagesForCurrentNarrow = createSelector(
getActiveNarrow,
getCaughtUpForActiveNarrow,
getActiveNarrowString,
getOutbox,
(narrow, activeNarrowString, outboxMessages) =>
isHomeNarrow(narrow)
? outboxMessages
: outboxMessages.filter(item => {
if (isAllPrivateNarrow(narrow) && isPrivateOrGroupNarrow(item.narrow)) return true;
if (isStreamNarrow(narrow) && item.narrow[0].operand === narrow[0].operand) return true;
return JSON.stringify(item.narrow) === activeNarrowString;
}),
(narrow, caughtUp, activeNarrowString, outboxMessages) => {
if (!caughtUp.newer) {
return [];
}

if (isHomeNarrow(narrow)) {
return outboxMessages;
}

return outboxMessages.filter(item => {
if (isAllPrivateNarrow(narrow) && isPrivateOrGroupNarrow(item.narrow)) return true;
if (isStreamNarrow(narrow) && item.narrow[0].operand === narrow[0].operand) return true;
return JSON.stringify(item.narrow) === activeNarrowString;
});
},
);

export const getFetchedMessagesInActiveNarrow = createSelector(
Expand Down
4 changes: 2 additions & 2 deletions src/chat/fetchingReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
MESSAGE_FETCH_START,
MESSAGE_FETCH_COMPLETE,
} from '../actionConstants';
import { NULL_FETCHING } from '../nullObjects';
import { NULL_FETCHING, NULL_OBJECT } from '../nullObjects';

const initialState: FetchingState = {};
const initialState: FetchingState = NULL_OBJECT;

export default (state: FetchingState = initialState, action: Action) => {
switch (action.type) {
Expand Down
3 changes: 2 additions & 1 deletion src/drafts/draftsReducers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* @flow */
import type { DraftState, Action } from '../types';
import { DRAFT_ADD, DRAFT_REMOVE, LOGOUT } from '../actionConstants';
import { NULL_OBJECT } from '../nullObjects';

const initialState = {};
const initialState = NULL_OBJECT;

export default (state: DraftState = initialState, action: Action): DraftState => {
switch (action.type) {
Expand Down
2 changes: 2 additions & 0 deletions src/nullObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {

export const nullFunction = () => {};

export const NULL_OBJECT = Object.freeze({});

export const NULL_ARRAY = Object.freeze([]);

export const NULL_ACCOUNT: Account = {
Expand Down
3 changes: 2 additions & 1 deletion src/presence/presenceReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
EVENT_PRESENCE,
PRESENCE_RESPONSE,
} from '../actionConstants';
import { NULL_OBJECT } from '../nullObjects';

const initialState: Object = {};
const initialState: Object = NULL_OBJECT;

export default (state: UsersState = initialState, action: Action): UsersState => {
switch (action.type) {
Expand Down
4 changes: 2 additions & 2 deletions src/typing/typingReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import type { Action, TypingState } from '../types';
import { EVENT_TYPING_START, EVENT_TYPING_STOP } from '../actionConstants';
import { normalizeRecipientsSansMe } from '../utils/message';
import { NULL_ARRAY } from '../nullObjects';
import { NULL_ARRAY, NULL_OBJECT } from '../nullObjects';

const initialState: TypingState = {};
const initialState: TypingState = NULL_OBJECT;

export default (state: TypingState = initialState, action: Action): TypingState => {
switch (action.type) {
Expand Down

0 comments on commit b23628a

Please sign in to comment.