[SMS App] Improve contact matching

Summary: Improve contact matching by checking if the requested address ends with a candidate phone number, or vice-versa

Test Plan:
Check that *all* conversations which correspond to a contact are matched
 - All of mine match. If someone finds one which doesn't, I will need the particulars...

Reviewers: #kde_connect, nicolasfella

Reviewed By: #kde_connect, nicolasfella

Subscribers: nicolasfella, kdeconnect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D15992
This commit is contained in:
Simon Redman 2018-10-07 21:32:01 -06:00
parent 5fe74ce041
commit ab33cce5a5

View file

@ -138,6 +138,7 @@ void ConversationListModel::createRowFromMessage(const QVariantMap& msg, int row
KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address)
{
const QString& canonicalAddress = canonicalizePhoneNumber(address);
int rowIndex = 0;
for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) {
const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString();
@ -146,7 +147,16 @@ KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString&
const QString& email = person->email();
const QString& phoneNumber = canonicalizePhoneNumber(person->contactCustomProperty("phoneNumber").toString());
if (address == email || canonicalizePhoneNumber(address) == phoneNumber) {
// To decide if a phone number matches:
// 1. Are they similar lengths? If two numbers are very different, probably one is junk data and should be ignored
// 2. Is one a superset of the other? Phone number digits get more specific the further towards the end of the string,
// so if one phone number ends with the other, it is probably just a more-complete version of the same thing
const QString& longerNumber = canonicalAddress.length() >= phoneNumber.length() ? canonicalAddress : phoneNumber;
const QString& shorterNumber = canonicalAddress.length() < phoneNumber.length() ? canonicalAddress : phoneNumber;
bool matchingPhoneNumber = longerNumber.endsWith(shorterNumber) && shorterNumber.length() * 2 >= longerNumber.length();
if (address == email || matchingPhoneNumber) {
qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name();
return person;
}
@ -165,5 +175,6 @@ QString ConversationListModel::canonicalizePhoneNumber(const QString& phoneNumbe
toReturn = toReturn.remove('(');
toReturn = toReturn.remove(')');
toReturn = toReturn.remove('+');
toReturn = toReturn.remove(QRegularExpression("^0*")); // Strip leading zeroes
return toReturn;
}