Don't pre-fetch posts for unread archived channels (#4113)

This commit is contained in:
Miguel Alatzar 2020-04-02 14:40:18 -07:00 committed by GitHub
parent ee21ff8d81
commit 754d794dae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View file

@ -20,7 +20,7 @@ import {Posts} from '@mm-redux/constants';
import {getPost as selectPost, getPostIdsInChannel} from '@mm-redux/selectors/entities/posts';
import {getCurrentChannelId} from '@mm-redux/selectors/entities/channels';
import {removeUserFromList} from '@mm-redux/utils/user_utils';
import {isUnreadChannel} from '@mm-redux/utils/channel_utils';
import {isUnreadChannel, isArchivedChannel} from '@mm-redux/utils/channel_utils';
import {ViewTypes} from '@constants';
import {generateId} from '@utils/file';
@ -409,7 +409,7 @@ export function loadUnreadChannelPosts(channels, channelMembers) {
});
channels.forEach((channel) => {
if (channel.id === currentChannelId) {
if (channel.id === currentChannelId || isArchivedChannel(channel)) {
return;
}

View file

@ -38,6 +38,11 @@ describe('Actions.Views.Post', () => {
];
const channelMembers = [];
beforeEach(() => {
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
ChannelUtils.isArchivedChannel = jest.fn().mockReturnValue(false);
});
test('loadUnreadChannelPosts does not dispatch actions if no unread channels', async () => {
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(false);
@ -48,8 +53,18 @@ describe('Actions.Views.Post', () => {
expect(storeActions).toStrictEqual([]);
});
test('loadUnreadChannelPosts does not dispatch actions for archived channels', async () => {
ChannelUtils.isArchivedChannel = jest.fn().mockReturnValue(true);
Client4.getPosts = jest.fn().mockResolvedValue({posts: ['post-1', 'post-2']});
store = mockStore(storeObj);
await store.dispatch(loadUnreadChannelPosts(channels, channelMembers));
const storeActions = store.getActions();
expect(storeActions).toStrictEqual([]);
});
test('loadUnreadChannelPosts does not dispatch actions for current channel', async () => {
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
Client4.getPosts = jest.fn().mockResolvedValue({posts: ['post-1', 'post-2']});
store = mockStore(storeObj);
@ -60,7 +75,6 @@ describe('Actions.Views.Post', () => {
});
test('loadUnreadChannelPosts dispatches actions for unread channels with no postIds in channel', async () => {
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
Client4.getPosts = jest.fn().mockResolvedValue({posts: ['post-1', 'post-2']});
store = mockStore(storeObj);
@ -84,7 +98,6 @@ describe('Actions.Views.Post', () => {
});
test('loadUnreadChannelPosts dispatches actions for unread channels with postIds in channel', async () => {
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
PostSelectors.getPostIdsInChannel = jest.fn().mockReturnValue(['post-id-in-channel']);
Client4.getPostsSince = jest.fn().mockResolvedValue({posts: ['post-1', 'post-2']});
@ -128,7 +141,6 @@ describe('Actions.Views.Post', () => {
user_id: 'user-id',
message: '@user post-1',
}];
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
PostSelectors.getPostIdsInChannel = jest.fn().mockReturnValue(['post-id-in-channel']);
Client4.getPostsSince = jest.fn().mockResolvedValue({posts});
Client4.getProfilesByIds = jest.fn().mockResolvedValue(['data']);

View file

@ -141,9 +141,9 @@ export function isAutoClosed(
channelActivity: number,
channelArchiveTime: number,
currentChannelId = '',
now = Date.now()
now = Date.now(),
): boolean {
const cutoff = now - 7 * 24 * 60 * 60 * 1000;
const cutoff = now - (7 * 24 * 60 * 60 * 1000);
const viewTimePref = myPreferences[`${Preferences.CATEGORY_CHANNEL_APPROXIMATE_VIEW_TIME}--${channel.id}`];
const viewTime = viewTimePref ? parseInt(viewTimePref.value!, 10) : 0;
@ -193,7 +193,7 @@ export function isDirectChannelVisible(
lastPost?: Post | null,
isUnread?: boolean,
currentChannelId = '',
now?: number
now?: number,
): boolean {
const otherUser = typeof otherUserOrOtherUserId === 'object' ? otherUserOrOtherUserId : null;
const otherUserId = typeof otherUserOrOtherUserId === 'object' ? otherUserOrOtherUserId.id : otherUserOrOtherUserId;
@ -210,7 +210,7 @@ export function isDirectChannelVisible(
lastPost ? lastPost.create_at : 0,
otherUser ? otherUser.delete_at : 0,
currentChannelId,
now
now,
);
}
@ -226,7 +226,7 @@ export function isGroupChannelVisible(
channel: Channel,
lastPost?: Post,
isUnread?: boolean,
now?: number
now?: number,
): boolean {
const gm = myPreferences[`${Preferences.CATEGORY_GROUP_CHANNEL_SHOW}--${channel.id}`];
@ -241,7 +241,7 @@ export function isGroupChannelVisible(
lastPost ? lastPost.create_at : 0,
0,
'',
now
now,
);
}
@ -256,7 +256,7 @@ export function isGroupOrDirectChannelVisible(
users: IDMappedObjects<UserProfile>,
lastPosts: RelationOneToOne<Channel, Post>,
currentChannelId?: string,
now?: number
now?: number,
): boolean {
const lastPost = lastPosts[channel.id];
@ -278,7 +278,7 @@ export function isGroupOrDirectChannelVisible(
lastPost,
isUnreadChannel(memberships, channel),
currentChannelId,
now
now,
);
}
@ -571,6 +571,10 @@ function isNotDeletedChannel(channel: Channel) {
return channel.delete_at === 0;
}
export function isArchivedChannel(channel: Channel) {
return channel.delete_at !== 0;
}
export function isOpenChannel(channel: Channel): boolean {
return channel.type === General.OPEN_CHANNEL;
}