mattermost-mobile/service/utils/post_utils.js
Chris Duarte 496068e99c PLT-5496 Allow user to load more posts in channel posts list (#307)
* Group addDatesToPostList options into object

* Clean up imports in PostList

* Dry up code for adding dates to posts in PostList

* Allow user to load more posts in channel post list

* Load more posts upon scrolling to end instead of with button

* Wait for initial posts to be loaded before loading more

* Return data in getPosts actions

* Check how many posts were fetched to determine if there are more

* Insert load-more-posts into list in PostList not post_utils.js

* Rename component for LoadMorePosts

* Use LoadMorePosts to indicate if more posts are loading

* Only try to load more posts if initial posts have been loaded

* Don’t show LoadMorePosts if posts list is empty

* Don’t try to load more posts if first post has been loaded

* Don’t show LoadMorePosts if first post has been loaded

* Set hasFirstPost in willReceiveProps of PostList

* Use constant for load-more-posts token string

* Only show LoadMorePosts if pagination is enabled on PostList

* Fix logic for determining if posts loaded in PostList

* Ensure PostList ListView rerenders

* Move load more posts logic into ChannelPostList

* Remove unused getTheme from ChannelPostList container

* Avoid clever variable assignment in LoadMorePosts
2017-03-09 14:07:35 -03:00

48 lines
1.4 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {Constants} from 'service/constants';
export function isSystemMessage(post) {
return post.type && post.type.startsWith(Constants.SYSTEM_MESSAGE_PREFIX);
}
export function shouldIgnorePost(post) {
return Constants.IGNORE_POST_TYPES.includes(post.type);
}
export function addDatesToPostList(posts, options = {}) {
const {indicateNewMessages, currentUserId, lastViewedAt} = options;
const out = [];
let lastDate = null;
let subsequentPostIsUnread = false;
let subsequentPostUserId;
let postIsUnread;
for (const post of posts) {
postIsUnread = post.create_at > lastViewedAt;
if (indicateNewMessages && subsequentPostIsUnread && !postIsUnread && subsequentPostUserId !== currentUserId) {
out.push(Constants.START_OF_NEW_MESSAGES);
}
subsequentPostIsUnread = postIsUnread;
subsequentPostUserId = post.user_id;
const postDate = new Date(post.create_at);
// Push on a date header if the last post was on a different day than the current one
if (lastDate && lastDate.toDateString() !== postDate.toDateString()) {
out.push(lastDate);
}
lastDate = postDate;
out.push(post);
}
// Push on the date header for the oldest post
if (lastDate) {
out.push(lastDate);
}
return out;
}