From b58fce3a680039021d1a286efa0b4031ffff9e84 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 24 Jun 2019 14:10:09 +1000 Subject: [PATCH] Update for core changes & refactor --- .../conversation/ConversationActivity.java | 16 ++++----- .../securesms/database/DatabaseFactory.java | 8 ++--- .../securesms/database/SmsDatabase.java | 2 +- .../database/helpers/SQLCipherOpenHelper.java | 4 +-- .../SignalCommunicationModule.java | 25 ++++++------- .../securesms/jobs/PushDecryptJob.java | 4 +-- .../securesms/loki/AccountDetailsActivity.kt | 1 - .../securesms/loki/DatabaseUtilities.kt | 6 ++-- .../securesms/loki/FriendRequestView.kt | 5 +-- .../loki/LokiMessageFriendRequestDatabase.kt | 15 +++++--- .../loki/LokiMessageFriendRequestStatus.kt | 13 ------- .../loki/LokiPreKeyBundleDatabase.kt | 35 ++++--------------- .../securesms/loki/LokiPreKeyBundleStore.kt | 25 ------------- ...atabase.kt => LokiPreKeyRecordDatabase.kt} | 12 +++---- .../loki/LokiThreadFriendRequestDatabase.kt | 13 +++++-- .../loki/LokiThreadFriendRequestStatus.kt | 28 --------------- 16 files changed, 67 insertions(+), 145 deletions(-) delete mode 100644 src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestStatus.kt delete mode 100644 src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStore.kt rename src/org/thoughtcrime/securesms/loki/{LokiContactPreKeyDatabase.kt => LokiPreKeyRecordDatabase.kt} (83%) delete mode 100644 src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestStatus.kt diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 585a5ffb6a..711caea1d7 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -143,16 +143,15 @@ import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.MmsMessageRecord; import org.thoughtcrime.securesms.events.ReminderUpdateEvent; import org.thoughtcrime.securesms.giph.ui.GiphyActivity; -import org.thoughtcrime.securesms.linkpreview.LinkPreview; -import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository; -import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel; -import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestStatus; -import org.thoughtcrime.securesms.mediasend.MediaSendActivity; -import org.thoughtcrime.securesms.mediasend.Media; import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob; import org.thoughtcrime.securesms.jobs.RetrieveProfileJob; import org.thoughtcrime.securesms.jobs.ServiceOutageDetectionJob; +import org.thoughtcrime.securesms.linkpreview.LinkPreview; +import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository; +import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel; import org.thoughtcrime.securesms.logging.Log; +import org.thoughtcrime.securesms.mediasend.Media; +import org.thoughtcrime.securesms.mediasend.MediaSendActivity; import org.thoughtcrime.securesms.mms.AttachmentManager; import org.thoughtcrime.securesms.mms.AttachmentManager.MediaType; import org.thoughtcrime.securesms.mms.AudioSlide; @@ -179,9 +178,9 @@ import org.thoughtcrime.securesms.permissions.Permissions; import org.thoughtcrime.securesms.profiles.GroupShareProfileView; import org.thoughtcrime.securesms.providers.BlobProvider; import org.thoughtcrime.securesms.recipients.Recipient; +import org.thoughtcrime.securesms.recipients.RecipientExporter; import org.thoughtcrime.securesms.recipients.RecipientFormattingException; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; -import org.thoughtcrime.securesms.recipients.RecipientExporter; import org.thoughtcrime.securesms.search.model.MessageResult; import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.sms.MessageSender; @@ -209,6 +208,7 @@ 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.loki.messaging.LokiThreadFriendRequestStatus; import java.io.IOException; import java.security.SecureRandom; @@ -2155,7 +2155,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } // Loki - Always send friend requests if we're not friends with the user - LokiThreadFriendRequestStatus friendRequestStatus = DatabaseFactory.getLokiThreadFriendRequestDatabase(context).friendRequestStatus(threadId); + LokiThreadFriendRequestStatus friendRequestStatus = DatabaseFactory.getLokiThreadFriendRequestDatabase(context).getFriendRequestStatus(threadId); message.isFriendRequest = (friendRequestStatus != LokiThreadFriendRequestStatus.FRIENDS); Permissions.with(this) diff --git a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java index febdec90e6..015d5f2762 100644 --- a/src/org/thoughtcrime/securesms/database/DatabaseFactory.java +++ b/src/org/thoughtcrime/securesms/database/DatabaseFactory.java @@ -32,7 +32,7 @@ import org.thoughtcrime.securesms.database.helpers.ClassicOpenHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherMigrationHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; import org.thoughtcrime.securesms.loki.LokiAPIDatabase; -import org.thoughtcrime.securesms.loki.LokiContactPreKeyDatabase; +import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase; import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase; import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase; @@ -66,7 +66,7 @@ public class DatabaseFactory { // Loki private final LokiAPIDatabase lokiAPIDatabase; - private final LokiContactPreKeyDatabase lokiContactPreKeyDatabase; + private final LokiPreKeyRecordDatabase lokiContactPreKeyDatabase; private final LokiPreKeyBundleDatabase lokiPreKeyBundleDatabase; private final LokiMessageFriendRequestDatabase lokiMessageFriendRequestDatabase; private final LokiThreadFriendRequestDatabase lokiThreadFriendRequestDatabase; @@ -161,7 +161,7 @@ public class DatabaseFactory { return getInstance(context).lokiAPIDatabase; } - public static LokiContactPreKeyDatabase getLokiContactPreKeyDatabase(Context context) { + public static LokiPreKeyRecordDatabase getLokiPreKeyRecordDatabase(Context context) { return getInstance(context).lokiContactPreKeyDatabase; } @@ -210,7 +210,7 @@ public class DatabaseFactory { this.jobDatabase = new JobDatabase(context, databaseHelper); this.lokiAPIDatabase = new LokiAPIDatabase(context, databaseHelper); - this.lokiContactPreKeyDatabase = new LokiContactPreKeyDatabase(context, databaseHelper); + this.lokiContactPreKeyDatabase = new LokiPreKeyRecordDatabase(context, databaseHelper); this.lokiPreKeyBundleDatabase = new LokiPreKeyBundleDatabase(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 552a4e0f67..c583963f62 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -37,7 +37,6 @@ import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.SmsMessageRecord; import org.thoughtcrime.securesms.jobs.TrimThreadJob; import org.thoughtcrime.securesms.logging.Log; -import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestStatus; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.sms.IncomingGroupMessage; import org.thoughtcrime.securesms.sms.IncomingTextMessage; @@ -45,6 +44,7 @@ import org.thoughtcrime.securesms.sms.OutgoingTextMessage; import org.thoughtcrime.securesms.util.JsonUtils; import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.whispersystems.libsignal.util.guava.Optional; +import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus; import java.io.IOException; import java.security.SecureRandom; diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index e8c3e2662e..4800b67a81 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -35,7 +35,7 @@ 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.LokiPreKeyRecordDatabase; import org.thoughtcrime.securesms.loki.LokiMessageFriendRequestDatabase; import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase; @@ -120,7 +120,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTableCommand()); db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand()); db.execSQL(LokiPreKeyBundleDatabase.getCreateTableCommand()); - db.execSQL(LokiContactPreKeyDatabase.getCreateTableCommand()); + db.execSQL(LokiPreKeyRecordDatabase.getCreateTableCommand()); db.execSQL(LokiMessageFriendRequestDatabase.getCreateTableCommand()); db.execSQL(LokiThreadFriendRequestDatabase.getCreateTableCommand()); diff --git a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java index 56854b0e02..14c4affc4b 100644 --- a/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java +++ b/src/org/thoughtcrime/securesms/dependencies/SignalCommunicationModule.java @@ -2,33 +2,28 @@ package org.thoughtcrime.securesms.dependencies; import android.content.Context; -import org.thoughtcrime.securesms.database.DatabaseFactory; -import org.thoughtcrime.securesms.gcm.FcmService; -import org.thoughtcrime.securesms.jobs.AttachmentUploadJob; -import org.thoughtcrime.securesms.jobs.MultiDeviceConfigurationUpdateJob; -import org.thoughtcrime.securesms.jobs.PushDecryptJob; -import org.thoughtcrime.securesms.jobs.RefreshUnidentifiedDeliveryAbilityJob; -import org.thoughtcrime.securesms.jobs.RotateProfileKeyJob; -import org.thoughtcrime.securesms.jobs.TypingSendJob; -import org.thoughtcrime.securesms.logging.Log; - import org.greenrobot.eventbus.EventBus; import org.thoughtcrime.securesms.BuildConfig; import org.thoughtcrime.securesms.CreateProfileActivity; import org.thoughtcrime.securesms.DeviceListFragment; import org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl; +import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.events.ReminderUpdateEvent; +import org.thoughtcrime.securesms.gcm.FcmService; import org.thoughtcrime.securesms.jobs.AttachmentDownloadJob; +import org.thoughtcrime.securesms.jobs.AttachmentUploadJob; import org.thoughtcrime.securesms.jobs.AvatarDownloadJob; import org.thoughtcrime.securesms.jobs.CleanPreKeysJob; import org.thoughtcrime.securesms.jobs.CreateSignedPreKeyJob; import org.thoughtcrime.securesms.jobs.FcmRefreshJob; import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob; +import org.thoughtcrime.securesms.jobs.MultiDeviceConfigurationUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceContactUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceGroupUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceProfileKeyUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob; import org.thoughtcrime.securesms.jobs.MultiDeviceVerifiedUpdateJob; +import org.thoughtcrime.securesms.jobs.PushDecryptJob; import org.thoughtcrime.securesms.jobs.PushGroupSendJob; import org.thoughtcrime.securesms.jobs.PushGroupUpdateJob; import org.thoughtcrime.securesms.jobs.PushMediaSendJob; @@ -36,14 +31,18 @@ import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob; import org.thoughtcrime.securesms.jobs.PushTextSendJob; import org.thoughtcrime.securesms.jobs.RefreshAttributesJob; import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob; +import org.thoughtcrime.securesms.jobs.RefreshUnidentifiedDeliveryAbilityJob; import org.thoughtcrime.securesms.jobs.RequestGroupInfoJob; import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob; import org.thoughtcrime.securesms.jobs.RetrieveProfileJob; import org.thoughtcrime.securesms.jobs.RotateCertificateJob; +import org.thoughtcrime.securesms.jobs.RotateProfileKeyJob; import org.thoughtcrime.securesms.jobs.RotateSignedPreKeyJob; import org.thoughtcrime.securesms.jobs.SendDeliveryReceiptJob; import org.thoughtcrime.securesms.jobs.SendReadReceiptJob; -import org.thoughtcrime.securesms.loki.LokiPreKeyBundleStore; +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; @@ -140,7 +139,9 @@ public class SignalCommunicationModule { Optional.of(new SecurityEventListener(context)), TextSecurePreferences.getLocalNumber(context), DatabaseFactory.getLokiAPIDatabase(context), - new LokiPreKeyBundleStore(context)); + DatabaseFactory.getLokiThreadFriendRequestDatabase(context), + DatabaseFactory.getLokiMessageFriendRequestDatabase(context), + DatabaseFactory.getLokiPreKeyBundleDatabase(context)); } else { this.messageSender.setMessagePipe(IncomingMessageObserver.getPipe(), IncomingMessageObserver.getUnidentifiedPipe()); this.messageSender.setIsMultiDevice(TextSecurePreferences.isMultiDevice(context)); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 8742573255..5a9ea61c46 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -62,7 +62,6 @@ import org.thoughtcrime.securesms.linkpreview.Link; import org.thoughtcrime.securesms.linkpreview.LinkPreview; import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil; import org.thoughtcrime.securesms.logging.Log; -import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestStatus; import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase; import org.thoughtcrime.securesms.loki.LokiThreadFriendRequestDatabase; import org.thoughtcrime.securesms.mms.IncomingMediaMessage; @@ -113,6 +112,7 @@ import org.whispersystems.signalservice.api.messages.shared.SharedContact; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher; import org.whispersystems.signalservice.loki.messaging.LokiServiceMessage; +import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus; import java.security.MessageDigest; import java.util.ArrayList; @@ -827,7 +827,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType { Recipient recipient = getMessageDestination(content, message); long threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient); LokiThreadFriendRequestDatabase database = DatabaseFactory.getLokiThreadFriendRequestDatabase(context); - LokiThreadFriendRequestStatus friendRequestStatus = database.friendRequestStatus(threadID); + LokiThreadFriendRequestStatus friendRequestStatus = database.getFriendRequestStatus(threadID); if (envelope.isFriendRequest()) { if (friendRequestStatus == LokiThreadFriendRequestStatus.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/AccountDetailsActivity.kt b/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt index 688e2e0248..758b5e0847 100644 --- a/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/AccountDetailsActivity.kt @@ -9,7 +9,6 @@ import org.thoughtcrime.securesms.R import org.thoughtcrime.securesms.util.TextSecurePreferences import org.whispersystems.signalservice.api.crypto.ProfileCipher - class AccountDetailsActivity : BaseActionBarActivity() { override fun onCreate(savedInstanceState: Bundle?) { diff --git a/src/org/thoughtcrime/securesms/loki/DatabaseUtilities.kt b/src/org/thoughtcrime/securesms/loki/DatabaseUtilities.kt index 3dfe9dae2c..e488ca5144 100644 --- a/src/org/thoughtcrime/securesms/loki/DatabaseUtilities.kt +++ b/src/org/thoughtcrime/securesms/loki/DatabaseUtilities.kt @@ -3,7 +3,7 @@ package org.thoughtcrime.securesms.loki import android.content.ContentValues import net.sqlcipher.Cursor import net.sqlcipher.database.SQLiteDatabase -import org.thoughtcrime.securesms.util.Base64 +import org.whispersystems.signalservice.internal.util.Base64 fun SQLiteDatabase.get(table: String, query: String, arguments: Array, get: (Cursor) -> T): T? { var cursor: Cursor? = null @@ -18,10 +18,10 @@ fun SQLiteDatabase.get(table: String, query: String, arguments: Array) { +fun SQLiteDatabase.insertOrUpdate(table: String, values: ContentValues, query: String, arguments: Array) { val id = insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE).toInt() if (id == -1) { - update(table, values, whereClause, whereArgs) + update(table, values, query, arguments) } } diff --git a/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt b/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt index dd097381bd..bb90b4ab98 100644 --- a/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt +++ b/src/org/thoughtcrime/securesms/loki/FriendRequestView.kt @@ -11,6 +11,7 @@ import org.thoughtcrime.securesms.R import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.sms.IncomingTextMessage import org.thoughtcrime.securesms.sms.OutgoingTextMessage +import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) { private var isUISetUp = false @@ -89,7 +90,7 @@ class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: In val database = DatabaseFactory.getLokiMessageFriendRequestDatabase(context) if (message is IncomingTextMessage) { val message = this.message as IncomingTextMessage - val friendRequestStatus = database.friendRequestStatus(0) // TODO: Message ID + val friendRequestStatus = database.getFriendRequestStatus(0) // TODO: Message ID buttonLinearLayout.visibility = if (friendRequestStatus != LokiMessageFriendRequestStatus.REQUEST_PENDING) View.GONE else View.VISIBLE val formatID = when (friendRequestStatus) { LokiMessageFriendRequestStatus.NONE, LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED -> throw IllegalStateException() @@ -102,7 +103,7 @@ class FriendRequestView(context: Context, attrs: AttributeSet?, defStyleAttr: In label.text = resources.getString(formatID, contactID) } else { val message = this.message as OutgoingTextMessage - val friendRequestStatus = database.friendRequestStatus(0) // TODO: Message ID + val friendRequestStatus = database.getFriendRequestStatus(0) // TODO: Message ID buttonLinearLayout.visibility = View.GONE val formatID = when (friendRequestStatus) { LokiMessageFriendRequestStatus.NONE -> throw IllegalStateException() diff --git a/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt index d0f99c73ee..220779284d 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestDatabase.kt @@ -3,9 +3,12 @@ package org.thoughtcrime.securesms.loki import android.content.ContentValues import android.content.Context import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper +import org.whispersystems.signalservice.loki.messaging.LokiMessageDatabaseProtocol +import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus -class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiMessageDatabaseProtocol { companion object { private val tableName = "loki_message_friend_request_database" @@ -14,7 +17,7 @@ class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHe @JvmStatic val createTableCommand = "CREATE TABLE $tableName ($messageID INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);" } - fun friendRequestStatus(messageID: Long): LokiMessageFriendRequestStatus { + override fun getFriendRequestStatus(messageID: Long): LokiMessageFriendRequestStatus { val database = databaseHelper.readableDatabase val result = database.get(tableName, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) { cursor -> cursor.getInt(friendRequestStatus) @@ -26,19 +29,21 @@ class LokiMessageFriendRequestDatabase(context: Context, helper: SQLCipherOpenHe } } - fun setFriendRequestStatus(messageID: Long, friendRequestStatus: LokiMessageFriendRequestStatus) { + override fun setFriendRequestStatus(messageID: Long, friendRequestStatus: LokiMessageFriendRequestStatus) { val database = databaseHelper.writableDatabase val contentValues = ContentValues(2) contentValues.put(Companion.messageID, messageID) contentValues.put(Companion.friendRequestStatus, friendRequestStatus.rawValue) database.insertOrUpdate(tableName, contentValues, "${Companion.messageID} = ?", arrayOf( messageID.toString() )) + val threadID = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageID) + notifyConversationListeners(threadID) } fun isFriendRequest(messageID: Long): Boolean { - return friendRequestStatus(messageID) != LokiMessageFriendRequestStatus.NONE + return getFriendRequestStatus(messageID) != LokiMessageFriendRequestStatus.NONE } fun hasFriendRequestStatusMessage(messageID: Long): Boolean { - return isFriendRequest(messageID) && friendRequestStatus(messageID) != LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED + return isFriendRequest(messageID) && getFriendRequestStatus(messageID) != LokiMessageFriendRequestStatus.REQUEST_SENDING_OR_FAILED } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestStatus.kt b/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestStatus.kt deleted file mode 100644 index ed2bcdee03..0000000000 --- a/src/org/thoughtcrime/securesms/loki/LokiMessageFriendRequestStatus.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.thoughtcrime.securesms.loki - -enum class LokiMessageFriendRequestStatus(val rawValue: Int) { - NONE(0), - REQUEST_SENDING_OR_FAILED(1), - /** - * Either sent or received. - */ - REQUEST_PENDING(2), - REQUEST_ACCEPTED(3), - REQUEST_REJECTED(4), - REQUEST_EXPIRED(5) -} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt index 80153e966f..ae2bef429f 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleDatabase.kt @@ -14,8 +14,9 @@ import org.whispersystems.libsignal.IdentityKey import org.whispersystems.libsignal.ecc.Curve import org.whispersystems.libsignal.state.PreKeyBundle import org.whispersystems.signalservice.api.push.SignalServiceAddress +import org.whispersystems.signalservice.loki.messaging.LokiPreKeyDatabaseProtocol -class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiPreKeyDatabaseProtocol { companion object { private val tableName = "loki_pre_key_bundle_database" @@ -33,30 +34,17 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : "$signedPreKeySignature TEXT," + "$identityKey TEXT NOT NULL," + "$deviceID INTEGER," + "$registrationID INTEGER" + ");" } - /** - * Generate a `PreKeyBundle` for the given contact. - * This generated bundle shouldn't be stored locally since this is used to generate bundles to send to other users. - * - * @param hexEncodedPublicKey String The hex encoded public key of the contact. - * @return PreKeyBundle? A pre key bundle or `null` if something went wrong. - */ fun generatePreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { val identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context) val signedPreKey = PreKeyUtil.getActiveSignedPreKey(context) ?: return null - val preKeyRecord = DatabaseFactory.getLokiContactPreKeyDatabase(context).getOrCreatePreKey(hexEncodedPublicKey) + val preKeyRecord = DatabaseFactory.getLokiPreKeyRecordDatabase(context).getOrCreatePreKey(hexEncodedPublicKey) val registrationID = TextSecurePreferences.getLocalRegistrationId(context) if (registrationID == 0) return null val deviceID = SignalServiceAddress.DEFAULT_DEVICE_ID return PreKeyBundle(registrationID, deviceID,preKeyRecord.id, preKeyRecord.keyPair.publicKey, signedPreKey.id, signedPreKey.keyPair.publicKey, signedPreKey.signature, identityKeyPair.publicKey) } - /** - * Get the `PreKeyBundle` associated with the given contact. - * - * @param hexEncodedPublicKey String The hex encoded public key of the contact. - * @return PreKeyBundle? The pre key bundle or `null` if it doesn't exist. - */ - fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { + override fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { val database = databaseHelper.readableDatabase return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor -> val registrationID = cursor.getInt(registrationID) @@ -71,15 +59,9 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : } } - /** - * Set the `PreKeyBundle` for the given contact. - * - * @param hexEncodedPublicKey String The hex encoded public key of the contact. - * @param preKeyBundle PreKeyBundle The pre key bundle. - */ fun setPreKeyBundle(hexEncodedPublicKey: String, preKeyBundle: PreKeyBundle) { val database = databaseHelper.writableDatabase - val contentValues = ContentValues() + val contentValues = ContentValues(9) contentValues.put(registrationID, preKeyBundle.registrationId) contentValues.put(deviceID, preKeyBundle.deviceId) contentValues.put(preKeyID, preKeyBundle.preKeyId) @@ -92,12 +74,7 @@ class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : database.insertWithOnConflict(tableName, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE) } - /** - * Remove the `PreKeyBundle` for the given contact. - * - * @param hexEncodedPublicKey String The hex encoded public key of the contact. - */ - fun removePreKeyBundle(hexEncodedPublicKey: String) { + override fun removePreKeyBundle(hexEncodedPublicKey: String) { val database = databaseHelper.writableDatabase database.delete(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) } diff --git a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStore.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStore.kt deleted file mode 100644 index 074d1c1e0b..0000000000 --- a/src/org/thoughtcrime/securesms/loki/LokiPreKeyBundleStore.kt +++ /dev/null @@ -1,25 +0,0 @@ -package org.thoughtcrime.securesms.loki - -import android.content.Context -import org.thoughtcrime.securesms.database.DatabaseFactory -import org.whispersystems.libsignal.state.PreKeyBundle -import org.whispersystems.signalservice.loki.messaging.LokiPreKeyBundleStoreProtocol - -class LokiPreKeyBundleStore(val context: Context) : LokiPreKeyBundleStoreProtocol { - - companion object { - private val lock = Object() - } - - override fun getPreKeyBundle(hexEncodedPublicKey: String): PreKeyBundle? { - synchronized(lock) { - return DatabaseFactory.getLokiPreKeyBundleDatabase(context).getPreKeyBundle(hexEncodedPublicKey) - } - } - - override fun removePreKeyBundle(hexEncodedPublicKey: String) { - synchronized(lock) { - DatabaseFactory.getLokiPreKeyBundleDatabase(context).removePreKeyBundle(hexEncodedPublicKey) - } - } -} \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiPreKeyRecordDatabase.kt similarity index 83% rename from src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt rename to src/org/thoughtcrime/securesms/loki/LokiPreKeyRecordDatabase.kt index 516d79eb2f..bbad274387 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiContactPreKeyDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiPreKeyRecordDatabase.kt @@ -8,10 +8,10 @@ import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper import org.whispersystems.libsignal.state.PreKeyRecord -class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +class LokiPreKeyRecordDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { companion object { - private val tableName = "loki_contact_pre_key_database" + private val tableName = "loki_pre_key_record_database" private val preKeyID = "pre_key_id" private val hexEncodedPublicKey = "public_key" @JvmStatic val createTableCommand = "CREATE TABLE $tableName ($preKeyID INTEGER PRIMARY KEY, $hexEncodedPublicKey TEXT);" @@ -19,14 +19,12 @@ class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : fun hasPreKey(hexEncodedPublicKey: String): Boolean { val database = databaseHelper.readableDatabase - return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey)) { cursor -> - cursor.count > 0 - } ?: false + return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { it.count > 0 } ?: false } fun getPreKey(hexEncodedPublicKey: String): PreKeyRecord? { val database = databaseHelper.readableDatabase - return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf(hexEncodedPublicKey)) { cursor -> + return database.get(tableName, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) { cursor -> val preKeyID = cursor.getInt(preKeyID) PreKeyUtil.loadPreKey(context, preKeyID) } @@ -41,7 +39,7 @@ class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : PreKeyUtil.storePreKeyRecords(context, preKeyRecords) val record = preKeyRecords.first() val database = databaseHelper.writableDatabase - val values = ContentValues() + val values = ContentValues(2) values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey) values.put(preKeyID, record.id) database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE) diff --git a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt index 6bad08e2c9..27d0a3eaec 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestDatabase.kt @@ -3,9 +3,12 @@ package org.thoughtcrime.securesms.loki import android.content.ContentValues import android.content.Context import org.thoughtcrime.securesms.database.Database +import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper +import org.whispersystems.signalservice.loki.messaging.LokiThreadDatabaseProtocol +import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus -class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) { +class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiThreadDatabaseProtocol { companion object { private val tableName = "loki_thread_friend_request_database" @@ -14,7 +17,11 @@ class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHel @JvmStatic val createTableCommand = "CREATE TABLE $tableName ($threadID INTEGER PRIMARY KEY, $friendRequestStatus INTEGER DEFAULT 0);" } - fun friendRequestStatus(threadID: Long): LokiThreadFriendRequestStatus { + override fun getThreadID(messageID: Long): Long { + return DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageID) + } + + override fun getFriendRequestStatus(threadID: Long): LokiThreadFriendRequestStatus { val database = databaseHelper.readableDatabase val result = database.get(tableName, "${Companion.threadID} = ?", arrayOf( threadID.toString() )) { cursor -> cursor.getInt(friendRequestStatus) @@ -26,7 +33,7 @@ class LokiThreadFriendRequestDatabase(context: Context, helper: SQLCipherOpenHel } } - fun setFriendRequestStatus(threadID: Long, friendRequestStatus: LokiThreadFriendRequestStatus) { + override fun setFriendRequestStatus(threadID: Long, friendRequestStatus: LokiThreadFriendRequestStatus) { val database = databaseHelper.writableDatabase val contentValues = ContentValues(2) contentValues.put(Companion.threadID, threadID) diff --git a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestStatus.kt b/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestStatus.kt deleted file mode 100644 index bd1cfd3c54..0000000000 --- a/src/org/thoughtcrime/securesms/loki/LokiThreadFriendRequestStatus.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.thoughtcrime.securesms.loki - -enum class LokiThreadFriendRequestStatus(val rawValue: Int) { - /** - * New conversation; no messages sent or received. - */ - NONE(0), - /** - * Used to lock the input early while sending. - */ - REQUEST_SENDING(1), - /** - * Friend request sent; awaiting response. - */ - REQUEST_SENT(2), - /** - * Friend request received; awaiting user input. - */ - REQUEST_RECEIVED(3), - /** - * The user is friends with the other user in this thread. - */ - FRIENDS(4), - /** - * A friend request was sent, but it timed out (i.e the other user didn't accept within the allocated time). - */ - REQUEST_EXPIRED(5) -} \ No newline at end of file