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

MM-22989 - Adding reply count to root posts in RHS (Search, Flags, Pinned) #5031

Merged
merged 3 commits into from
Mar 12, 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
20 changes: 2 additions & 18 deletions components/post_view/post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

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

import {Posts} from 'mattermost-redux/constants';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
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 {isPostEphemeral, isSystemMessage} from 'mattermost-redux/utils/post_utils';
import {isSystemMessage} from 'mattermost-redux/utils/post_utils';

import {markPostAsUnread} from 'actions/post_actions';
import {selectPost, selectPostCard} from 'actions/views/rhs';

import {isArchivedChannel} from 'utils/channel_utils';
import {Preferences} from 'utils/constants';
import {makeCreateAriaLabelForPost} from 'utils/post_utils.jsx';
import {makeCreateAriaLabelForPost, makeGetReplyCount} from 'utils/post_utils.jsx';

import Post from './post.jsx';

Expand All @@ -37,21 +36,6 @@ 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();
Expand Down
90 changes: 1 addition & 89 deletions components/post_view/post/index.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

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

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

describe('isFirstReply', () => {
for (const testCase of [
Expand Down Expand Up @@ -61,89 +59,3 @@ 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);
});
});
7 changes: 5 additions & 2 deletions components/search_results_item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {getUser} from 'mattermost-redux/selectors/entities/users';
Expand All @@ -18,12 +19,13 @@ import {
setRhsExpanded,
} from 'actions/views/rhs';

import {makeCreateAriaLabelForPost} from 'utils/post_utils.jsx';
import {makeCreateAriaLabelForPost, makeGetReplyCount} from 'utils/post_utils.jsx';
import {getDirectTeammate, getDisplayNameByUser} from 'utils/utils.jsx';

import SearchResultsItem from './search_results_item.jsx';

function mapStateToProps() {
const getReplyCount = makeGetReplyCount();
const createAriaLabelForPost = makeCreateAriaLabelForPost();
const getCommentCountForPost = makeGetCommentCountForPost();

Expand All @@ -48,7 +50,8 @@ function mapStateToProps() {
isFlagged: isPostFlagged(post.id, preferences),
isBot: user ? user.is_bot : false,
directTeammate,
displayName: getDisplayNameByUser(state, directTeammate)
displayName: getDisplayNameByUser(state, directTeammate),
replyCount: getReplyCount(state, post),
};
};
}
Expand Down
7 changes: 7 additions & 0 deletions components/search_results_item/search_results_item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ class SearchResultsItem extends React.PureComponent {
intl: intlShape.isRequired,
directTeammate: PropTypes.string.isRequired,
displayName: PropTypes.string.isRequired,

/**
* The number of replies in the same thread as this post
*/
replyCount: PropTypes.number,
};

static defaultProps = {
Expand Down Expand Up @@ -307,8 +312,10 @@ class SearchResultsItem extends React.PureComponent {
<CommentIcon
location={Locations.SEARCH}
handleCommentClick={this.handleFocusRHSClick}
commentCount={this.props.replyCount}
postId={post.id}
searchStyle={'search-item__comment'}
extraClass={this.props.replyCount ? 'icon--visible' : ''}
/>
<a
href='#'
Expand Down
17 changes: 16 additions & 1 deletion utils/post_utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {get} from 'mattermost-redux/selectors/entities/preferences';
import {makeGetDisplayName, getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {Permissions, Posts} from 'mattermost-redux/constants';
import * as PostListUtils from 'mattermost-redux/utils/post_list';
import {canEditPost as canEditPostRedux} from 'mattermost-redux/utils/post_utils';
import {canEditPost as canEditPostRedux, isPostEphemeral} from 'mattermost-redux/utils/post_utils';

import {getEmojiMap} from 'selectors/emojis';

Expand Down Expand Up @@ -455,3 +455,18 @@ export function getNewMessageIndex(postListIds) {
(item) => item.indexOf(PostListRowListIds.START_OF_NEW_MESSAGES) === 0
);
}

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;
}
);
}
87 changes: 87 additions & 0 deletions utils/post_utils.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import assert from 'assert';

import {createIntl} from 'react-intl';
import {Posts} from 'mattermost-redux/constants';

import * as PostUtils from 'utils/post_utils.jsx';
import {PostListRowListIds} from 'utils/constants';
Expand Down Expand Up @@ -741,3 +742,89 @@ describe('PostUtils.splitMessageBasedOnCaretPosition', () => {
assert.equal('st Message', stringPieces.lastPiece);
});
});

describe('makeGetReplyCount', () => {
test('should return the number of comments when called on a root post', () => {
const getReplyCount = PostUtils.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 = PostUtils.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 = PostUtils.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 = PostUtils.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);
});
});