From f29f775cf96ebc8207954a4ee3581584687874e8 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 14 May 2020 14:24:05 +1000 Subject: [PATCH] Implement AFR sending --- .../loki/protocol/FriendRequestProtocol.kt | 24 ++++++++++++++++++- .../loki/protocol/SyncMessagesProtocol.kt | 5 +++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/protocol/FriendRequestProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/FriendRequestProtocol.kt index a322f32efc..6736102fa0 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/FriendRequestProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/FriendRequestProtocol.kt @@ -1,7 +1,9 @@ package org.thoughtcrime.securesms.loki.protocol import android.content.Context +import android.util.Log import org.thoughtcrime.securesms.ApplicationContext +import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.DatabaseFactory 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.util.TextSecurePreferences 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.multidevice.MultiDeviceProtocol import org.whispersystems.signalservice.loki.protocol.todo.LokiMessageFriendRequestStatus @@ -48,7 +52,7 @@ object FriendRequestProtocol { // Do nothing } else if (!allUserDevices.contains(device) && (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) } } + + 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()) + } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/protocol/SyncMessagesProtocol.kt b/src/org/thoughtcrime/securesms/loki/protocol/SyncMessagesProtocol.kt index 20abf3471c..d17cb723d9 100644 --- a/src/org/thoughtcrime/securesms/loki/protocol/SyncMessagesProtocol.kt +++ b/src/org/thoughtcrime/securesms/loki/protocol/SyncMessagesProtocol.kt @@ -104,7 +104,10 @@ object SyncMessagesProtocol { val threadFRStatus = lokiThreadDB.getFriendRequestStatus(threadID) when (threadFRStatus) { 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 -> { FriendRequestProtocol.acceptFriendRequest(context, recipient(context, contactPublicKey)) // Takes into account multi device internally