Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Thread fetching revert #4665

Merged
merged 5 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions actions/views/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function autocompleteUsersInChannel(prefix, channelId) {
export function loadUnreads(channelId) {
return async (dispatch) => {
const time = Date.now();
const {data, error} = await dispatch(PostActions.getPostsUnread(channelId, false));
const {data, error} = await dispatch(PostActions.getPostsUnread(channelId));
if (error) {
return {
error,
Expand Down Expand Up @@ -192,7 +192,7 @@ export function loadUnreads(channelId) {

export function loadPostsAround(channelId, focusedPostId) {
return async (dispatch) => {
const {data, error} = await dispatch(PostActions.getPostsAround(channelId, focusedPostId, Posts.POST_CHUNK_SIZE / 2, false));
const {data, error} = await dispatch(PostActions.getPostsAround(channelId, focusedPostId, Posts.POST_CHUNK_SIZE / 2));
if (error) {
return {
error,
Expand All @@ -216,7 +216,7 @@ export function loadPostsAround(channelId, focusedPostId) {
export function loadLatestPosts(channelId) {
return async (dispatch) => {
const time = Date.now();
const {data, error} = await dispatch(PostActions.getPosts(channelId, 0, Posts.POST_CHUNK_SIZE / 2, false));
const {data, error} = await dispatch(PostActions.getPosts(channelId, 0, Posts.POST_CHUNK_SIZE / 2));

if (error) {
return {
Expand Down Expand Up @@ -254,9 +254,9 @@ export function loadPosts({channelId, postId, type}) {
const page = 0;
let result;
if (type === PostRequestTypes.BEFORE_ID) {
result = await dispatch(PostActions.getPostsBefore(channelId, postId, page, POST_INCREASE_AMOUNT, false));
result = await dispatch(PostActions.getPostsBefore(channelId, postId, page, POST_INCREASE_AMOUNT));
} else {
result = await dispatch(PostActions.getPostsAfter(channelId, postId, page, POST_INCREASE_AMOUNT, false));
result = await dispatch(PostActions.getPostsAfter(channelId, postId, page, POST_INCREASE_AMOUNT));
}

const {data} = result;
Expand Down Expand Up @@ -287,7 +287,7 @@ export function loadPosts({channelId, postId, type}) {
};
}

export function syncPostsInChannel(channelId, since, fetchThreads = true) {
export function syncPostsInChannel(channelId, since) {
return async (dispatch, getState) => {
const time = Date.now();
const state = getState();
Expand All @@ -299,7 +299,7 @@ export function syncPostsInChannel(channelId, since, fetchThreads = true) {
sinceTimeToGetPosts = lastPostsApiCallForChannel;
}

const {data, error} = await dispatch(PostActions.getPostsSince(channelId, sinceTimeToGetPosts, fetchThreads));
const {data, error} = await dispatch(PostActions.getPostsSince(channelId, sinceTimeToGetPosts));
if (data) {
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
Expand Down
16 changes: 8 additions & 8 deletions actions/views/channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('channel view actions', () => {

expect(result.data).toBe(posts);

expect(PostActions.getPosts).toHaveBeenCalledWith('channel', 0, Posts.POST_CHUNK_SIZE / 2, false);
expect(PostActions.getPosts).toHaveBeenCalledWith('channel', 0, Posts.POST_CHUNK_SIZE / 2);
});

test('when oldest posts are recived', async () => {
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('channel view actions', () => {
const result = await store.dispatch(Actions.loadUnreads('channel', 'post'));

expect(result).toEqual({atLatestMessage: true, atOldestmessage: true});
expect(PostActions.getPostsUnread).toHaveBeenCalledWith('channel', false);
expect(PostActions.getPostsUnread).toHaveBeenCalledWith('channel');
});

test('when there are posts before and after the response', async () => {
Expand All @@ -223,7 +223,7 @@ describe('channel view actions', () => {

const result = await store.dispatch(Actions.loadUnreads('channel', 'post'));
expect(result).toEqual({atLatestMessage: false, atOldestmessage: false});
expect(PostActions.getPostsUnread).toHaveBeenCalledWith('channel', false);
expect(PostActions.getPostsUnread).toHaveBeenCalledWith('channel');
});

test('when there are no posts after RECEIVED_POSTS_FOR_CHANNEL_AT_TIME should be dispatched', async () => {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('channel view actions', () => {

expect(result).toEqual({atLatestMessage: true, atOldestmessage: true});

expect(PostActions.getPostsAround).toHaveBeenCalledWith('channel', 'post', Posts.POST_CHUNK_SIZE / 2, false);
expect(PostActions.getPostsAround).toHaveBeenCalledWith('channel', 'post', Posts.POST_CHUNK_SIZE / 2);
});

test('when there are posts before and after reponse posts chunk', async () => {
Expand Down Expand Up @@ -461,8 +461,8 @@ describe('channel view actions', () => {
},
});

await store.dispatch(Actions.syncPostsInChannel(channelId, 12350, false));
expect(PostActions.getPostsSince).toHaveBeenCalledWith(channelId, 12350, false);
await store.dispatch(Actions.syncPostsInChannel(channelId, 12350));
expect(PostActions.getPostsSince).toHaveBeenCalledWith(channelId, 12350);
});

test('should call getPostsSince with lastDisconnect time as last discconet was later than lastGetPosts', async () => {
Expand All @@ -485,8 +485,8 @@ describe('channel view actions', () => {
},
});

await store.dispatch(Actions.syncPostsInChannel(channelId, 12355, false));
expect(PostActions.getPostsSince).toHaveBeenCalledWith(channelId, 12343, false);
await store.dispatch(Actions.syncPostsInChannel(channelId, 12355));
expect(PostActions.getPostsSince).toHaveBeenCalledWith(channelId, 12343);
});
});

Expand Down
2 changes: 1 addition & 1 deletion actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function reconnect(includeWebSocket = true) {
const mostRecentPost = getPost(state, mostRecentId);
dispatch(loadChannelsForCurrentUser());
if (mostRecentPost) {
dispatch(syncPostsInChannel(currentChannelId, mostRecentPost.create_at, false));
dispatch(syncPostsInChannel(currentChannelId, mostRecentPost.create_at));
} else {
// if network timed-out the first time when loading a channel
// we can request for getPosts again when socket is connected
Expand Down
2 changes: 1 addition & 1 deletion actions/websocket_actions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ describe('handleNewPostEvents', () => {
describe('reconnect', () => {
test('should call syncPostsInChannel when socket reconnects', () => {
reconnect(false);
expect(syncPostsInChannel).toHaveBeenCalledWith('otherChannel', '12345', false);
expect(syncPostsInChannel).toHaveBeenCalledWith('otherChannel', '12345');
});
});

Expand Down
2 changes: 1 addition & 1 deletion components/permalink_view/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {ActionTypes, Constants, ErrorPageTypes} from 'utils/constants';

export function focusPost(postId, returnTo = '') {
return async (dispatch, getState) => {
const {data} = await dispatch(getPostThread(postId, false));
const {data} = await dispatch(getPostThread(postId));

if (!data) {
browserHistory.replace(`/error?type=${ErrorPageTypes.PERMALINK_NOT_FOUND}&returnTo=${returnTo}`);
Expand Down
8 changes: 4 additions & 4 deletions components/permalink_view/permalink_view.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('components/PermalinkView', () => {
const testStore = await mockStore(initialState);
await testStore.dispatch(focusPost('postid1'));

expect(getPostThread).toHaveBeenCalledWith('postid1', false);
expect(getPostThread).toHaveBeenCalledWith('postid1');
expect(testStore.getActions()).toEqual([
{type: 'MOCK_GET_POST_THREAD', data: {posts: {postid1: {id: 'postid1', message: 'some message', channel_id: 'channelid1'}}, order: ['postid1']}},
{type: 'MOCK_SELECT_CHANNEL', args: ['channelid1']},
Expand All @@ -160,7 +160,7 @@ describe('components/PermalinkView', () => {

await testStore.dispatch(focusPost('postid2'));

expect(getPostThread).toHaveBeenCalledWith('postid2', false);
expect(getPostThread).toHaveBeenCalledWith('postid2');
expect(testStore.getActions()).toEqual([
{type: 'MOCK_GET_POST_THREAD', data: {posts: {postid2: {id: 'postid2', message: 'some message', channel_id: 'channelid2'}}, order: ['postid2']}},
{type: 'MOCK_GET_CHANNEL', data: {id: 'channelid2', type: 'O', team_id: 'current_team_id'}},
Expand All @@ -176,7 +176,7 @@ describe('components/PermalinkView', () => {
const testStore = await mockStore(initialState);
await testStore.dispatch(focusPost('dmpostid1'));

expect(getPostThread).toHaveBeenCalledWith('dmpostid1', false);
expect(getPostThread).toHaveBeenCalledWith('dmpostid1');
expect(testStore.getActions()).toEqual([
{type: 'MOCK_GET_POST_THREAD', data: {posts: {dmpostid1: {id: 'dmpostid1', message: 'some message', channel_id: 'dmchannelid'}}, order: ['dmpostid1']}},
]);
Expand All @@ -187,7 +187,7 @@ describe('components/PermalinkView', () => {
const testStore = await mockStore(initialState);
await testStore.dispatch(focusPost('gmpostid1'));

expect(getPostThread).toHaveBeenCalledWith('gmpostid1', false);
expect(getPostThread).toHaveBeenCalledWith('gmpostid1');
expect(testStore.getActions()).toEqual([
{type: 'MOCK_GET_POST_THREAD', data: {posts: {gmpostid1: {id: 'gmpostid1', message: 'some message', channel_id: 'gmchannelid'}}, order: ['gmpostid1']}},
]);
Expand Down
27 changes: 20 additions & 7 deletions components/post_view/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {createSelector} from 'reselect';

import {Posts} from 'mattermost-redux/constants';
import {getPost, makeIsPostCommentMention} from 'mattermost-redux/selectors/entities/posts';
import {get} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {isSystemMessage} from 'mattermost-redux/utils/post_utils';
import {isPostEphemeral, isSystemMessage} from 'mattermost-redux/utils/post_utils';

import {markPostAsUnread} from 'actions/post_actions';
import {selectPost, selectPostCard} from 'actions/views/rhs';
Expand All @@ -34,17 +35,29 @@ export function isFirstReply(post, previousPost) {
return false;
}

export function makeGetReplyCount() {
return createSelector(
(state) => state.entities.posts.posts,
(state, post) => state.entities.posts.postsInThread[post.root_id || post.id],
(allPosts, postIds) => {
if (!postIds) {
return 0;
}

// Count the number of non-ephemeral posts in the thread
return postIds.map((id) => allPosts[id]).filter((post) => post && !isPostEphemeral(post)).length;
}
);
}

function makeMapStateToProps() {
const getReplyCount = makeGetReplyCount();
const isPostCommentMention = makeIsPostCommentMention();
const createAriaLabelForPost = makeCreateAriaLabelForPost();

return (state, ownProps) => {
const post = ownProps.post || getPost(state, ownProps.postId);
let replyCount = post.reply_count;
if (post.root_id !== '') {
const rootPost = getPost(state, post.root_id);
replyCount = rootPost ? rootPost.reply_count : 0;
}

let previousPost = null;
if (ownProps.previousPostId) {
previousPost = getPost(state, ownProps.previousPostId);
Expand All @@ -69,7 +82,7 @@ function makeMapStateToProps() {
isFirstReply: isFirstReply(post, previousPost),
consecutivePostByUser,
previousPostIsComment,
replyCount,
replyCount: getReplyCount(state, post),
isCommentMention: isPostCommentMention(state, post.id),
center: get(state, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.CHANNEL_DISPLAY_MODE, Preferences.CHANNEL_DISPLAY_MODE_DEFAULT) === Preferences.CHANNEL_DISPLAY_MODE_CENTERED,
compactDisplay: get(state, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.MESSAGE_DISPLAY, Preferences.MESSAGE_DISPLAY_DEFAULT) === Preferences.MESSAGE_DISPLAY_COMPACT,
Expand Down
90 changes: 89 additions & 1 deletion components/post_view/post/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {isFirstReply} from './index';
import {Posts} from 'mattermost-redux/constants';

import {isFirstReply, makeGetReplyCount} from './index';

describe('isFirstReply', () => {
for (const testCase of [
Expand Down Expand Up @@ -59,3 +61,89 @@ describe('isFirstReply', () => {
});
}
});

describe('makeGetReplyCount', () => {
test('should return the number of comments when called on a root post', () => {
const getReplyCount = makeGetReplyCount();

const state = {
entities: {
posts: {
posts: {
post1: {id: 'post1'},
post2: {id: 'post2', root_id: 'post1'},
post3: {id: 'post3', root_id: 'post1'},
},
postsInThread: {
post1: ['post2', 'post3'],
},
},
},
};
const post = state.entities.posts.posts.post1;

expect(getReplyCount(state, post)).toBe(2);
});

test('should return the number of comments when called on a comment', () => {
const getReplyCount = makeGetReplyCount();

const state = {
entities: {
posts: {
posts: {
post1: {id: 'post1'},
post2: {id: 'post2', root_id: 'post1'},
post3: {id: 'post3', root_id: 'post1'},
},
postsInThread: {
post1: ['post2', 'post3'],
},
},
},
};
const post = state.entities.posts.posts.post3;

expect(getReplyCount(state, post)).toBe(2);
});

test('should return 0 when called on a post without comments', () => {
const getReplyCount = makeGetReplyCount();

const state = {
entities: {
posts: {
posts: {
post1: {id: 'post1'},
},
postsInThread: {},
},
},
};
const post = state.entities.posts.posts.post1;

expect(getReplyCount(state, post)).toBe(0);
});

test('should not count ephemeral comments', () => {
const getReplyCount = makeGetReplyCount();

const state = {
entities: {
posts: {
posts: {
post1: {id: 'post1'},
post2: {id: 'post2', root_id: 'post1', type: Posts.POST_TYPES.EPHEMERAL},
post3: {id: 'post3', root_id: 'post1'},
},
postsInThread: {
post1: ['post2', 'post3'],
},
},
},
};
const post = state.entities.posts.posts.post1;

expect(getReplyCount(state, post)).toBe(1);
});
});
2 changes: 1 addition & 1 deletion components/rhs_thread/rhs_thread.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class RhsThread extends React.Component {
const curPostsArray = this.props.posts || [];

if (this.props.socketConnectionStatus && !prevProps.socketConnectionStatus) {
this.props.actions.getPostThread(this.props.selected.id, true);
this.props.actions.getPostThread(this.props.selected.id);
}

if (prevPostsArray.length >= curPostsArray.length) {
Expand Down
2 changes: 1 addition & 1 deletion components/rhs_thread/rhs_thread.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('components/RhsThread', () => {
wrapper.setProps({socketConnectionStatus: false});
wrapper.setProps({socketConnectionStatus: true});

expect(actions.getPostThread).toHaveBeenCalledWith(post.id, true);
expect(actions.getPostThread).toHaveBeenCalledWith(post.id);
});

test('should update openTime state when selected prop updated', async () => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"localforage-observable": "2.0.0",
"mark.js": "8.11.1",
"marked": "github:mattermost/marked#8214e10918264fb88cec474196023949f7cb4b30",
"mattermost-redux": "github:mattermost/mattermost-redux#a8342fcfc12d4b2b183551ee9e12d2f22bab28cf",
"mattermost-redux": "github:mattermost/mattermost-redux#d6fd8d66e3c4d9bae7ad2f291484496d0d6ccd10",
"moment-timezone": "0.5.27",
"pdfjs-dist": "2.0.489",
"popmotion": "8.7.0",
Expand Down