Removed recipient cache as signal already had that.
Removed message cache as it messes with the ui (public chat conversations pop in with all the messages)
This commit is contained in:
Mikunj 2019-12-05 10:51:54 +11:00
parent 12e07bc5bb
commit 3d574be931
5 changed files with 11 additions and 43 deletions

View File

@ -62,8 +62,6 @@ public class ThreadDatabase extends Database {
private static final String TAG = ThreadDatabase.class.getSimpleName();
private Map<Long, Address> addressCache = new HashMap<>();
private Map<Long, List<ContentValues>> messageCache = new HashMap<>();
private Map<Long, String> recipientCache = new HashMap<>();
public static final String TABLE_NAME = "thread";
public static final String ID = "_id";
@ -157,31 +155,9 @@ public class ThreadDatabase extends Database {
contentValues.put(ARCHIVED, 0);
}
if (!messageCache.containsKey(threadId)) {
messageCache.put(threadId, new ArrayList<>());
}
messageCache.get(threadId).add(contentValues);
if (!recipientCache.containsKey(threadId)) {
recipientCache.put(threadId, getRecipientForThreadId(threadId).getName().replaceAll("\\s*", ""));
}
String key = recipientCache.get(threadId);
int newMessagesNumber = TextSecurePreferences.getNewMessagesNumber(this.context, key);
if (newMessagesNumber == 0 || newMessagesNumber == messageCache.get(threadId).size()) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.beginTransactionNonExclusive();
try {
for (ContentValues contentValue : messageCache.get(threadId)) {
db.update(TABLE_NAME, contentValue, ID + " = ?", new String[] {threadId + ""});
}
TextSecurePreferences.setNewMessagesNumber(this.context, key, TextSecurePreferences.getNewMessagesNumber(this.context, key) - newMessagesNumber);
messageCache.get(threadId).clear();
db.setTransactionSuccessful();
notifyConversationListListeners();
} finally {
db.endTransaction();
}
}
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""});
notifyConversationListListeners();
}
@ -207,6 +183,7 @@ public class ThreadDatabase extends Database {
private void deleteThread(long threadId) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""});
addressCache.remove(threadId);
notifyConversationListListeners();
}
@ -221,12 +198,16 @@ public class ThreadDatabase extends Database {
where = where.substring(0, where.length() - 4);
db.delete(TABLE_NAME, where, null);
for (long threadId: threadIds) {
addressCache.remove(threadId);
}
notifyConversationListListeners();
}
private void deleteAllThreads() {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
addressCache.clear();
notifyConversationListListeners();
}
@ -559,8 +540,6 @@ public class ThreadDatabase extends Database {
public @Nullable Recipient getRecipientForThreadId(long threadId) {
// Loki - Cache the address.
// Don't know if this will affect any other signal code
// Don't know if it is necessary to add some cache time
if (addressCache.containsKey(threadId) && addressCache.get(threadId) != null) {
return Recipient.from(context, addressCache.get(threadId), false);
}

View File

@ -1712,7 +1712,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
}
return Recipient.from(context, Address.fromSerialized(publicKey), false);
} catch (Exception e) {
Log.d("Loki", "Failed to get primary device public key for message. " + e.getMessage());
Log.d("Loki", "Failed to get primary device public key for " + pubKey + ". " + e.getMessage());
return Recipient.from(context, Address.fromSerialized(pubKey), false);
}
}

View File

@ -186,12 +186,12 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(
}
override fun removePairingAuthorisations(hexEncodedPublicKey: String) {
val database = databaseHelper.readableDatabase
val database = databaseHelper.writableDatabase
database.delete(pairingAuthorisationCache, "$primaryDevicePublicKey = ? OR $secondaryDevicePublicKey = ?", arrayOf( hexEncodedPublicKey, hexEncodedPublicKey ))
}
fun removePairingAuthorisation(primaryDevicePublicKey: String, secondaryDevicePublicKey: String) {
val database = databaseHelper.readableDatabase
val database = databaseHelper.writableDatabase
database.delete(pairingAuthorisationCache, "${Companion.primaryDevicePublicKey} = ? OR ${Companion.secondaryDevicePublicKey} = ?", arrayOf( primaryDevicePublicKey, secondaryDevicePublicKey ))
}
}

View File

@ -10,7 +10,6 @@ import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.database.RecipientDatabase
import org.thoughtcrime.securesms.jobs.PushDecryptJob
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
import org.thoughtcrime.securesms.recipients.Recipient
@ -246,8 +245,6 @@ class LokiPublicChatPoller(private val context: Context, private val group: Loki
displayNameUpdatees = displayNameUpdatees.union(newDisplayNameUpdatees)
}.successBackground { messages ->
// Process messages in the background
val key = group.displayName.replace(Regex("\\s*"), "")
TextSecurePreferences.setNewMessagesNumber(this.context, key, TextSecurePreferences.getNewMessagesNumber(this.context, key) + messages.size)
messages.forEach { message ->
if (userDevices.contains(message.hexEncodedPublicKey)) {
processOutgoingMessage(message)

View File

@ -185,14 +185,6 @@ public class TextSecurePreferences {
private static final String MEDIA_KEYBOARD_MODE = "pref_media_keyboard_mode";
public static void setNewMessagesNumber(@NonNull Context context, String key, int value) {
setIntegerPrefrence(context, key, value);
}
public static int getNewMessagesNumber(@NonNull Context context, String key) {
return getIntegerPreference(context, key, 0);
}
public static boolean isScreenLockEnabled(@NonNull Context context) {
return getBooleanPreference(context, SCREEN_LOCK, false);
}