Pre-populate user ID cache as needed

This commit is contained in:
Niels Andriesse 2019-10-10 10:39:56 +11:00
parent 5901967eee
commit 25bd1073b0
4 changed files with 41 additions and 1 deletions

View File

@ -156,6 +156,7 @@ import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.loki.FriendRequestViewDelegate;
import org.thoughtcrime.securesms.loki.LokiAPIUtilities;
import org.thoughtcrime.securesms.loki.LokiThreadDatabaseDelegate;
import org.thoughtcrime.securesms.mediasend.Media;
import org.thoughtcrime.securesms.mediasend.MediaSendActivity;
@ -394,6 +395,8 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
}
});
LokiAPIUtilities.INSTANCE.populateUserIDCacheIfNeeded(threadId, this);
if (this.recipient.isGroupRecipient()) {
if (this.recipient.getName().equals("Loki Public Chat")) {
Analytics.Companion.getShared().track("Loki Public Chat Opened");

View File

@ -67,6 +67,7 @@ import org.thoughtcrime.securesms.linkpreview.LinkPreview;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository;
import org.thoughtcrime.securesms.linkpreview.LinkPreviewUtil;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.loki.LokiAPIUtilities;
import org.thoughtcrime.securesms.loki.LokiMessageDatabase;
import org.thoughtcrime.securesms.loki.LokiPreKeyBundleDatabase;
import org.thoughtcrime.securesms.loki.LokiPreKeyRecordDatabase;
@ -123,6 +124,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage
import org.whispersystems.signalservice.api.messages.shared.SharedContact;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.loki.api.DeviceLinkingSession;
import org.whispersystems.signalservice.loki.api.LokiAPI;
import org.whispersystems.signalservice.loki.api.LokiStorageAPI;
import org.whispersystems.signalservice.loki.api.PairingAuthorisation;
import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher;
@ -1021,6 +1023,12 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
if (smsMessageId.isPresent()) database.deleteMessage(smsMessageId.get());
// Loki - Cache the user hex encoded public key (for mentions)
if (threadId != null) {
LokiAPIUtilities.INSTANCE.populateUserIDCacheIfNeeded(threadId, context);
LokiAPI.Companion.cache(textMessage.getSender().serialize(), threadId);
}
// Loki - Store message server ID
updateGroupChatMessageServerID(messageServerIDOrNull, insertResult);

View File

@ -0,0 +1,29 @@
package org.thoughtcrime.securesms.loki
import android.content.Context
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.model.MessageRecord
import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.signalservice.loki.api.LokiAPI
object LokiAPIUtilities {
fun populateUserIDCacheIfNeeded(threadID: Long, context: Context) {
if (LokiAPI.userIDCache[threadID] != null) { return }
val result = mutableSetOf<String>()
val messageDatabase = DatabaseFactory.getMmsSmsDatabase(context)
val reader = messageDatabase.readerFor(messageDatabase.getConversation(threadID))
var record: MessageRecord? = reader.next
while (record != null) {
result.add(record.individualRecipient.address.serialize())
try {
record = reader.next
} catch (exception: Exception) {
record = null
}
}
reader.close()
result.add(TextSecurePreferences.getLocalNumber(context))
LokiAPI.userIDCache[threadID] = result
}
}

View File

@ -46,7 +46,7 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database
Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners()
}
fun getServerDisplayName(serverID: String, hexEncodedPublicKey: String): String? {
override fun getServerDisplayName(serverID: String, hexEncodedPublicKey: String): String? {
val database = databaseHelper.readableDatabase
return database.get(serverDisplayNameTable, "${Companion.hexEncodedPublicKey} = ? AND ${Companion.serverID} = ?", arrayOf( hexEncodedPublicKey, serverID )) { cursor ->
cursor.getString(cursor.getColumnIndexOrThrow(displayName))