Keep syncing regardless of connectivity (#93)

This commit is contained in:
Konstantinos Sideris 2018-01-13 22:25:15 +02:00
parent d3603606e7
commit d31a08f1d5
3 changed files with 18 additions and 15 deletions

View file

@ -86,7 +86,6 @@ private slots:
void setOwnAvatar(const QPixmap &img); void setOwnAvatar(const QPixmap &img);
void initialSyncCompleted(const mtx::responses::Sync &response); void initialSyncCompleted(const mtx::responses::Sync &response);
void syncCompleted(const mtx::responses::Sync &response); void syncCompleted(const mtx::responses::Sync &response);
void syncFailed(const QString &msg);
void changeTopRoomInfo(const QString &room_id); void changeTopRoomInfo(const QString &room_id);
void logout(); void logout();
void addRoom(const QString &room_id); void addRoom(const QString &room_id);
@ -157,6 +156,7 @@ private:
// Safety net if consensus is not possible or too slow. // Safety net if consensus is not possible or too slow.
QTimer *showContentTimer_; QTimer *showContentTimer_;
QTimer *consensusTimer_; QTimer *consensusTimer_;
QTimer *syncTimeoutTimer_;
QString current_room_; QString current_room_;
QString current_community_; QString current_community_;

View file

@ -43,7 +43,7 @@
#include "timeline/TimelineViewManager.h" #include "timeline/TimelineViewManager.h"
constexpr int MAX_INITIAL_SYNC_FAILURES = 5; constexpr int MAX_INITIAL_SYNC_FAILURES = 5;
constexpr int SYNC_RETRY_TIMEOUT = 10000; constexpr int SYNC_RETRY_TIMEOUT = 40000;
ChatPage *ChatPage::instance_ = nullptr; ChatPage *ChatPage::instance_ = nullptr;
@ -304,7 +304,6 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
client_->initialSync(); client_->initialSync();
}); });
connect(client_.data(), &MatrixClient::syncCompleted, this, &ChatPage::syncCompleted); connect(client_.data(), &MatrixClient::syncCompleted, this, &ChatPage::syncCompleted);
connect(client_.data(), &MatrixClient::syncFailed, this, &ChatPage::syncFailed);
connect(client_.data(), connect(client_.data(),
&MatrixClient::getOwnProfileResponse, &MatrixClient::getOwnProfileResponse,
this, this,
@ -365,6 +364,17 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
} }
}); });
syncTimeoutTimer_ = new QTimer(this);
connect(syncTimeoutTimer_, &QTimer::timeout, this, [=]() {
if (client_->getHomeServer().isEmpty()) {
syncTimeoutTimer_->stop();
return;
}
qDebug() << "Sync took too long. Retrying...";
client_->sync();
});
connect(communitiesList_, connect(communitiesList_,
&CommunitiesList::communityChanged, &CommunitiesList::communityChanged,
this, this,
@ -474,20 +484,11 @@ ChatPage::setOwnAvatar(const QPixmap &img)
user_info_widget_->setAvatar(img.toImage()); user_info_widget_->setAvatar(img.toImage());
} }
void
ChatPage::syncFailed(const QString &msg)
{
// Stop if sync is not active. e.g user is logged out.
if (client_->getHomeServer().isEmpty())
return;
qWarning() << "Sync error:" << msg;
QTimer::singleShot(SYNC_RETRY_TIMEOUT, this, [=]() { client_->sync(); });
}
void void
ChatPage::syncCompleted(const mtx::responses::Sync &response) ChatPage::syncCompleted(const mtx::responses::Sync &response)
{ {
syncTimeoutTimer_->stop();
updateJoinedRooms(response.rooms.join); updateJoinedRooms(response.rooms.join);
removeLeftRooms(response.rooms.leave); removeLeftRooms(response.rooms.leave);
@ -504,6 +505,8 @@ ChatPage::syncCompleted(const mtx::responses::Sync &response)
client_->setNextBatchToken(nextBatchToken); client_->setNextBatchToken(nextBatchToken);
client_->sync(); client_->sync();
syncTimeoutTimer_->start(SYNC_RETRY_TIMEOUT);
} }
void void

View file

@ -260,7 +260,7 @@ MatrixClient::sync() noexcept
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status == 0 || status >= 400) { if (status == 0 || status >= 400) {
emit syncFailed(reply->errorString()); qDebug() << reply->errorString();
return; return;
} }