mattermost-mobile/app/selectors/post_list.js
Harrison Healey 0e4f9d5825 Changed to pass IDs to PostList instead of posts (#1036)
* Moved PostList to take a list of postIds instead of posts

* Removed usage of selectors that return posts from ChannelPostList and Thread

* Fixed search and switched to use getPostIdsAroundPost selector

* Removed use of selectors that returned posts from emoji reactions

* Updated makePreparePostIdsForPostList to be better memoized

* Fixed filter of join/leave messages

* Added unit tests for makePrepaprePostIdsForPostList

* Check if post edit/delete should be enabled more often

* Updated mattermost-redux version in yarn.lock
2017-10-20 18:58:44 -03:00

79 lines
3.1 KiB
JavaScript

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {Posts, Preferences} from 'mattermost-redux/constants';
import {makeGetPostsForIds} from 'mattermost-redux/selectors/entities/posts';
import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {createIdsSelector} from 'mattermost-redux/utils/helpers';
import {shouldFilterPost} from 'mattermost-redux/utils/post_utils';
export const DATE_LINE = 'date-';
export const START_OF_NEW_MESSAGES = 'start-of-new-messages';
function shouldShowJoinLeaveMessages(state) {
// This setting is true or not set if join/leave messages are to be displayed
return getBool(state, Preferences.CATEGORY_ADVANCED_SETTINGS, Preferences.ADVANCED_FILTER_JOIN_LEAVE, true);
}
// Returns a selector that, given the state and an object containing an array of postIds and an optional
// timestamp of when the channel was last read, returns a memoized array of postIds interspersed with
// day indicators and an optional new message indicator.
export function makePreparePostIdsForPostList() {
const getMyPosts = makeGetPostsForIds();
return createIdsSelector(
(state, props) => getMyPosts(state, props.postIds),
(state, props) => props.lastViewedAt,
getCurrentUserId,
shouldShowJoinLeaveMessages,
(posts, lastViewedAt, currentUserId, showJoinLeave) => {
const out = [];
let lastDate = null;
let addedNewMessagesIndicator = false;
const filterOptions = {showJoinLeave};
// Remember that we're iterating through the posts from newest to oldest
for (const post of posts) {
if (post.state === Posts.POST_DELETED && post.user_id === currentUserId) {
continue;
}
// Filter out join/leave messages if necessary
if (shouldFilterPost(post, filterOptions)) {
continue;
}
// Only add the new messages line if a lastViewedAt time is set
if (lastViewedAt && !addedNewMessagesIndicator) {
const postIsUnread = post.create_at > lastViewedAt;
if (postIsUnread) {
out.push(START_OF_NEW_MESSAGES);
addedNewMessagesIndicator = true;
}
}
// Push on a date header if the last post was on a different day than the current one
const postDate = new Date(post.create_at);
if (lastDate && lastDate.toDateString() !== postDate.toDateString()) {
out.push(DATE_LINE + lastDate.toDateString());
}
lastDate = postDate;
out.push(post.id);
}
// Push on the date header for the oldest post
if (lastDate) {
out.push(DATE_LINE + lastDate.toDateString());
}
return out;
}
);
}