Fix bug where cache was initialized twice in a row

pull/1/head
Konstantinos Sideris 7 years ago
parent 36cb62748b
commit a97528b432
  1. 1
      include/ChatPage.h
  2. 18
      src/Cache.cc
  3. 116
      src/ChatPage.cc
  4. 14
      src/main.cc

@ -152,6 +152,7 @@ private:
void tryInitialSync();
void trySync();
void ensureOneTimeKeyCount(const std::map<std::string, uint16_t> &counts);
void getProfileInfo();
//! Check if the given room is currently open.
bool isRoomActive(const QString &room_id)

@ -87,8 +87,7 @@ init(const QString &user_id)
qRegisterMetaType<QMap<QString, RoomInfo>>();
qRegisterMetaType<std::map<QString, RoomInfo>>();
if (!instance_)
instance_ = std::make_unique<Cache>(user_id);
instance_ = std::make_unique<Cache>(user_id);
}
Cache *
@ -113,14 +112,16 @@ Cache::Cache(const QString &userId, QObject *parent)
, outboundMegolmSessionDb_{0}
, outboundOlmSessionDb_{0}
, localUserId_{userId}
{}
{
setup();
}
void
Cache::setup()
{
log::db()->debug("setting up cache");
auto statePath = QString("%1/%2/state")
auto statePath = QString("%1/%2")
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))
.arg(QString::fromUtf8(localUserId_.toUtf8().toHex()));
@ -558,10 +559,11 @@ Cache::nextBatchToken() const
void
Cache::deleteData()
{
log::db()->info("deleting data");
if (!cacheDirectory_.isEmpty())
// TODO: We need to remove the env_ while not accepting new requests.
if (!cacheDirectory_.isEmpty()) {
QDir(cacheDirectory_).removeRecursively();
log::db()->info("deleted cache files from disk");
}
}
bool
@ -575,7 +577,7 @@ Cache::isFormatValid()
txn.commit();
if (!res)
return false;
return true;
std::string stored_version(current_version.data(), current_version.size());

@ -696,69 +696,28 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
http::v2::client()->set_server(homeserver.toStdString());
http::v2::client()->set_access_token(token.toStdString());
http::v2::client()->get_profile(
userid.toStdString(),
[this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
if (err) {
log::net()->warn("failed to retrieve own profile info");
return;
}
emit setUserDisplayName(QString::fromStdString(res.display_name));
if (cache::client()) {
auto data = cache::client()->image(res.avatar_url);
if (!data.isNull()) {
emit setUserAvatar(QImage::fromData(data));
return;
}
}
if (res.avatar_url.empty())
return;
http::v2::client()->download(
res.avatar_url,
[this, res](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
log::net()->warn(
"failed to download user avatar: {} - {}",
mtx::errors::to_string(err->matrix_error.errcode),
err->matrix_error.error);
return;
}
if (cache::client())
cache::client()->saveImage(res.avatar_url, data);
emit setUserAvatar(
QImage::fromData(QByteArray(data.data(), data.size())));
});
});
// TODO http::client()->getOwnCommunities();
// The Olm client needs the user_id & device_id that will be included
// in the generated payloads & keys.
olm::client()->set_user_id(http::v2::client()->user_id().to_string());
olm::client()->set_device_id(http::v2::client()->device_id());
cache::init(userid);
try {
cache::client()->setup();
cache::init(userid);
if (!cache::client()->isFormatValid()) {
const bool isInitialized = cache::client()->isInitialized();
const bool isValid = cache::client()->isFormatValid();
if (isInitialized && !isValid) {
log::db()->warn("breaking changes in cache");
// TODO: Deleting session data but keep using the
// same device doesn't work.
cache::client()->deleteData();
cache::client()->setup();
cache::client()->setCurrentFormat();
}
if (cache::client()->isInitialized()) {
cache::init(userid);
cache::client()->setCurrentFormat();
} else if (isInitialized) {
loadStateFromCache();
// TODO: Bootstrap olm client with saved data.
return;
}
} catch (const lmdb::error &e) {
@ -783,6 +742,7 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
return;
}
getProfileInfo();
tryInitialSync();
}
@ -858,6 +818,8 @@ ChatPage::loadStateFromCache()
{
log::db()->info("restoring state from cache");
getProfileInfo();
QtConcurrent::run([this]() {
try {
cache::client()->restoreSessions();
@ -1315,3 +1277,53 @@ ChatPage::ensureOneTimeKeyCount(const std::map<std::string, uint16_t> &counts)
}
}
}
void
ChatPage::getProfileInfo()
{
QSettings settings;
const auto userid = settings.value("auth/user_id").toString().toStdString();
http::v2::client()->get_profile(
userid, [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) {
if (err) {
log::net()->warn("failed to retrieve own profile info");
return;
}
emit setUserDisplayName(QString::fromStdString(res.display_name));
if (cache::client()) {
auto data = cache::client()->image(res.avatar_url);
if (!data.isNull()) {
emit setUserAvatar(QImage::fromData(data));
return;
}
}
if (res.avatar_url.empty())
return;
http::v2::client()->download(
res.avatar_url,
[this, res](const std::string &data,
const std::string &,
const std::string &,
mtx::http::RequestErr err) {
if (err) {
log::net()->warn(
"failed to download user avatar: {} - {}",
mtx::errors::to_string(err->matrix_error.errcode),
err->matrix_error.error);
return;
}
if (cache::client())
cache::client()->saveImage(res.avatar_url, data);
emit setUserAvatar(
QImage::fromData(QByteArray(data.data(), data.size())));
});
});
// TODO http::client()->getOwnCommunities();
}

@ -17,6 +17,7 @@
#include <QApplication>
#include <QDesktopWidget>
#include <QDir>
#include <QFile>
#include <QFontDatabase>
#include <QLabel>
@ -48,6 +49,17 @@ screenCenter(int width, int height)
return QPoint(x, y);
}
void
createCacheDirectory()
{
auto dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
if (!QDir().mkpath(dir)) {
throw std::runtime_error(
("Unable to create state directory:" + dir).toStdString().c_str());
}
}
int
main(int argc, char *argv[])
{
@ -112,6 +124,8 @@ main(int argc, char *argv[])
http::init();
createCacheDirectory();
try {
log::init(QString("%1/nheko.log")
.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))

Loading…
Cancel
Save