From ca370211f84a30f3671c665ca9707ec6bd4cf111 Mon Sep 17 00:00:00 2001
From: Niels Andriesse <andriesseniels@gmail.com>
Date: Fri, 28 Jun 2019 16:08:27 +1000
Subject: [PATCH] Resolve TODO

---
 .../securesms/ApplicationContext.java             |  9 ++++++---
 .../conversation/ConversationActivity.java        | 15 +++++++++++++--
 .../storage/TextSecureIdentityKeyStore.java       |  4 ++--
 .../thoughtcrime/securesms/database/Address.java  |  2 +-
 .../dependencies/SignalCommunicationModule.java   |  3 +--
 .../securesms/loki/KeyPairActivity.kt             |  3 +++
 6 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java
index 36c7dcd309..487a736e4e 100644
--- a/src/org/thoughtcrime/securesms/ApplicationContext.java
+++ b/src/org/thoughtcrime/securesms/ApplicationContext.java
@@ -107,7 +107,10 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
   private IncomingMessageObserver incomingMessageObserver;
   private ObjectGraph             objectGraph;
   private PersistentLogger        persistentLogger;
-  private LokiLongPoller          lokiLongPoller = null; // Loki
+
+  // Loki
+  private LokiLongPoller lokiLongPoller = null;
+  public SignalCommunicationModule communicationModule;
 
   private volatile boolean isAppVisible;
 
@@ -244,8 +247,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc
   }
 
   private void initializeDependencyInjection() {
-    this.objectGraph = ObjectGraph.create(new SignalCommunicationModule(this, new SignalServiceNetworkAccess(this)),
-                                          new AxolotlStorageModule(this));
+    communicationModule = new SignalCommunicationModule(this, new SignalServiceNetworkAccess(this));
+    this.objectGraph = ObjectGraph.create(communicationModule, new AxolotlStorageModule(this));
   }
 
   private void initializeGcmCheck() {
diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
index 80c141f9d0..05604c4bcd 100644
--- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
+++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
@@ -145,7 +145,6 @@ import org.thoughtcrime.securesms.database.model.MmsMessageRecord;
 import org.thoughtcrime.securesms.events.ReminderUpdateEvent;
 import org.thoughtcrime.securesms.giph.ui.GiphyActivity;
 import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob;
-import org.thoughtcrime.securesms.jobs.PushDecryptJob;
 import org.thoughtcrime.securesms.jobs.RetrieveProfileJob;
 import org.thoughtcrime.securesms.jobs.ServiceOutageDetectionJob;
 import org.thoughtcrime.securesms.linkpreview.LinkPreview;
@@ -211,6 +210,10 @@ import org.thoughtcrime.securesms.util.concurrent.SettableFuture;
 import org.thoughtcrime.securesms.util.views.Stub;
 import org.whispersystems.libsignal.InvalidMessageException;
 import org.whispersystems.libsignal.util.guava.Optional;
+import org.whispersystems.signalservice.api.SignalServiceMessageSender;
+import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
+import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus;
 
 import java.io.IOException;
@@ -2689,7 +2692,15 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
   public void acceptFriendRequest(@NotNull MessageRecord friendRequest) {
     DatabaseFactory.getLokiThreadFriendRequestDatabase(this).setFriendRequestStatus(this.threadId, LokiThreadFriendRequestStatus.FRIENDS);
     String contactID = DatabaseFactory.getThreadDatabase(this).getRecipientForThreadId(this.threadId).getAddress().toString();
-    new PushDecryptJob(this).sendEmptyMessage(contactID); // TODO: Use a better approach for this
+    SignalServiceMessageSender messageSender = ApplicationContext.getInstance(this).communicationModule.provideSignalMessageSender();
+    SignalServiceAddress address = new SignalServiceAddress(contactID);
+    SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), "");
+    Optional<UnidentifiedAccessPair> access = Optional.absent();
+    try {
+      messageSender.sendMessage(0, address, access, message); // The message ID doesn't matter
+    } catch (Exception e) {
+      Log.d("Loki", "Failed to send empty message to: " + contactID + ".");
+    }
   }
 
   @Override
diff --git a/src/org/thoughtcrime/securesms/crypto/storage/TextSecureIdentityKeyStore.java b/src/org/thoughtcrime/securesms/crypto/storage/TextSecureIdentityKeyStore.java
index 13b1becf8f..3faff38244 100644
--- a/src/org/thoughtcrime/securesms/crypto/storage/TextSecureIdentityKeyStore.java
+++ b/src/org/thoughtcrime/securesms/crypto/storage/TextSecureIdentityKeyStore.java
@@ -47,7 +47,7 @@ public class TextSecureIdentityKeyStore implements IdentityKeyStore {
   public boolean saveIdentity(SignalProtocolAddress address, IdentityKey identityKey, boolean nonBlockingApproval) {
     synchronized (LOCK) {
       IdentityDatabase         identityDatabase = DatabaseFactory.getIdentityDatabase(context);
-      Address                  signalAddress    = Address.fromExternal(context, address.getName());
+      Address                  signalAddress    = Address.fromSerialized(address.getName());
       Optional<IdentityRecord> identityRecord   = identityDatabase.getIdentity(signalAddress);
 
       if (!identityRecord.isPresent()) {
@@ -94,7 +94,7 @@ public class TextSecureIdentityKeyStore implements IdentityKeyStore {
     synchronized (LOCK) {
       IdentityDatabase identityDatabase = DatabaseFactory.getIdentityDatabase(context);
       String           ourNumber        = TextSecurePreferences.getLocalNumber(context);
-      Address          theirAddress     = Address.fromExternal(context, address.getName());
+      Address          theirAddress     = Address.fromSerialized(address.getName());
 
       if (ourNumber.equals(address.getName()) || Address.fromSerialized(ourNumber).equals(theirAddress)) {
         return identityKey.equals(IdentityKeyUtil.getIdentityKey(context));
diff --git a/src/org/thoughtcrime/securesms/database/Address.java b/src/org/thoughtcrime/securesms/database/Address.java
index a60028c7a6..5cb2cf258e 100644
--- a/src/org/thoughtcrime/securesms/database/Address.java
+++ b/src/org/thoughtcrime/securesms/database/Address.java
@@ -66,7 +66,7 @@ public class Address implements Parcelable, Comparable<Address> {
   }
 
   public static Address fromExternal(@NonNull Context context, @Nullable String external) {
-    return new Address(getExternalAddressFormatter(context).format(external));
+    return Address.fromSerialized(external);
   }
 
   public static @NonNull List<Address> fromSerializedList(@NonNull String serialized, char delimiter) {
diff --git a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java
index 14c4affc4b..43d92de914 100644
--- a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java
+++ b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java
@@ -42,7 +42,6 @@ import org.thoughtcrime.securesms.jobs.SendDeliveryReceiptJob;
 import org.thoughtcrime.securesms.jobs.SendReadReceiptJob;
 import org.thoughtcrime.securesms.jobs.TypingSendJob;
 import org.thoughtcrime.securesms.logging.Log;
-import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
 import org.thoughtcrime.securesms.preferences.AppProtectionPreferenceFragment;
 import org.thoughtcrime.securesms.push.SecurityEventListener;
 import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
@@ -127,7 +126,7 @@ public class SignalCommunicationModule {
   }
 
   @Provides
-  synchronized SignalServiceMessageSender provideSignalMessageSender() {
+  public synchronized SignalServiceMessageSender provideSignalMessageSender() {
     if (this.messageSender == null) {
       this.messageSender = new SignalServiceMessageSender(networkAccess.getConfiguration(context),
                                                           new DynamicCredentialsProvider(context),
diff --git a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
index 2927687eec..69a6c5edda 100644
--- a/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
+++ b/src/org/thoughtcrime/securesms/loki/KeyPairActivity.kt
@@ -17,6 +17,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory
 import org.thoughtcrime.securesms.database.IdentityDatabase
 import org.thoughtcrime.securesms.util.TextSecurePreferences
 import org.whispersystems.libsignal.IdentityKeyPair
+import org.whispersystems.libsignal.util.KeyHelper
 import org.whispersystems.signalservice.loki.crypto.MnemonicCodec
 import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
 import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
@@ -90,6 +91,8 @@ class KeyPairActivity : BaseActionBarActivity() {
     private fun register() {
         val publicKey = keyPair!!.publicKey
         val hexEncodedPublicKey = keyPair!!.hexEncodedPublicKey
+        val registrationID = KeyHelper.generateRegistrationId(false)
+        TextSecurePreferences.setLocalRegistrationId(this, registrationID)
         DatabaseFactory.getIdentityDatabase(this).saveIdentity(Address.fromSerialized(hexEncodedPublicKey), publicKey,
             IdentityDatabase.VerifiedStatus.VERIFIED, true, System.currentTimeMillis(), true)
         TextSecurePreferences.setLocalNumber(this, hexEncodedPublicKey)