From b53f3988b8cc5e9801094835702ac0469375bd92 Mon Sep 17 00:00:00 2001 From: enahum Date: Mon, 12 Feb 2018 16:44:34 -0300 Subject: [PATCH] [ICU-680] Fix Duplicate posts (#1430) --- app/actions/views/channel.js | 10 +++++++--- app/store/index.js | 8 -------- app/store/middleware.js | 33 +++++++++++++++++++++++++++++++++ app/utils/promise_timeout.js | 16 ---------------- 4 files changed, 40 insertions(+), 27 deletions(-) delete mode 100644 app/utils/promise_timeout.js diff --git a/app/actions/views/channel.js b/app/actions/views/channel.js index d43dd914d..f22cc7e53 100644 --- a/app/actions/views/channel.js +++ b/app/actions/views/channel.js @@ -149,13 +149,15 @@ export function loadPostsIfNecessaryWithRetry(channelId) { const time = Date.now(); - let loadMorePostsVisible; + let loadMorePostsVisible = true; let received; if (!postsIds || postsIds.length < ViewTypes.POST_VISIBILITY_CHUNK_SIZE) { // Get the first page of posts if it appears we haven't gotten it yet, like the webapp received = await retryGetPostsAction(getPosts(channelId), dispatch, getState); - loadMorePostsVisible = received.order.length >= ViewTypes.POST_VISIBILITY_CHUNK_SIZE; + if (received) { + loadMorePostsVisible = received.order.length >= ViewTypes.POST_VISIBILITY_CHUNK_SIZE; + } } else { const {lastConnectAt} = state.device.websocket; const lastGetPosts = state.views.channel.lastGetPosts[channelId]; @@ -173,7 +175,9 @@ export function loadPostsIfNecessaryWithRetry(channelId) { received = await retryGetPostsAction(getPostsSince(channelId, since), dispatch, getState); - loadMorePostsVisible = postsIds.length + received.order.length >= ViewTypes.POST_VISIBILITY_CHUNK_SIZE; + if (received) { + loadMorePostsVisible = postsIds.length + received.order.length >= ViewTypes.POST_VISIBILITY_CHUNK_SIZE; + } } if (received) { diff --git a/app/store/index.js b/app/store/index.js index a73c23b53..f340d816e 100644 --- a/app/store/index.js +++ b/app/store/index.js @@ -16,7 +16,6 @@ import appReducer from 'app/reducers'; import {throttle} from 'app/utils/general'; import networkConnectionListener from 'app/utils/network'; import {createSentryMiddleware} from 'app/utils/sentry/middleware'; -import {promiseTimeout} from 'app/utils/promise_timeout'; import mattermostBucket from 'app/mattermost_bucket'; import Config from 'assets/config'; @@ -134,13 +133,6 @@ export default function configureAppStore(initialState) { throw new Error('Offline Action: commit action must be present.'); } - if (action.meta.offline.canTimeout) { - const defaultTimeout = 10000; - const timeout = action.meta.offline.timeout || defaultTimeout; - - return promiseTimeout(effect(), timeout); - } - return effect(); }, detectNetwork: (callback) => networkConnectionListener(callback), diff --git a/app/store/middleware.js b/app/store/middleware.js index 3969a6db0..0aec38c8c 100644 --- a/app/store/middleware.js +++ b/app/store/middleware.js @@ -277,6 +277,32 @@ function cleanupState(action, keepCurrent = false) { } }); + // remove any pending posts that hasn't failed + if (payload.entities.posts && payload.entities.posts.pendingPostIds.length) { + const nextPendingPostIds = [...payload.entities.posts.pendingPostIds]; + payload.entities.posts.pendingPostIds.forEach((id) => { + const posts = nextEntitites.posts.posts; + const post = posts[id]; + + if (post) { + const postsInChannel = [...nextEntitites.posts.postsInChannel[post.channel_id]] || []; + if (!post.failed) { + Reflect.deleteProperty(posts, id); + const index = postsInChannel.indexOf(id); + if (index !== -1) { + postsInChannel.splice(index, 1); + nextEntitites.posts.postsInChannel[post.channel_id] = postsInChannel; + } + removePendingPost(nextPendingPostIds, id); + } + } else { + removePendingPost(nextPendingPostIds, id); + } + }); + + nextEntitites.posts.pendingPostIds = nextPendingPostIds; + } + const nextState = { app: resetPayload.app, entities: { @@ -324,3 +350,10 @@ export function shareExtensionData() { return nextAction; }; } + +function removePendingPost(pendingPostIds, id) { + const pendingIndex = pendingPostIds.indexOf(id); + if (pendingIndex !== -1) { + pendingPostIds.splice(pendingIndex, 1); + } +} diff --git a/app/utils/promise_timeout.js b/app/utils/promise_timeout.js deleted file mode 100644 index 3023872a5..000000000 --- a/app/utils/promise_timeout.js +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. - -export function promiseTimeout(promise, ms) { - const timeout = new Promise((resolve, reject) => { - const id = setTimeout(() => { - clearTimeout(id); - reject('Timed out in ' + ms + 'ms.'); - }, ms); - }); - - return Promise.race([ - promise, - timeout - ]); -}