Model friend request status as an enum

This commit is contained in:
Niels Andriesse 2019-06-18 09:57:36 +10:00
parent d44081d040
commit 21e1ece19c
8 changed files with 38 additions and 42 deletions

View File

@ -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)

View File

@ -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);
}

View File

@ -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,

View File

@ -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);

View File

@ -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

View File

@ -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)
}

View File

@ -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() ))
}
}

View File

@ -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()
}
}