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