2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.utilities
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2021-01-13 07:11:30 +01:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
2021-01-19 00:48:02 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2020-11-26 00:07:45 +01:00
|
|
|
import org.session.libsignal.service.loki.protocol.shelved.multidevice.MultiDeviceProtocol
|
2020-05-11 08:19:26 +02:00
|
|
|
|
|
|
|
data class Contact(
|
|
|
|
val recipient: Recipient,
|
|
|
|
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)
|
2021-01-19 00:48:02 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
2020-05-11 08:19:26 +02:00
|
|
|
val lokiAPIDatabase = DatabaseFactory.getLokiAPIDatabase(context)
|
2020-07-15 06:26:20 +02:00
|
|
|
val userDevices = MultiDeviceProtocol.shared.getAllLinkedDevices(userPublicKey)
|
2020-05-11 08:19:26 +02:00
|
|
|
val cursor = threadDatabase.conversationList
|
|
|
|
val result = mutableSetOf<Contact>()
|
|
|
|
threadDatabase.readerFor(cursor).use { reader ->
|
|
|
|
while (reader.next != null) {
|
|
|
|
val thread = reader.current
|
|
|
|
val recipient = thread.recipient
|
|
|
|
val publicKey = recipient.address.serialize()
|
|
|
|
val isUserDevice = userDevices.contains(publicKey)
|
|
|
|
var isSlave = false
|
|
|
|
if (!recipient.isGroupRecipient) {
|
|
|
|
val deviceLinks = lokiAPIDatabase.getDeviceLinks(publicKey)
|
2020-07-15 04:24:43 +02:00
|
|
|
isSlave = deviceLinks.find { it.slavePublicKey == publicKey } != null
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
2020-07-15 06:26:20 +02:00
|
|
|
result.add(Contact(recipient, isSlave, isUserDevice))
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|