From fcf6ed4dd798906df1935db5370c4df37b2fe166 Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Tue, 9 Jun 2020 17:09:18 -0300 Subject: [PATCH] [FIX] Load messages when hideSystemMessages is enabled (#2101) * [WIP] Load messages when hideSystemMessages is enabled * Improve method name * Minor improvements Co-authored-by: Diego Mello --- app/views/RoomView/List.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/views/RoomView/List.js b/app/views/RoomView/List.js index 51240c7a..eeec5dee 100644 --- a/app/views/RoomView/List.js +++ b/app/views/RoomView/List.js @@ -171,9 +171,9 @@ class List extends React.Component { onEndReached = debounce(async() => { const { - loading, end, messages + loading, end, messages, latest = messages[messages.length - 1].ts } = this.state; - if (loading || end || messages.length < 50) { + if (loading || end) { return; } @@ -185,16 +185,40 @@ class List extends React.Component { // `offset` is `messages.length - 1` because we append thread start to `messages` obj result = await RocketChat.loadThreadMessages({ tmid, rid, offset: messages.length - 1 }); } else { - result = await RocketChat.loadMessagesForRoom({ rid, t, latest: messages[messages.length - 1].ts }); + result = await RocketChat.loadMessagesForRoom({ rid, t, latest }); } - this.setState({ end: result.length < 50, loading: false }); + this.setState({ end: result.length < 50, loading: false, latest: result[result.length - 1].ts }, () => this.loadMoreMessages(result)); } catch (e) { this.setState({ loading: false }); log(e); } }, 300) + loadMoreMessages = (result) => { + const { end } = this.state; + + if (end) { + return; + } + + // handle servers with version < 3.0.0 + let { hideSystemMessages = [] } = this.props; + if (!Array.isArray(hideSystemMessages)) { + hideSystemMessages = []; + } + + if (!hideSystemMessages.length) { + return; + } + + const hasReadableMessages = result.filter(message => !message.t || (message.t && !hideSystemMessages.includes(message.t))).length > 0; + // if this batch doesn't contain any messages that will be displayed, we'll request a new batch + if (!hasReadableMessages) { + this.onEndReached(); + } + } + onRefresh = () => this.setState({ refreshing: true }, async() => { const { messages } = this.state; const { rid, tmid } = this.props;