diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index 0170777746..617bfce8e3 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -34,6 +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.util.TextSecurePreferences; public class DatabaseFactory { @@ -66,6 +67,7 @@ public class DatabaseFactory { private final LokiAPIDatabase lokiAPIDatabase; private final LokiContactPreKeyDatabase lokiContactPreKeyDatabase; private final LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase; + private final LokiSmsFriendRequestDatabase lokiSmsFriendRequestDatabase; public static DatabaseFactory getInstance(Context context) { synchronized (lock) { @@ -164,6 +166,10 @@ public class DatabaseFactory { public static LokiPreKeyBundleDatabase getLokiPreKeyBundleDatabase(Context context) { return getInstance(context).lokiPreKeyBundleDatabase; } + + public static LokiSmsFriendRequestDatabase getLokiSmsFriendRequestDatabase(Context context) { + return getInstance(context).lokiSmsFriendRequestDatabase; + } // endregion public static void upgradeRestored(Context context, SQLiteDatabase database){ @@ -200,6 +206,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); } public void onApplicationLevelUpgrade(@NonNull Context context, @NonNull MasterSecret masterSecret, diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index c182a39061..6172cfeac4 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -72,9 +72,6 @@ public class SmsDatabase extends MessagingDatabase { public static final String SUBJECT = "subject"; public static final String SERVICE_CENTER = "service_center"; - // Loki - public static final String IS_FRIEND_REQUEST = "is_friend_request"; - public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " integer PRIMARY KEY, " + THREAD_ID + " INTEGER, " + ADDRESS + " TEXT, " + ADDRESS_DEVICE_ID + " INTEGER DEFAULT 1, " + PERSON + " INTEGER, " + DATE_RECEIVED + " INTEGER, " + DATE_SENT + " INTEGER, " + PROTOCOL + " INTEGER, " + READ + " INTEGER DEFAULT 0, " + @@ -82,7 +79,7 @@ public class SmsDatabase extends MessagingDatabase { DELIVERY_RECEIPT_COUNT + " INTEGER DEFAULT 0," + SUBJECT + " TEXT, " + BODY + " TEXT, " + MISMATCHED_IDENTITIES + " TEXT DEFAULT NULL, " + SERVICE_CENTER + " TEXT, " + SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " + EXPIRES_IN + " INTEGER DEFAULT 0, " + EXPIRE_STARTED + " INTEGER DEFAULT 0, " + NOTIFIED + " DEFAULT 0, " + - READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNIDENTIFIED + " INTEGER DEFAULT 0, " + IS_FRIEND_REQUEST + " INTEGER DEFAULT 0);"; + READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNIDENTIFIED + " INTEGER DEFAULT 0);"; public static final String[] CREATE_INDEXS = { "CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");", @@ -643,7 +640,6 @@ public class SmsDatabase extends MessagingDatabase { contentValues.put(EXPIRES_IN, message.getExpiresIn()); contentValues.put(DELIVERY_RECEIPT_COUNT, Stream.of(earlyDeliveryReceipts.values()).mapToLong(Long::longValue).sum()); contentValues.put(READ_RECEIPT_COUNT, Stream.of(earlyReadReceipts.values()).mapToLong(Long::longValue).sum()); - contentValues.put(IS_FRIEND_REQUEST, message.isFriendRequest ? 1 : 0); SQLiteDatabase db = databaseHelper.getWritableDatabase(); long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues); @@ -665,6 +661,11 @@ public class SmsDatabase extends MessagingDatabase { ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(threadId)); } + // Loki: Save friend request state on sms + if (message.isFriendRequest) { + DatabaseFactory.getLokiSmsFriendRequestDatabase(context).setIsFriendRequest(messageId, message.isFriendRequest); + } + return messageId; } @@ -829,7 +830,7 @@ public class SmsDatabase extends MessagingDatabase { 0, message.isSecureMessage() ? MmsSmsColumns.Types.getOutgoingEncryptedMessageType() : MmsSmsColumns.Types.getOutgoingSmsMessageType(), threadId, 0, new LinkedList(), message.getSubscriptionId(), message.getExpiresIn(), - System.currentTimeMillis(), 0, false); + System.currentTimeMillis(), 0, false, message.isFriendRequest); } } @@ -870,7 +871,6 @@ public class SmsDatabase extends MessagingDatabase { long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.EXPIRE_STARTED)); String body = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY)); boolean unidentified = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.UNIDENTIFIED)) == 1; - boolean isFriendRequest = true;//= cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.IS_FRIEND_REQUEST)) == 1; if (!TextSecurePreferences.isReadReceiptsEnabled(context)) { readReceiptCount = 0; @@ -879,6 +879,9 @@ public class SmsDatabase extends MessagingDatabase { List mismatches = getMismatches(mismatchDocument); Recipient recipient = Recipient.from(context, address, true); + // Loki: Check to see if this message was a friend request + boolean isFriendRequest = DatabaseFactory.getLokiSmsFriendRequestDatabase(context).getIsFriendRequest(messageId); + return new SmsMessageRecord(messageId, body, recipient, recipient, addressDeviceId, diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index fb66026457..84751aaa78 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -38,6 +38,7 @@ import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob; 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.notifications.NotificationChannels; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.util.TextSecurePreferences; @@ -120,6 +121,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand()); db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand()); db.execSQL(LokiContactPreKeyDatabase.getCreateTableCommand()); + db.execSQL(LokiSmsFriendRequestDatabase.getCreateTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); diff --git a/src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt new file mode 100644 index 0000000000..13907a0d64 --- /dev/null +++ b/src/org/thoughtcrime/securesms/loki/LokiSmsFriendRequestDatabase.kt @@ -0,0 +1,46 @@ +package org.thoughtcrime.securesms.loki + +import android.content.ContentValues +import android.content.Context +import net.sqlcipher.database.SQLiteDatabase +import org.thoughtcrime.securesms.crypto.PreKeyUtil +import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper +import org.whispersystems.libsignal.state.PreKeyRecord + +/** + * A database for associating friend request data to Sms objects + */ +class LokiSmsFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { + + companion object { + private val tableName = "loki_sms_friend_request_database" + private val smsId = "_id" + private val isFriendRequest = "is_friend_request" + + @JvmStatic + val createTableCommand = "CREATE TABLE $tableName ($smsId INTEGER PRIMARY KEY, $isFriendRequest INTEGER DEFAULT 0);" + } + + fun setIsFriendRequest(messageId: Long, isFriendRequest: Boolean) { + val database = databaseHelper.writableDatabase + + val rawIsFriendRequest = if (isFriendRequest) 1 else 0 + + val values = ContentValues() + values.put(smsId, messageId) + values.put(Companion.isFriendRequest, rawIsFriendRequest) + + // Note: If we add any other fields, then `SQLiteDatabase.CONFLICT_REPLACE` will most likely overwrite them + // we probably want to switch to `database.update` later, for now since we only have 1 field, it is fine + database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE) + } + + fun getIsFriendRequest(messageId: Long): Boolean { + val database = databaseHelper.readableDatabase + return database.get(tableName, ID_WHERE, arrayOf(messageId.toString())) { cursor -> + val rawIsFriendRequest = cursor.getInt(isFriendRequest) + rawIsFriendRequest == 1 + } ?: false + } +} \ No newline at end of file