From 21e1ece19c914e81d014dc3462138d69011023b6 Mon Sep 17 00:00:00 2001
From: Niels Andriesse <andriesseniels@gmail.com>
Date: Tue, 18 Jun 2019 09:57:36 +1000
Subject: [PATCH] Model friend request status as an enum

---
 .../conversation/ConversationActivity.java    |  2 +-
 .../securesms/database/DatabaseFactory.java   | 10 ++++-----
 .../securesms/database/SmsDatabase.java       |  4 ++--
 .../database/helpers/SQLCipherOpenHelper.java | 11 +++++-----
 .../securesms/jobs/PushDecryptJob.java        |  2 +-
 ...Status.java => LokiFriendRequestStatus.kt} | 19 ++++++++---------
 ...kt => LokiMessageFriendRequestDatabase.kt} | 11 ++++------
 .../loki/LokiThreadFriendRequestDatabase.kt   | 21 ++++++++++---------
 8 files changed, 38 insertions(+), 42 deletions(-)
 rename src/org/thoughtcrime/securesms/loki/{LokiFriendRequestStatus.java => LokiFriendRequestStatus.kt} (51%)
 rename src/org/thoughtcrime/securesms/loki/{LokiSmsFriendRequestDatabase.kt => LokiMessageFriendRequestDatabase.kt} (75%)

diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
index d89994f701..da31add1b6 100644
--- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
+++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java
@@ -2155,7 +2155,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
     }
 
     // Loki - Always send friend requests if we're not friends with the user
-    int friendRequestStatus = DatabaseFactory.getLokiThreadFriendRequestDatabase(context).getFriendRequestStatus(threadId);
+    LokiFriendRequestStatus friendRequestStatus = DatabaseFactory.getLokiThreadFriendRequestDatabase(context).getFriendRequestStatus(threadId);
     message.isFriendRequest = (friendRequestStatus != LokiFriendRequestStatus.FRIENDS);
 
     Permissions.with(this)
diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java
index 0dcd665172..36d43300ba 100644
--- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java
+++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java
@@ -34,7 +34,7 @@ import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
 import org.thoughtcrime.securesms.loki.LokiAPIDatabase;
 import org.thoughtcrime.securesms.loki.LokiContactPreKeyDatabase;
 import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
-import org.thoughtcrime.securesms.loki.LokiSmsFriendRequestDatabase;
+import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase;
 import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase;
 import org.thoughtcrime.securesms.util.TextSecurePreferences;
 
@@ -68,7 +68,7 @@ public class DatabaseFactory {
   private final LokiAPIDatabase lokiAPIDatabase;
   private final LokiContactPreKeyDatabase lokiContactPreKeyDatabase;
   private final LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase;
-  private final LokiSmsFriendRequestDatabase lokiSmsFriendRequestDatabase;
+  private final LokiMessageFriendRequestDatabase lokiMessageFriendRequestDatabase;
   private final LokiThreadFriendRequestDatabase lokiThreadFriendRequestDatabase;
 
   public static DatabaseFactory getInstance(Context context) {
@@ -169,8 +169,8 @@ public class DatabaseFactory {
     return getInstance(context).lokiPreKeyBundleDatabase;
   }
 
-  public static  LokiSmsFriendRequestDatabase getLokiSmsFriendRequestDatabase(Context context) {
-    return getInstance(context).lokiSmsFriendRequestDatabase;
+  public static LokiMessageFriendRequestDatabase getLokiSmsFriendRequestDatabase(Context context) {
+    return getInstance(context).lokiMessageFriendRequestDatabase;
   }
 
   public static  LokiThreadFriendRequestDatabase getLokiThreadFriendRequestDatabase(Context context) {
@@ -212,7 +212,7 @@ public class DatabaseFactory {
     this.lokiAPIDatabase = new LokiAPIDatabase(context, databaseHelper);
     this.lokiContactPreKeyDatabase = new LokiContactPreKeyDatabase(context, databaseHelper);
     this.lokiPreKeyBundleDatabase = new LokiPreKeyBundleDatabase(context, databaseHelper);
-    this.lokiSmsFriendRequestDatabase = new LokiSmsFriendRequestDatabase(context, databaseHelper);
+    this.lokiMessageFriendRequestDatabase = new LokiMessageFriendRequestDatabase(context, databaseHelper);
     this.lokiThreadFriendRequestDatabase = new LokiThreadFriendRequestDatabase(context, databaseHelper);
   }
 
diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java
index 6172cfeac4..e5d5fe5099 100644
--- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java
+++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java
@@ -661,7 +661,7 @@ public class SmsDatabase extends MessagingDatabase {
       ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(threadId));
     }
 
-    // Loki: Save friend request state on sms
+    // Loki - Save friend request state on sms
     if (message.isFriendRequest) {
       DatabaseFactory.getLokiSmsFriendRequestDatabase(context).setIsFriendRequest(messageId, message.isFriendRequest);
     }
@@ -879,7 +879,7 @@ public class SmsDatabase extends MessagingDatabase {
       List<IdentityKeyMismatch> mismatches = getMismatches(mismatchDocument);
       Recipient                 recipient  = Recipient.from(context, address, true);
 
-      // Loki: Check to see if this message was a friend request
+      // Loki - Check to see if this message was a friend request
       boolean isFriendRequest = DatabaseFactory.getLokiSmsFriendRequestDatabase(context).getIsFriendRequest(messageId);
 
       return new SmsMessageRecord(messageId, body, recipient,
diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java
index b69c9caa2d..e8c3e2662e 100644
--- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java
+++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java
@@ -9,10 +9,6 @@ import android.os.SystemClock;
 import android.support.annotation.NonNull;
 import android.text.TextUtils;
 
-import org.thoughtcrime.securesms.database.Address;
-import org.thoughtcrime.securesms.database.JobDatabase;
-import org.thoughtcrime.securesms.logging.Log;
-
 import net.sqlcipher.database.SQLiteDatabase;
 import net.sqlcipher.database.SQLiteDatabaseHook;
 import net.sqlcipher.database.SQLiteOpenHelper;
@@ -20,11 +16,13 @@ import net.sqlcipher.database.SQLiteOpenHelper;
 import org.thoughtcrime.securesms.ApplicationContext;
 import org.thoughtcrime.securesms.crypto.DatabaseSecret;
 import org.thoughtcrime.securesms.crypto.MasterSecret;
+import org.thoughtcrime.securesms.database.Address;
 import org.thoughtcrime.securesms.database.AttachmentDatabase;
 import org.thoughtcrime.securesms.database.DraftDatabase;
 import org.thoughtcrime.securesms.database.GroupDatabase;
 import org.thoughtcrime.securesms.database.GroupReceiptDatabase;
 import org.thoughtcrime.securesms.database.IdentityDatabase;
+import org.thoughtcrime.securesms.database.JobDatabase;
 import org.thoughtcrime.securesms.database.MmsDatabase;
 import org.thoughtcrime.securesms.database.OneTimePreKeyDatabase;
 import org.thoughtcrime.securesms.database.PushDatabase;
@@ -35,10 +33,11 @@ import org.thoughtcrime.securesms.database.SignedPreKeyDatabase;
 import org.thoughtcrime.securesms.database.SmsDatabase;
 import org.thoughtcrime.securesms.database.ThreadDatabase;
 import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob;
+import org.thoughtcrime.securesms.logging.Log;
 import org.thoughtcrime.securesms.loki.LokiAPIDatabase;
 import org.thoughtcrime.securesms.loki.LokiContactPreKeyDatabase;
+import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase;
 import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
-import org.thoughtcrime.securesms.loki.LokiSmsFriendRequestDatabase;
 import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase;
 import org.thoughtcrime.securesms.notifications.NotificationChannels;
 import org.thoughtcrime.securesms.service.KeyCachingService;
@@ -122,7 +121,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
     db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand());
     db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand());
     db.execSQL(LokiContactPreKeyDatabase.getCreateTableCommand());
-    db.execSQL(LokiSmsFriendRequestDatabase.getCreateTableCommand());
+    db.execSQL(LokiMessageFriendRequestDatabase.getCreateTableCommand());
     db.execSQL(LokiThreadFriendRequestDatabase.getCreateTableCommand());
 
     executeStatements(db, SmsDatabase.CREATE_INDEXS);
diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
index 89b830adee..91cbafe9d2 100644
--- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
+++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java
@@ -830,7 +830,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
     long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient);
 
     LokiThreadFriendRequestDatabase database = DatabaseFactory.getLokiThreadFriendRequestDatabase(context);
-    int friendRequestStatus = database.getFriendRequestStatus(threadId);
+    LokiFriendRequestStatus friendRequestStatus = database.getFriendRequestStatus(threadId);
     if (envelope.isFriendRequest()) {
       if (friendRequestStatus == LokiFriendRequestStatus.REQUEST_SENT) {
         // This can happen if Alice sent Bob a friend request, Bob declined, but then Bob changed his
diff --git a/src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.java b/src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.kt
similarity index 51%
rename from src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.java
rename to src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.kt
index 626ca534de..4da237afb9 100644
--- a/src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.java
+++ b/src/org/thoughtcrime/securesms/loki/LokiFriendRequestStatus.kt
@@ -1,17 +1,16 @@
-package org.thoughtcrime.securesms.loki;
+package org.thoughtcrime.securesms.loki
 
-public class LokiFriendRequestStatus {
+enum class LokiFriendRequestStatus(val rawValue: Int) {
     // New conversation; no messages sent or received.
-    public static final int NONE = 0;
+    NONE(0),
     // This state is used to lock the input early while sending.
-    public static final int REQUEST_SENDING = 1;
+    REQUEST_SENDING(1),
     // Friend request sent; awaiting response.
-    public static final int REQUEST_SENT = 2;
+    REQUEST_SENT(2),
     // Friend request received; awaiting user input.
-    public static final int REQUEST_RECEIVED = 3;
+    REQUEST_RECEIVED(3),
     // We are friends with the user in this thread.
-    public static final int FRIENDS = 4;
+    FRIENDS(4),
     // A friend request was sent, but it timed out (i.e other user didn't accept within the allocated time)
-    public static final int REQUEST_EXPIRED = 5;
-}
-
+    REQUEST_EXPIRED(5)
+}
\ No newline at end of file
diff --git a/src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt
similarity index 75%
rename from src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt
rename to src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt
index 0b2714c62f..69cfe3299d 100644
--- a/src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt
+++ b/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt
@@ -5,10 +5,7 @@ import android.content.Context
 import org.thoughtcrime.securesms.database.Database
 import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
 
-/**
- * A database for associating friend request data to Sms
- */
-class LokiSmsFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
+class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
 
     companion object {
         private val tableName = "loki_sms_friend_request_database"
@@ -21,8 +18,8 @@ class LokiSmsFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper
 
     fun getIsFriendRequest(messageId: Long): Boolean {
         val database = databaseHelper.readableDatabase
-        return database.get(tableName, ID_WHERE, arrayOf(messageId.toString())) { cursor ->
-            val rawIsFriendRequest =  cursor.getInt(isFriendRequest)
+        return database.get(tableName, ID_WHERE, arrayOf( messageId.toString() )) { cursor ->
+            val rawIsFriendRequest = cursor.getInt(isFriendRequest)
             rawIsFriendRequest == 1
         } ?: false
     }
@@ -36,6 +33,6 @@ class LokiSmsFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper
         contentValues.put(smsId, messageId)
         contentValues.put(Companion.isFriendRequest, rawIsFriendRequest)
 
-        database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf(messageId.toString()))
+        database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf( messageId.toString() ))
     }
 }
\ No newline at end of file
diff --git a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt
index 147d3ccdab..f433d72ec7 100644
--- a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt
+++ b/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt
@@ -5,9 +5,6 @@ import android.content.Context
 import org.thoughtcrime.securesms.database.Database
 import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
 
-/**
- * A database for associating friend request data to Threads
- */
 class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
 
     companion object {
@@ -19,20 +16,24 @@ class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHel
         val createTableCommand = "CREATE TABLE $tableName ($threadId INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);"
     }
 
-    fun getFriendRequestStatus(threadId: Long): Int {
+    fun getFriendRequestStatus(threadId: Long): LokiFriendRequestStatus {
         val db = databaseHelper.readableDatabase
-        return db.get(tableName, ID_WHERE, arrayOf(threadId.toString())) { cursor ->
+        val result = db.get(tableName, ID_WHERE, arrayOf( threadId.toString() )) { cursor ->
             cursor.getInt(friendRequestStatus)
-        } ?: LokiFriendRequestStatus.NONE
+        }
+        return if (result != null) {
+            LokiFriendRequestStatus.values().first { it.rawValue == result }
+        } else {
+            LokiFriendRequestStatus.NONE
+        }
     }
 
-    fun setFriendRequestStatus(threadId: Long, status: Int) {
+    fun setFriendRequestStatus(threadId: Long, status: LokiFriendRequestStatus) {
         val database = databaseHelper.writableDatabase
         val contentValues = ContentValues(1)
         contentValues.put(Companion.threadId, threadId)
-        contentValues.put(friendRequestStatus, status)
-
-        database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf(threadId.toString()))
+        contentValues.put(friendRequestStatus, status.rawValue)
+        database.insertOrUpdate(tableName, contentValues, ID_WHERE, arrayOf( threadId.toString() ))
         notifyConversationListListeners()
     }
 }
\ No newline at end of file