From 26dfbfd08ceb913fd91f123935696d253fca2035 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 5 Jun 2017 19:54:45 +0300 Subject: [PATCH] Lazy load initial timeline events --- include/MatrixClient.h | 2 +- src/MatrixClient.cc | 5 +++-- src/TimelineView.cc | 14 ++++++++------ src/TimelineViewManager.cc | 3 ++- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 2fde1c1e..7a4ac24b 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -44,7 +44,7 @@ public: void fetchUserAvatar(const QString &userId, const QUrl &avatarUrl); void fetchOwnAvatar(const QUrl &avatar_url); void downloadImage(const QString &event_id, const QUrl &url); - void messages(const QString &room_id, const QString &from_token) noexcept; + void messages(const QString &room_id, const QString &from_token, int limit = 20) noexcept; inline QUrl getHomeServer(); inline int transactionId(); diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 927db541..ebecb05a 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -541,7 +541,7 @@ void MatrixClient::initialSync() noexcept }; QJsonObject filter{{"room", - QJsonObject{{"timeline", QJsonObject{{"limit", 70}}}, + QJsonObject{{"timeline", QJsonObject{{"limit", 20}}}, {"ephemeral", QJsonObject{{"limit", 0}}}}}, {"presence", QJsonObject{{"not_types", excluded_presence}}}}; @@ -677,12 +677,13 @@ void MatrixClient::fetchOwnAvatar(const QUrl &avatar_url) reply->setProperty("endpoint", static_cast(Endpoint::GetOwnAvatar)); } -void MatrixClient::messages(const QString &room_id, const QString &from_token) noexcept +void MatrixClient::messages(const QString &room_id, const QString &from_token, int limit) noexcept { QUrlQuery query; query.addQueryItem("access_token", token_); query.addQueryItem("from", from_token); query.addQueryItem("dir", "b"); + query.addQueryItem("limit", QString::number(limit)); QUrl endpoint(server_); endpoint.setPath(api_url_ + QString("/rooms/%1/messages").arg(room_id)); diff --git a/src/TimelineView.cc b/src/TimelineView.cc index 412b9520..418545a6 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -76,7 +76,7 @@ void TimelineView::scrollDown() // The first time we enter the room move the scroll bar to the bottom. if (!isInitialized) { - scroll_area_->ensureVisible(0, scroll_widget_->size().height(), 0, 0); + scroll_area_->verticalScrollBar()->setValue(max); isInitialized = true; return; } @@ -226,11 +226,6 @@ int TimelineView::addEvents(const Timeline &timeline) QSettings settings; QString localUser = settings.value("auth/user_id").toString(); - if (isInitialSync) { - prev_batch_token_ = timeline.previousBatch(); - isInitialSync = false; - } - for (const auto &event : timeline.events()) { TimelineItem *item = parseMessageEvent(event.toObject(), TimelineDirection::Bottom); auto sender = event.toObject().value("sender").toString(); @@ -243,6 +238,13 @@ int TimelineView::addEvents(const Timeline &timeline) } } + if (isInitialSync) { + prev_batch_token_ = timeline.previousBatch(); + isInitialSync = false; + + client_->messages(room_id_, prev_batch_token_); + } + return message_count; } diff --git a/src/TimelineViewManager.cc b/src/TimelineViewManager.cc index 008dc5dc..f55d4868 100644 --- a/src/TimelineViewManager.cc +++ b/src/TimelineViewManager.cc @@ -119,9 +119,10 @@ void TimelineViewManager::setHistoryView(const QString &room_id) active_room_ = room_id; auto widget = views_.value(room_id); - widget->scrollDown(); setCurrentWidget(widget.data()); + + widget->scrollDown(); } QMap TimelineViewManager::NICK_COLORS;