Added ContactUtilities.

This commit is contained in:
Mikunj 2020-04-14 16:31:31 +10:00
parent 11b5c78e3e
commit 7b8fbcea4e
2 changed files with 73 additions and 22 deletions

View File

@ -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<List<String>>(context) {
override fun loadInBackground(): List<String> {
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<String>()
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
}
}

View File

@ -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<Contact> {
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<Contact>()
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
}
}