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

Commit

Permalink
MM-14977 Add bi-directional with loading of last unread post (#3049)
Browse files Browse the repository at this point in the history
* MM-14977 Add bi-directional with loading of last unread post

* Fix tests

* update depedency hashes

* Preevnt loading auto load of posts when postList does not exist
prevent loading older posts when a callback is in place
Fix range
Sync posts only if postListIds exist
Sync posts only if lastPostTimestamp exists

* Change variable and state names for unread chunk and timestamp reference
Use the new util func for selecting unread chunk
Remove the check for loading posts when calling loadPosts again

* Fix review comments

* Redux hash update
  • Loading branch information
sudheerDev authored and hmhealey committed Jul 9, 2019
1 parent cddfb0b commit 3652f10
Show file tree
Hide file tree
Showing 25 changed files with 1,326 additions and 642 deletions.
139 changes: 85 additions & 54 deletions actions/views/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {getLastPostsApiTimeForChannel} from 'selectors/views/channel';
import {getSocketStatus} from 'selectors/views/websocket';

import {browserHistory} from 'utils/browser_history';
import {Constants, ActionTypes, EventTypes} from 'utils/constants.jsx';
import {Constants, ActionTypes, EventTypes, PostRequestTypes} from 'utils/constants.jsx';
import {isMobile} from 'utils/utils.jsx';
import LocalStorageStore from 'stores/local_storage_store.jsx';

Expand Down Expand Up @@ -130,92 +130,123 @@ export function autocompleteUsersInChannel(prefix, channelId) {
};
}

export function loadInitialPosts(channelId, focusedPostId) {
export function loadUnreads(channelId) {
return async (dispatch) => {
let posts;
let hasMoreBefore = false;
let hasMoreAfter = false;
const time = Date.now();
if (focusedPostId) {
const result = await dispatch(PostActions.getPostsAround(channelId, focusedPostId, Posts.POST_CHUNK_SIZE / 2));

posts = result.data;

if (posts) {
// If the post is at index i, there are i posts after it and len - i - 1 before it
const numPostsAfter = posts.order.indexOf(focusedPostId);
const numPostsBefore = posts.order.length - numPostsAfter - 1;

hasMoreBefore = numPostsBefore >= Posts.POST_CHUNK_SIZE / 2;
hasMoreAfter = numPostsAfter >= Posts.POST_CHUNK_SIZE / 2;
}
} else {
const result = await dispatch(PostActions.getPosts(channelId, 0, Posts.POST_CHUNK_SIZE / 2));
const {data, error} = await dispatch(PostActions.getPostsUnread(channelId));
if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}

posts = result.data;
dispatch({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});

if (posts) {
hasMoreBefore = posts && posts.order.length >= Posts.POST_CHUNK_SIZE / 2;
}
}
return {
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}

if (posts) {
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
channelId,
time,
});
export function loadPostsAround(channelId, focusedPostId) {
return async (dispatch) => {
const {data, error} = await dispatch(PostActions.getPostsAround(channelId, focusedPostId, Posts.POST_CHUNK_SIZE / 2));
if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}

dispatch({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});
return {
posts,
hasMoreBefore,
hasMoreAfter,
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}

export function increasePostVisibility(channelId, beforePostId) {
return async (dispatch, getState) => {
const state = getState();
if (state.views.channel.loadingPosts[channelId]) {
return true;
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));

if (error) {
return {
error,
atLatestMessage: false,
atOldestmessage: false,
};
}

const currentPostVisibility = state.views.channel.postVisibility[channelId];
dispatch({
type: ActionTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME,
channelId,
time,
});

if (currentPostVisibility >= Constants.MAX_POST_VISIBILITY) {
return true;
}
return {
data,
atLatestMessage: data.next_post_id === '',
atOldestmessage: data.prev_post_id === '',
};
};
}

export function loadPosts({channelId, postId, type}) {
//type here can be BEFORE_ID or AFTER_ID
return async (dispatch) => {
const POST_INCREASE_AMOUNT = Constants.POST_CHUNK_SIZE / 2;

dispatch({
type: ActionTypes.LOADING_POSTS,
data: true,
channelId,
});

const result = await dispatch(PostActions.getPostsBefore(channelId, beforePostId, 0, Posts.POST_CHUNK_SIZE / 2));
const posts = result.data;
const page = 0;
let result;
if (type === PostRequestTypes.BEFORE_ID) {
result = await dispatch(PostActions.getPostsBefore(channelId, postId, page, POST_INCREASE_AMOUNT));
} else {
result = await dispatch(PostActions.getPostsAfter(channelId, postId, page, POST_INCREASE_AMOUNT));
}

const {data} = result;

const actions = [{
type: ActionTypes.LOADING_POSTS,
data: false,
channelId,
}];

if (posts) {
actions.push({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: posts.order.length,
});
if (result.error) {
return {
error: result.error,
moreToLoad: true,
};
}
actions.push({
type: ActionTypes.INCREASE_POST_VISIBILITY,
data: channelId,
amount: data.order.length,
});

dispatch(batchActions(actions));

return {
moreToLoad: posts ? posts.order.length >= Posts.POST_CHUNK_SIZE / 2 : false,
error: result.error,
moreToLoad: type === PostRequestTypes.BEFORE_ID ? data.prev_post_id !== '' : data.next_post_id !== '',
};
};
}
Expand Down
Loading

0 comments on commit 3652f10

Please sign in to comment.