forked from blue/squawk
some refactoring, some improvements
This commit is contained in:
parent
be466fbad1
commit
e31ef78e71
13 changed files with 188 additions and 147 deletions
|
@ -52,6 +52,7 @@ QXmppTask<QXmppOmemoStorage::OmemoData> Core::OmemoHandler::allData() {
|
|||
OmemoData data;
|
||||
data.ownDevice = ownDevice;
|
||||
|
||||
// LMDBAL::Transaction txn = db.beginReadOnlyTransaction(); TODO need to enable transaction after fixing LMDBAL
|
||||
std::map<uint32_t, QByteArray> pkeys = preKeyPairs->readAll();
|
||||
for (const std::pair<const uint32_t, QByteArray>& pair : pkeys) {
|
||||
data.preKeyPairs.insert(pair.first, pair.second);
|
||||
|
@ -73,28 +74,30 @@ QXmppTask<QXmppOmemoStorage::OmemoData> Core::OmemoHandler::allData() {
|
|||
|
||||
QXmppTask<void> Core::OmemoHandler::addDevice(const QString& jid, uint32_t deviceId, const QXmppOmemoStorage::Device& device) {
|
||||
QHash<uint32_t, Device> devs;
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
bool had = true;
|
||||
try {
|
||||
devs = devices->getRecord(jid);
|
||||
devices->getRecord(jid, devs, txn);
|
||||
} catch (const LMDBAL::NotFound& error) {
|
||||
had = false;
|
||||
}
|
||||
|
||||
devs.insert(deviceId, device);
|
||||
if (had) {
|
||||
devices->changeRecord(jid, devs);
|
||||
} else {
|
||||
devices->addRecord(jid, devs);
|
||||
}
|
||||
devs.insert(deviceId, device); //overwrites
|
||||
if (had)
|
||||
devices->changeRecord(jid, devs, txn);
|
||||
else
|
||||
devices->addRecord(jid, devs, txn);
|
||||
|
||||
txn.commit();
|
||||
return Core::makeReadyTask();
|
||||
}
|
||||
|
||||
QXmppTask<void> Core::OmemoHandler::addPreKeyPairs(const QHash<uint32_t, QByteArray>& keyPairs) {
|
||||
for (QHash<uint32_t, QByteArray>::const_iterator itr = keyPairs.begin(), end = keyPairs.end(); itr != end; ++itr) {
|
||||
preKeyPairs->forceRecord(itr.key(), itr.value());
|
||||
}
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
for (QHash<uint32_t, QByteArray>::const_iterator itr = keyPairs.begin(), end = keyPairs.end(); itr != end; ++itr)
|
||||
preKeyPairs->forceRecord(itr.key(), itr.value(), txn);
|
||||
|
||||
txn.commit();
|
||||
return Core::makeReadyTask();
|
||||
}
|
||||
|
||||
|
@ -104,13 +107,15 @@ QXmppTask<void> Core::OmemoHandler::addSignedPreKeyPair(uint32_t keyId, const QX
|
|||
}
|
||||
|
||||
QXmppTask<void> Core::OmemoHandler::removeDevice(const QString& jid, uint32_t deviceId) {
|
||||
QHash<uint32_t, Device> devs = devices->getRecord(jid);
|
||||
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
||||
QHash<uint32_t, Device> devs = devices->getRecord(jid, txn);
|
||||
devs.remove(deviceId);
|
||||
if (devs.isEmpty()) {
|
||||
devices->removeRecord(jid);
|
||||
} else {
|
||||
devices->changeRecord(jid, devs);
|
||||
}
|
||||
if (devs.isEmpty())
|
||||
devices->removeRecord(jid, txn);
|
||||
else
|
||||
devices->changeRecord(jid, devs, txn);
|
||||
|
||||
txn.commit();
|
||||
return Core::makeReadyTask();
|
||||
}
|
||||
|
||||
|
@ -120,7 +125,11 @@ QXmppTask<void> Core::OmemoHandler::removeDevices(const QString& jid) {
|
|||
}
|
||||
|
||||
QXmppTask<void> Core::OmemoHandler::removePreKeyPair(uint32_t keyId) {
|
||||
preKeyPairs->removeRecord(keyId);
|
||||
try {
|
||||
preKeyPairs->removeRecord(keyId);
|
||||
} catch (const LMDBAL::NotFound& e) {
|
||||
qDebug() << "Couldn't remove preKeyPair " << e.what();
|
||||
}
|
||||
return Core::makeReadyTask();
|
||||
}
|
||||
|
||||
|
@ -135,15 +144,12 @@ QXmppTask<void> Core::OmemoHandler::setOwnDevice(const std::optional<OwnDevice>&
|
|||
bool had = ownDevice.has_value();
|
||||
ownDevice = device;
|
||||
if (ownDevice.has_value()) {
|
||||
if (had) {
|
||||
if (had)
|
||||
meta->changeRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
|
||||
} else {
|
||||
else
|
||||
meta->addRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
|
||||
}
|
||||
} else {
|
||||
if (had) {
|
||||
meta->removeRecord("ownDevice");
|
||||
}
|
||||
} else if (had) {
|
||||
meta->removeRecord("ownDevice");
|
||||
}
|
||||
return Core::makeReadyTask();
|
||||
}
|
||||
|
@ -158,7 +164,7 @@ QXmppTask<void> Core::OmemoHandler::resetAll() {
|
|||
void Core::OmemoHandler::getDevices(const QString& jid, std::list<Shared::KeyInfo>& out) const {
|
||||
QHash<uint32_t, Device> devs;
|
||||
try {
|
||||
devs = devices->getRecord(jid);
|
||||
devices->getRecord(jid, devs);
|
||||
} catch (const LMDBAL::NotFound& error) {}
|
||||
|
||||
for (QHash<uint32_t, Device>::const_iterator itr = devs.begin(), end = devs.end(); itr != end; ++itr) {
|
||||
|
@ -169,6 +175,10 @@ void Core::OmemoHandler::getDevices(const QString& jid, std::list<Shared::KeyInf
|
|||
|
||||
void Core::OmemoHandler::requestBundles(const QString& jid) {
|
||||
QXmppTask<void> task = acc->om->buildMissingSessions({jid});
|
||||
Contact* cnt = acc->rh->getContact(jid);
|
||||
if (cnt)
|
||||
cnt->omemoBundles = Shared::Possible::discovering;
|
||||
|
||||
task.then(this, std::bind(&OmemoHandler::onBundlesReceived, this, jid));
|
||||
}
|
||||
|
||||
|
@ -191,6 +201,10 @@ void Core::OmemoHandler::onBundlesReceived(const QString& jid) {
|
|||
}
|
||||
}
|
||||
|
||||
Contact* cnt = acc->rh->getContact(jid);
|
||||
if (cnt)
|
||||
cnt->omemoBundles = Shared::Possible::present;
|
||||
|
||||
acc->delay->receivedBundles(jid, keys);
|
||||
}
|
||||
|
||||
|
@ -217,7 +231,6 @@ void Core::OmemoHandler::onOmemoDeviceAdded(const QString& jid, uint32_t id) {
|
|||
qDebug() << "OMEMO device added for" << jid;
|
||||
}
|
||||
|
||||
|
||||
QDataStream & operator >> (QDataStream& in, QXmppOmemoStorage::Device& device) {
|
||||
in >> device.label;
|
||||
in >> device.keyId;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue