Skip to content
Snippets Groups Projects
Commit 2e766860 authored by Adrien Béraud's avatar Adrien Béraud Committed by Adrien Béraud
Browse files

ContactList: avoid re-saving device list n times on load

Change-Id: I8f11b6ede9701ba7770c7c774e87b6028dda117b
parent c96ea3ee
No related branches found
No related tags found
No related merge requests found
......@@ -425,6 +425,7 @@ ContactList::discardTrustRequest(const dht::InfoHash& from)
void
ContactList::loadKnownDevices()
{
auto& certStore = jami::Manager::instance().certStore(accountId_);
try {
// read file
auto file = fileutils::loadFile("knownDevices", path_);
......@@ -434,32 +435,19 @@ ContactList::loadKnownDevices()
std::map<dht::PkId, std::pair<std::string, uint64_t>> knownDevices;
oh.get().convert(knownDevices);
for (const auto& d : knownDevices) {
if (auto crt = jami::Manager::instance().certStore(accountId_).getCertificate(d.first.toString())) {
if (not foundAccountDevice(crt, d.second.first, clock::from_time_t(d.second.second)))
JAMI_WARN("[Contacts] Unable to add device %s", d.first.toString().c_str());
if (auto crt = certStore.getCertificate(d.first.toString())) {
if (not foundAccountDevice(crt, d.second.first, clock::from_time_t(d.second.second), false))
JAMI_WARNING("[Account {}] [Contacts] Unable to add device {}", accountId_, d.first);
} else {
JAMI_WARN("[Contacts] Unable to find certificate for device %s",
d.first.toString().c_str());
JAMI_WARNING("[Contacts] Unable to find certificate for device {}",
d.first);
}
}
} catch (const std::exception& e) {
// Legacy fallback
try {
auto file = fileutils::loadFile("knownDevicesNames", path_);
msgpack::object_handle oh = msgpack::unpack((const char*) file.data(), file.size());
std::map<dht::InfoHash, std::pair<std::string, uint64_t>> knownDevices;
oh.get().convert(knownDevices);
for (const auto& d : knownDevices) {
if (auto crt = jami::Manager::instance().certStore(accountId_).getCertificate(d.first.toString())) {
if (not foundAccountDevice(crt,
d.second.first,
clock::from_time_t(d.second.second)))
JAMI_WARN("[Contacts] Unable to add device %s", d.first.toString().c_str());
}
}
} catch (const std::exception& e) {
JAMI_WARN("[Contacts] Error loading devices: %s", e.what());
if (not knownDevices.empty()) {
callbacks_.devicesChanged(knownDevices_);
}
} catch (const std::exception& e) {
JAMI_WARN("[Contacts] Error loading devices: %s", e.what());
return;
}
}
......@@ -470,9 +458,10 @@ ContactList::saveKnownDevices() const
std::ofstream file(path_ / "knownDevices", std::ios::trunc | std::ios::binary);
std::map<dht::PkId, std::pair<std::string, uint64_t>> devices;
for (const auto& id : knownDevices_)
for (const auto& id : knownDevices_) {
devices.emplace(id.first,
std::make_pair(id.second.name, clock::to_time_t(id.second.last_sync)));
}
msgpack::pack(file, devices);
}
......@@ -485,15 +474,14 @@ ContactList::foundAccountDevice(const dht::PkId& device,
// insert device
auto it = knownDevices_.emplace(device, KnownDevice {{}, name, updated});
if (it.second) {
JAMI_DBG("[Contacts] Found account device: %s %s", name.c_str(), device.toString().c_str());
JAMI_LOG("[Account {}] [Contacts] Found account device: {} {}", accountId_, name, device);
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
} else {
// update device name
if (not name.empty() and it.first->second.name != name) {
JAMI_DBG("[Contacts] Updating device name: %s %s",
name.c_str(),
device.toString().c_str());
JAMI_LOG("[Account {}] [Contacts] Updating device name: {} {}", accountId_,
name, device);
it.first->second.name = name;
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
......@@ -504,7 +492,8 @@ ContactList::foundAccountDevice(const dht::PkId& device,
bool
ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt,
const std::string& name,
const time_point& updated)
const time_point& updated,
bool notify)
{
if (not crt)
return false;
......@@ -531,15 +520,19 @@ ContactList::foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>&
trust_->setCertificateStatus(crt, dhtnet::tls::TrustStore::PermissionStatus::BANNED, false);
}
}
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
if (notify) {
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
}
} else {
// update device name
if (not name.empty() and it.first->second.name != name) {
JAMI_LOG("[Contacts] updating device name: {} {}", name, id);
it.first->second.name = name;
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
if (notify) {
saveKnownDevices();
callbacks_.devicesChanged(knownDevices_);
}
}
}
return true;
......
......@@ -132,7 +132,8 @@ public:
const time_point& last_sync = time_point::min());
bool foundAccountDevice(const std::shared_ptr<dht::crypto::Certificate>& crt,
const std::string& name = {},
const time_point& last_sync = time_point::min());
const time_point& last_sync = time_point::min(),
bool notify = true);
bool removeAccountDevice(const dht::PkId& device);
void setAccountDeviceName(const dht::PkId& device, const std::string& name);
std::string getAccountDeviceName(const dht::PkId& device) const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment