Skip to content
Snippets Groups Projects
Commit da09c939 authored by Hugo Lefeuvre's avatar Hugo Lefeuvre Committed by Andreas Traczyk
Browse files

contactmodel: remove old lrc


Change-Id: I7861c266d67d5e14e51c043b1ee7c074e74596e1
Reviewed-by: default avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent 6dff5ee3
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
* You should have received a copy of the GNU General Public License * * You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. * * along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#include "api/contactmodel.h"
// Std // Std
#include <algorithm> #include <algorithm>
...@@ -28,19 +27,17 @@ ...@@ -28,19 +27,17 @@
#include <account_const.h> #include <account_const.h>
// LRC // LRC
#include "api/contactmodel.h"
#include "api/account.h" #include "api/account.h"
#include "api/contact.h" #include "api/contact.h"
#include "api/interaction.h" #include "api/interaction.h"
#include "api/newaccountmodel.h" #include "api/newaccountmodel.h"
#include "api/newcallmodel.h" #include "api/newcallmodel.h"
#include "api/conversationmodel.h" #include "api/conversationmodel.h"
#include "api/newaccountmodel.h"
#include "callbackshandler.h" #include "callbackshandler.h"
#include "uri.h"
#include "accountmodel.h"
#include "contactmethod.h"
#include "namedirectory.h"
#include "phonedirectorymodel.h"
#include "bannedcontactmodel.h"
#include "private/vcardutils.h" #include "private/vcardutils.h"
#include "authority/daemon.h" #include "authority/daemon.h"
...@@ -64,24 +61,27 @@ public: ...@@ -64,24 +61,27 @@ public:
const BehaviorController& behaviorController); const BehaviorController& behaviorController);
~ContactModelPimpl(); ~ContactModelPimpl();
/** /**
* Fills with contacts based on database's requests * Fills with contacts based on database's requests
* @return if the method succeeds * @return if the method succeeds
*/ */
bool fillsWithSIPContacts(); bool fillsWithSIPContacts();
/** /**
* Fills with contacts based on daemon's requests * Fills with contacts based on daemon's requests
* @return if the method succeeds * @return if the method succeeds
*/ */
bool fillsWithRINGContacts(); bool fillsWithRINGContacts();
/** /**
* Add a contact::Info to contacts. * Add a contact::Info to contacts.
* @note: the cm must corresponds to a profile in the database. * @note: the contactId must corresponds to a profile in the database.
* @param cm ContactMethod. * @param contactId
* @param type * @param type
* @param banned whether contact is banned or not * @param banned whether contact is banned or not
*/ */
void addToContacts(ContactMethod* cm, const profile::Type& type, bool banned = false); void addToContacts(const std::string& contactId, const profile::Type& type, bool banned = false);
/** /**
* Helpers for searchContact. Search for a given RING or SIP contact. * Helpers for searchContact. Search for a given RING or SIP contact.
*/ */
...@@ -111,6 +111,7 @@ public Q_SLOTS: ...@@ -111,6 +111,7 @@ public Q_SLOTS:
* @param status * @param status
*/ */
void slotNewBuddySubscription(const std::string& uri, bool status); void slotNewBuddySubscription(const std::string& uri, bool status);
/** /**
* Listen CallbacksHandler when a contact is added * Listen CallbacksHandler when a contact is added
* @param accountId * @param accountId
...@@ -118,6 +119,7 @@ public Q_SLOTS: ...@@ -118,6 +119,7 @@ public Q_SLOTS:
* @param confirmed * @param confirmed
*/ */
void slotContactAdded(const std::string& accountId, const std::string& contactUri, bool confirmed); void slotContactAdded(const std::string& accountId, const std::string& contactUri, bool confirmed);
/** /**
* Listen CallbacksHandler when a contact is removed * Listen CallbacksHandler when a contact is removed
* @param accountId * @param accountId
...@@ -125,6 +127,7 @@ public Q_SLOTS: ...@@ -125,6 +127,7 @@ public Q_SLOTS:
* @param banned * @param banned
*/ */
void slotContactRemoved(const std::string& accountId, const std::string& contactUri, bool banned); void slotContactRemoved(const std::string& accountId, const std::string& contactUri, bool banned);
/** /**
* Listen CallbacksHandler when a registeredName is found * Listen CallbacksHandler when a registeredName is found
* @param accountId account linked * @param accountId account linked
...@@ -133,6 +136,7 @@ public Q_SLOTS: ...@@ -133,6 +136,7 @@ public Q_SLOTS:
* @param registeredName of the contact found * @param registeredName of the contact found
*/ */
void slotRegisteredNameFound(const std::string& accountId, int status, const std::string& uri, const std::string& registeredName); void slotRegisteredNameFound(const std::string& accountId, int status, const std::string& uri, const std::string& registeredName);
/** /**
* Listen CallbacksHandler when an incoming request arrives * Listen CallbacksHandler when an incoming request arrives
* @param accountId account linked * @param accountId account linked
...@@ -148,6 +152,7 @@ public Q_SLOTS: ...@@ -148,6 +152,7 @@ public Q_SLOTS:
* @param callId * @param callId
*/ */
void slotIncomingCall(const std::string& fromId, const std::string& callId); void slotIncomingCall(const std::string& fromId, const std::string& callId);
/** /**
* Listen from callbacksHandler for new account interaction and add pending contact if not present * Listen from callbacksHandler for new account interaction and add pending contact if not present
* @param accountId * @param accountId
...@@ -157,6 +162,7 @@ public Q_SLOTS: ...@@ -157,6 +162,7 @@ public Q_SLOTS:
void slotNewAccountMessage(std::string& accountId, void slotNewAccountMessage(std::string& accountId,
std::string& from, std::string& from,
std::map<std::string,std::string> payloads); std::map<std::string,std::string> payloads);
/** /**
* Listen from callbacksHandler to know when a file transfer interaction is incoming * Listen from callbacksHandler to know when a file transfer interaction is incoming
* @param dringId Daemon's ID for incoming transfer * @param dringId Daemon's ID for incoming transfer
...@@ -306,8 +312,8 @@ ContactModel::removeContact(const std::string& contactUri, bool banned) ...@@ -306,8 +312,8 @@ ContactModel::removeContact(const std::string& contactUri, bool banned)
if (emitContactRemoved) { if (emitContactRemoved) {
emit contactRemoved(contactUri); emit contactRemoved(contactUri);
} else { } else {
// NOTE: this method is async, slotContactRemoved will be call and // NOTE: this method is asynchronous, the model will be updated
// then the model will be updated. // in slotContactRemoved
daemon::removeContact(owner, contactUri, banned); daemon::removeContact(owner, contactUri, banned);
} }
} }
...@@ -386,9 +392,7 @@ ContactModelPimpl::searchRingContact(const URI& query) ...@@ -386,9 +392,7 @@ ContactModelPimpl::searchRingContact(const URI& query)
updateTemporaryMessage(tr("Searching…").toStdString(), uriID); updateTemporaryMessage(tr("Searching…").toStdString(), uriID);
// Default searching // Default searching
if (auto* account = AccountModel::instance().getById(linked.owner.id.c_str())) { ConfigurationManager::instance().lookupName(QString::fromStdString(linked.owner.id), "", QString::fromStdString(uriID));
account->lookupName(QString(uriID.c_str()));
}
} }
} }
...@@ -497,33 +501,27 @@ ContactModelPimpl::fillsWithSIPContacts() ...@@ -497,33 +501,27 @@ ContactModelPimpl::fillsWithSIPContacts()
} }
} }
} }
return true; return true;
} }
bool bool
ContactModelPimpl::fillsWithRINGContacts() { ContactModelPimpl::fillsWithRINGContacts() {
auto account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::fillsWithContacts(), nullptr";
return false;
}
// Add contacts from daemon // Add contacts from daemon
const VectorMapStringString& contacts_vector = ConfigurationManager::instance().getContacts(linked.owner.id.c_str()); const VectorMapStringString& contacts_vector = ConfigurationManager::instance().getContacts(linked.owner.id.c_str());
for (auto contact_info : contacts_vector) { for (auto contact_info : contacts_vector) {
auto cm = PhoneDirectoryModel::instance().getNumber(contact_info["id"], account);
std::lock_guard<std::mutex> lk(contactsMtx_); std::lock_guard<std::mutex> lk(contactsMtx_);
addToContacts(cm, linked.owner.profileInfo.type, contact_info["banned"] == "true"); bool banned = contact_info["banned"] == "true" ? true : false;
addToContacts(contact_info["id"].toStdString(), linked.owner.profileInfo.type, banned);
} }
// Add pending contacts // Add pending contacts
const VectorMapStringString& pending_tr {ConfigurationManager::instance().getTrustRequests(account->id())}; const VectorMapStringString& pending_tr {ConfigurationManager::instance().getTrustRequests(linked.owner.id.c_str())};
for (const auto& tr_info : pending_tr) { for (const auto& tr_info : pending_tr) {
// Get pending requests. // Get pending requests.
auto payload = tr_info[DRing::Account::TrustRequest::PAYLOAD].toUtf8(); auto payload = tr_info[DRing::Account::TrustRequest::PAYLOAD].toUtf8();
auto contactUri = tr_info[DRing::Account::TrustRequest::FROM]; auto contactUri = tr_info[DRing::Account::TrustRequest::FROM];
auto* cm = PhoneDirectoryModel::instance().getNumber(contactUri, account);
const auto vCard = VCardUtils::toHashMap(payload); const auto vCard = VCardUtils::toHashMap(payload);
const auto alias = vCard["FN"]; const auto alias = vCard["FN"];
...@@ -538,8 +536,8 @@ ContactModelPimpl::fillsWithRINGContacts() { ...@@ -538,8 +536,8 @@ ContactModelPimpl::fillsWithRINGContacts() {
contact::Info contactInfo; contact::Info contactInfo;
contactInfo.profileInfo = profileInfo; contactInfo.profileInfo = profileInfo;
contactInfo.registeredName = cm->registeredName().toStdString(); contactInfo.registeredName = "";
contactInfo.isBanned = account->bannedContactModel()->isBanned(cm); contactInfo.isBanned = false;
{ {
std::lock_guard<std::mutex> lk(contactsMtx_); std::lock_guard<std::mutex> lk(contactsMtx_);
...@@ -573,12 +571,6 @@ ContactModelPimpl::slotContactAdded(const std::string& accountId, const std::str ...@@ -573,12 +571,6 @@ ContactModelPimpl::slotContactAdded(const std::string& accountId, const std::str
{ {
Q_UNUSED(confirmed) Q_UNUSED(confirmed)
if (accountId != linked.owner.id) return; if (accountId != linked.owner.id) return;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotContactsAdded(), nullptr";
return;
}
auto* cm = PhoneDirectoryModel::instance().getNumber(QString(contactUri.c_str()), account);
auto contact = contacts.find(contactUri); auto contact = contacts.find(contactUri);
if (contact->second.profileInfo.type == profile::Type::PENDING) { if (contact->second.profileInfo.type == profile::Type::PENDING) {
...@@ -601,14 +593,9 @@ ContactModelPimpl::slotContactAdded(const std::string& accountId, const std::str ...@@ -601,14 +593,9 @@ ContactModelPimpl::slotContactAdded(const std::string& accountId, const std::str
// If contact is banned, do not re-add it, simply update its flag and the banned contacts list // If contact is banned, do not re-add it, simply update its flag and the banned contacts list
if (isBanned) { if (isBanned) {
bannedContacts.erase(it); bannedContacts.erase(it);
/* Update old LRC.
This method should NOT make any function call that requires the contactsMtx_ lock
otherwise we will get into a deadlock. This is only here for old-lrc transition. */
account->bannedContactModel()->remove(cm, false);
} }
addToContacts(cm, linked.owner.profileInfo.type, false); addToContacts(contactUri, linked.owner.profileInfo.type, false);
} }
} }
...@@ -641,16 +628,6 @@ ContactModelPimpl::slotContactRemoved(const std::string& accountId, const std::s ...@@ -641,16 +628,6 @@ ContactModelPimpl::slotContactRemoved(const std::string& accountId, const std::s
if (banned) { if (banned) {
contact->second.isBanned = true; contact->second.isBanned = true;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotContactsRemoved(), nullptr";
return;
}
auto* cm = PhoneDirectoryModel::instance().getNumber(QString(contactUri.c_str()), account);
// Update bannedContactModel from old LRC
account->bannedContactModel()->add(cm);
// Update bannedContacts index // Update bannedContacts index
bannedContacts.emplace_back(contact->second.profileInfo.uri); bannedContacts.emplace_back(contact->second.profileInfo.uri);
} else { } else {
...@@ -680,35 +657,32 @@ ContactModelPimpl::slotContactRemoved(const std::string& accountId, const std::s ...@@ -680,35 +657,32 @@ ContactModelPimpl::slotContactRemoved(const std::string& accountId, const std::s
} }
void void
ContactModelPimpl::addToContacts(ContactMethod* cm, const profile::Type& type, bool banned) ContactModelPimpl::addToContacts(const std::string& contactId, const profile::Type& type, bool banned)
{ {
if (!cm) { auto profileId = database::getOrInsertProfile(db, contactId, "", "",
qDebug() << "addToContacts: Called with NULL contact method.";
}
auto contactUri = cm->uri().toStdString();
auto contactId = database::getProfileId(db, contactUri);
if (contactId.empty()) {
contactId = database::getOrInsertProfile(db, contactUri, "", "",
to_string(linked.owner.profileInfo.type)); to_string(linked.owner.profileInfo.type));
}
auto contactInfo = database::buildContactFromProfileId(db, contactId); auto contactInfo = database::buildContactFromProfileId(db, profileId);
contactInfo.registeredName = cm->registeredName().toStdString();
contactInfo.isBanned = banned; contactInfo.isBanned = banned;
contactInfo.isPresent = cm->isPresent(); contactInfo.profileInfo.type = type; // PENDING should not be stored in the database
// lookup address in case of RING contact
if (type == profile::Type::RING) {
ConfigurationManager::instance().lookupAddress(QString::fromStdString(linked.owner.id),
"", QString::fromStdString(contactId));
}
contactInfo.profileInfo.type = type; // Because PENDING should not be stored in the database contactInfo.profileInfo.type = type; // Because PENDING should not be stored in the database
auto iter = contacts.find(contactInfo.profileInfo.uri); auto iter = contacts.find(contactInfo.profileInfo.uri);
if (iter != contacts.end()) { if (iter != contacts.end()) {
auto info = iter->second; auto info = iter->second;
contactInfo.registeredName = info.registeredName; contactInfo.registeredName = info.registeredName;
iter->second = contactInfo; iter->second = contactInfo;
} } else
else
contacts.emplace_hint(iter, contactInfo.profileInfo.uri, contactInfo); contacts.emplace_hint(iter, contactInfo.profileInfo.uri, contactInfo);
if (banned) { if (banned) {
bannedContacts.emplace_back(contactUri); bannedContacts.emplace_back(contactId);
} }
} }
...@@ -768,48 +742,33 @@ ContactModelPimpl::slotIncomingContactRequest(const std::string& accountId, ...@@ -768,48 +742,33 @@ ContactModelPimpl::slotIncomingContactRequest(const std::string& accountId,
if (linked.owner.id != accountId) if (linked.owner.id != accountId)
return; return;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotIncomingContactRequest(), nullptr";
return;
}
auto emitTrust = false; auto emitTrust = false;
{ {
std::lock_guard<std::mutex> lk(contactsMtx_); std::lock_guard<std::mutex> lk(contactsMtx_);
if (contacts.find(contactUri) == contacts.end()) { if (contacts.find(contactUri) == contacts.end()) {
auto* cm = PhoneDirectoryModel::instance().getNumber(URI(contactUri.c_str()), account);
const auto vCard = VCardUtils::toHashMap(payload.c_str()); const auto vCard = VCardUtils::toHashMap(payload.c_str());
const auto alias = vCard["FN"]; const auto alias = vCard["FN"];
const auto photo = (vCard.find("PHOTO;ENCODING=BASE64;TYPE=PNG") == vCard.end()) ? const auto photo = (vCard.find("PHOTO;ENCODING=BASE64;TYPE=PNG") == vCard.end()) ?
vCard["PHOTO;ENCODING=BASE64;TYPE=JPEG"] : vCard["PHOTO;ENCODING=BASE64;TYPE=PNG"]; vCard["PHOTO;ENCODING=BASE64;TYPE=JPEG"] : vCard["PHOTO;ENCODING=BASE64;TYPE=PNG"];
auto profileInfo = profile::Info {contactUri, photo.toStdString(), alias.toStdString(), profile::Type::PENDING}; auto profileInfo = profile::Info {contactUri, photo.toStdString(), alias.toStdString(), profile::Type::PENDING};
auto contactInfo = contact::Info {profileInfo, cm->bestName().toStdString(), cm->isConfirmed(), cm->isPresent()}; auto contactInfo = contact::Info {profileInfo, "", false, false, false};
contacts.emplace(contactUri, contactInfo); contacts.emplace(contactUri, contactInfo);
emitTrust = true; emitTrust = true;
database::getOrInsertProfile(db, contactUri, alias.toStdString(), database::getOrInsertProfile(db, contactUri, alias.toStdString(),
photo.toStdString(), photo.toStdString(),
profile::to_string(profile::Type::RING)); profile::to_string(profile::Type::RING));
} else }
return;
} }
emit linked.contactAdded(contactUri);
if (emitTrust) { if (emitTrust) {
emit linked.contactAdded(contactUri);
emit behaviorController.newTrustRequest(linked.owner.id, contactUri); emit behaviorController.newTrustRequest(linked.owner.id, contactUri);
} }
} }
void void
ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string& callId) ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string& callId)
{
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotIncomingCall(), nullptr";
return;
}
{ {
bool emitContactAdded = false; bool emitContactAdded = false;
{ {
...@@ -817,9 +776,8 @@ ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string ...@@ -817,9 +776,8 @@ ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string
if (contacts.find(fromId) == contacts.end()) { if (contacts.find(fromId) == contacts.end()) {
// Contact not found, load profile from database. // Contact not found, load profile from database.
// The conversation model will create an entry and link the incomingCall. // The conversation model will create an entry and link the incomingCall.
auto* cm = PhoneDirectoryModel::instance().getNumber(QString(fromId.c_str()), account);
auto type = (linked.owner.profileInfo.type == profile::Type::RING) ? profile::Type::PENDING : profile::Type::SIP; auto type = (linked.owner.profileInfo.type == profile::Type::RING) ? profile::Type::PENDING : profile::Type::SIP;
addToContacts(cm, type, false); addToContacts(fromId, type, false);
emitContactAdded = true; emitContactAdded = true;
} }
} }
...@@ -829,9 +787,8 @@ ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string ...@@ -829,9 +787,8 @@ ContactModelPimpl::slotIncomingCall(const std::string& fromId, const std::string
emit behaviorController.newTrustRequest(linked.owner.id, fromId); emit behaviorController.newTrustRequest(linked.owner.id, fromId);
} }
} }
emit linked.incomingCallFromPending(fromId, callId); emit linked.incomingCallFromPending(fromId, callId);
return;
}
} }
void void
...@@ -840,19 +797,14 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId, ...@@ -840,19 +797,14 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId,
std::map<std::string,std::string> payloads) std::map<std::string,std::string> payloads)
{ {
if (accountId != linked.owner.id) return; if (accountId != linked.owner.id) return;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotNewAccountMessage(), nullptr";
return;
}
auto emitNewTrust = false; auto emitNewTrust = false;
{ {
std::lock_guard<std::mutex> lk(contactsMtx_); std::lock_guard<std::mutex> lk(contactsMtx_);
if (contacts.find(from) == contacts.end()) { if (contacts.find(from) == contacts.end()) {
// Contact not found, load profile from database. // Contact not found, load profile from database.
// The conversation model will create an entry and link the incomingCall. // The conversation model will create an entry and link the incomingCall.
auto* cm = PhoneDirectoryModel::instance().getNumber(QString(from.c_str()), account); addToContacts(from, profile::Type::PENDING, false);
addToContacts(cm, profile::Type::PENDING, false);
emitNewTrust = true; emitNewTrust = true;
} }
} }
...@@ -866,25 +818,21 @@ void ...@@ -866,25 +818,21 @@ void
ContactModelPimpl::slotNewAccountTransfer(long long dringId, datatransfer::Info info) ContactModelPimpl::slotNewAccountTransfer(long long dringId, datatransfer::Info info)
{ {
if (info.accountId != linked.owner.id) return; if (info.accountId != linked.owner.id) return;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotNewAccountTransfer(), nullptr";
return;
}
bool emitNewTrust = false; bool emitNewTrust = false;
{ {
std::lock_guard<std::mutex> lk(contactsMtx_); std::lock_guard<std::mutex> lk(contactsMtx_);
if (contacts.find(info.peerUri) == contacts.end()) { if (contacts.find(info.peerUri) == contacts.end()) {
// Contact not found, load profile from database. // Contact not found, load profile from database.
// The conversation model will create an entry and link the incomingCall. // The conversation model will create an entry and link the incomingCall.
auto* cm = PhoneDirectoryModel::instance().getNumber(QString(info.peerUri.c_str()), account); addToContacts(info.peerUri, profile::Type::PENDING, false);
addToContacts(cm, profile::Type::PENDING, false);
emitNewTrust = true; emitNewTrust = true;
} }
} }
if (emitNewTrust) { if (emitNewTrust) {
emit behaviorController.newTrustRequest(linked.owner.id, info.peerUri); emit behaviorController.newTrustRequest(linked.owner.id, info.peerUri);
} }
emit linked.newAccountTransfer(dringId, info); emit linked.newAccountTransfer(dringId, info);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment