From 7b8fbcea4e3531fbc7a3be211674f08720bdd141 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 14 Apr 2020 16:31:31 +1000 Subject: [PATCH] Added ContactUtilities. --- .../activities/CreateClosedGroupLoader.kt | 29 ++------ .../redesign/utilities/ContactUtilities.kt | 66 +++++++++++++++++++ 2 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/loki/redesign/utilities/ContactUtilities.kt diff --git a/src/org/thoughtcrime/securesms/loki/redesign/activities/CreateClosedGroupLoader.kt b/src/org/thoughtcrime/securesms/loki/redesign/activities/CreateClosedGroupLoader.kt index 2c07fe0772..a10d1ac18f 100644 --- a/src/org/thoughtcrime/securesms/loki/redesign/activities/CreateClosedGroupLoader.kt +++ b/src/org/thoughtcrime/securesms/loki/redesign/activities/CreateClosedGroupLoader.kt @@ -1,33 +1,18 @@ package org.thoughtcrime.securesms.loki.redesign.activities import android.content.Context -import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.loki.redesign.utilities.ContactUtilities import org.thoughtcrime.securesms.util.AsyncLoader -import org.thoughtcrime.securesms.util.TextSecurePreferences -import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus class CreateClosedGroupLoader(context: Context) : AsyncLoader>(context) { override fun loadInBackground(): List { - val threadDatabase = DatabaseFactory.getThreadDatabase(context) - val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context) - val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) - val deviceLinks = DatabaseFactory.getLokiAPIDatabase(context).getDeviceLinks(userHexEncodedPublicKey) - val userLinkedDeviceHexEncodedPublicKeys = deviceLinks.flatMap { - listOf( it.masterHexEncodedPublicKey.toLowerCase(), it.slaveHexEncodedPublicKey.toLowerCase() ) - }.toMutableSet() - userLinkedDeviceHexEncodedPublicKeys.add(userHexEncodedPublicKey.toLowerCase()) - val cursor = threadDatabase.conversationList - val reader = threadDatabase.readerFor(cursor) - val result = mutableListOf() - while (reader.next != null) { - val thread = reader.current - if (thread.recipient.isGroupRecipient) { continue } - if (lokiThreadDatabase.getFriendRequestStatus(thread.threadId) != LokiThreadFriendRequestStatus.FRIENDS) { continue } - val hexEncodedPublicKey = thread.recipient.address.toString().toLowerCase() - if (userLinkedDeviceHexEncodedPublicKeys.contains(hexEncodedPublicKey)) { continue } - result.add(hexEncodedPublicKey) + val contacts = ContactUtilities.getAllContacts(context) + // Only show the master device of the users we are friends with + return contacts.filter { contact -> + !contact.recipient.isGroupRecipient && contact.isFriend && !contact.isOurDevice && !contact.isSlave + }.map { + it.recipient.address.toPhoneString() } - return result } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/redesign/utilities/ContactUtilities.kt b/src/org/thoughtcrime/securesms/loki/redesign/utilities/ContactUtilities.kt new file mode 100644 index 0000000000..c10aa3df6c --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/redesign/utilities/ContactUtilities.kt @@ -0,0 +1,66 @@ +package org.thoughtcrime.securesms.loki.redesign.utilities + +import android.content.Context +import org.thoughtcrime.securesms.database.DatabaseFactory +import org.thoughtcrime.securesms.recipients.Recipient +import org.thoughtcrime.securesms.util.TextSecurePreferences +import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus + +data class Contact( + val recipient: Recipient, + val threadId: Long, + val isFriend: Boolean, + val isSlave: Boolean, + val isOurDevice: Boolean +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other?.javaClass != javaClass) return false + + other as Contact + + return recipient == other.recipient + } + + override fun hashCode(): Int { + return recipient.hashCode() + } +} + +object ContactUtilities { + + @JvmStatic + fun getAllContacts(context: Context): Set { + val threadDatabase = DatabaseFactory.getThreadDatabase(context) + val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context) + val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) + val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context) + + val ourDeviceLinks = lokiAPIDatabase.getDeviceLinks(userHexEncodedPublicKey) + val ourDevices = ourDeviceLinks.flatMap { + listOf( it.masterHexEncodedPublicKey.toLowerCase(), it.slaveHexEncodedPublicKey.toLowerCase() ) + }.toMutableSet() + ourDevices.add(userHexEncodedPublicKey.toLowerCase()) + + val cursor = threadDatabase.conversationList + val reader = threadDatabase.readerFor(cursor) + val result = mutableSetOf() + while (reader.next != null) { + val thread = reader.current + val recipient = thread.recipient + val hexEncodedPublicKey = recipient.address.serialize() + + val isFriend = lokiThreadDatabase.getFriendRequestStatus(thread.threadId) == LokiThreadFriendRequestStatus.FRIENDS + var isSlave = false + if (!recipient.isGroupRecipient) { + val deviceLinks = lokiAPIDatabase.getDeviceLinks(hexEncodedPublicKey) + isSlave = deviceLinks.find { it.slaveHexEncodedPublicKey == hexEncodedPublicKey } != null + } + val isOurDevice = ourDevices.contains(hexEncodedPublicKey) + + result.add(Contact(recipient, thread.threadId, isFriend, isSlave, isOurDevice)) + } + return result + } + +} \ No newline at end of file