Implement AFR sending

This commit is contained in:
nielsandriesse 2020-05-14 14:24:05 +10:00
parent 7a351592a4
commit f29f775cf9
2 changed files with 27 additions and 2 deletions

View File

@ -1,7 +1,9 @@
package org.thoughtcrime.securesms.loki.protocol package org.thoughtcrime.securesms.loki.protocol
import android.content.Context import android.content.Context
import android.util.Log
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil
import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.loki.utilities.recipient import org.thoughtcrime.securesms.loki.utilities.recipient
@ -10,6 +12,8 @@ import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.sms.OutgoingTextMessage import org.thoughtcrime.securesms.sms.OutgoingTextMessage
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.api.messages.SignalServiceContent import org.whispersystems.signalservice.api.messages.SignalServiceContent
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
import org.whispersystems.signalservice.api.push.SignalServiceAddress
import org.whispersystems.signalservice.loki.protocol.meta.SessionMetaProtocol import org.whispersystems.signalservice.loki.protocol.meta.SessionMetaProtocol
import org.whispersystems.signalservice.loki.protocol.multidevice.MultiDeviceProtocol import org.whispersystems.signalservice.loki.protocol.multidevice.MultiDeviceProtocol
import org.whispersystems.signalservice.loki.protocol.todo.LokiMessageFriendRequestStatus import org.whispersystems.signalservice.loki.protocol.todo.LokiMessageFriendRequestStatus
@ -48,7 +52,7 @@ object FriendRequestProtocol {
// Do nothing // Do nothing
} else if (!allUserDevices.contains(device) } else if (!allUserDevices.contains(device)
&& (deviceFRStatus == LokiThreadFriendRequestStatus.NONE || deviceFRStatus == LokiThreadFriendRequestStatus.REQUEST_EXPIRED)) { && (deviceFRStatus == LokiThreadFriendRequestStatus.NONE || deviceFRStatus == LokiThreadFriendRequestStatus.REQUEST_EXPIRED)) {
// TODO: Send AFR to contact (NOT their linked devices) sendAutoGeneratedFriendRequest(context, device)
} }
} }
} }
@ -293,4 +297,22 @@ object FriendRequestProtocol {
threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.NONE) threadDB.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.NONE)
} }
} }
fun sendAutoGeneratedFriendRequest(context: Context, publicKey: String) {
val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender()
val address = SignalServiceAddress(publicKey)
val recipient = recipient(context, publicKey)
val preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.number)
val message = SignalServiceDataMessage.newBuilder()
.asFriendRequest(true)
.withPreKeyBundle(preKeyBundle)
.withBody("")
val threadDB = DatabaseFactory.getThreadDatabase(context)
val uncheckedThreadID = threadDB.getThreadIdIfExistsFor(recipient)
val threadID = if (uncheckedThreadID == -1L) threadDB.getThreadIdFor(recipient) else uncheckedThreadID
setFriendRequestStatusToSendingIfNeeded(context, 0, threadID)
Log.d("Loki", "Sending auto generated friend request message to: $publicKey.")
val udAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient(context, publicKey))
messageSender.sendMessage(0, address, udAccess, message.build())
}
} }

View File

@ -104,7 +104,10 @@ object SyncMessagesProtocol {
val threadFRStatus = lokiThreadDB.getFriendRequestStatus(threadID) val threadFRStatus = lokiThreadDB.getFriendRequestStatus(threadID)
when (threadFRStatus) { when (threadFRStatus) {
LokiThreadFriendRequestStatus.NONE, LokiThreadFriendRequestStatus.REQUEST_EXPIRED -> { LokiThreadFriendRequestStatus.NONE, LokiThreadFriendRequestStatus.REQUEST_EXPIRED -> {
// TODO: Send AFR to contact AND THEIR LINKED DEVICES val contactLinkedDevices = MultiDeviceProtocol.shared.getAllLinkedDevices(contactPublicKey)
for (device in contactLinkedDevices) {
FriendRequestProtocol.sendAutoGeneratedFriendRequest(context, device)
}
} }
LokiThreadFriendRequestStatus.REQUEST_RECEIVED -> { LokiThreadFriendRequestStatus.REQUEST_RECEIVED -> {
FriendRequestProtocol.acceptFriendRequest(context, recipient(context, contactPublicKey)) // Takes into account multi device internally FriendRequestProtocol.acceptFriendRequest(context, recipient(context, contactPublicKey)) // Takes into account multi device internally