From 842be4378a81cf5b32ea0b66a99e64491d4beb88 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 9 Sep 2019 12:53:31 +1000 Subject: [PATCH] Show correct names on public chats. --- .../conversation/ConversationItem.java | 20 +++++++------- .../database/helpers/SQLCipherOpenHelper.java | 10 +++++-- .../securesms/loki/LokiGroupChatPoller.kt | 5 +++- .../securesms/loki/LokiUserDatabase.kt | 26 ++++++++++++++++++- .../securesms/util/GroupUtil.java | 6 ++++- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationItem.java b/src/org/thoughtcrime/securesms/conversation/ConversationItem.java index c089c50f19..35b1be25d1 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationItem.java @@ -99,15 +99,7 @@ import org.thoughtcrime.securesms.mms.TextSlide; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; import org.thoughtcrime.securesms.stickers.StickerUrl; -import org.thoughtcrime.securesms.util.DateUtils; -import org.thoughtcrime.securesms.util.DynamicTheme; -import org.thoughtcrime.securesms.util.LongClickCopySpan; -import org.thoughtcrime.securesms.util.LongClickMovementMethod; -import org.thoughtcrime.securesms.util.SearchUtil; -import org.thoughtcrime.securesms.util.TextSecurePreferences; -import org.thoughtcrime.securesms.util.ThemeUtil; -import org.thoughtcrime.securesms.util.Util; -import org.thoughtcrime.securesms.util.ViewUtil; +import org.thoughtcrime.securesms.util.*; import org.thoughtcrime.securesms.util.views.Stub; import org.whispersystems.libsignal.util.guava.Optional; @@ -883,7 +875,15 @@ public class ConversationItem extends LinearLayout @SuppressLint("SetTextI18n") private void setGroupMessageStatus(MessageRecord messageRecord, Recipient recipient) { if (groupThread && !messageRecord.isOutgoing()) { - this.groupSender.setText(recipient.toShortString()); + // Show custom display names for group chats + String displayName = recipient.toShortString(); + try { + String serverId = GroupUtil.getDecodedStringId(conversationRecipient.getAddress().serialize()); + String senderDisplayName = DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(serverId, recipient.getAddress().serialize()); + if (senderDisplayName != null) { displayName = senderDisplayName; } + } catch (Exception e) {} + + this.groupSender.setText(displayName); if (recipient.getName() == null && !TextUtils.isEmpty(recipient.getProfileName())) { this.groupSenderProfileName.setText("~" + recipient.getProfileName()); diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index 5c54f7d13b..31ca0310ec 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -73,8 +73,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private static final int JOBMANAGER_STRIKES_BACK = 20; private static final int STICKERS = 21; private static final int lokiV1 = 22; + private static final int lokiV2 = 23; - private static final int DATABASE_VERSION = lokiV1; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes + private static final int DATABASE_VERSION = lokiV2; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes private static final String DATABASE_NAME = "signal.db"; private final Context context; @@ -134,7 +135,8 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiMessageDatabase.getCreateTableCommand()); db.execSQL(LokiThreadDatabase.getCreateFriendRequestTableCommand()); db.execSQL(LokiThreadDatabase.getCreateSessionResetTableCommand()); - db.execSQL(LokiUserDatabase.getCreateTableCommand()); + db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand()); + db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); executeStatements(db, SmsDatabase.CREATE_INDEXS); executeStatements(db, MmsDatabase.CREATE_INDEXS); @@ -493,6 +495,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiAPIDatabase.getCreateLastDeletionServerIDTableCommand()); } + if (oldVersion < lokiV2) { + db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand()); + } + db.setTransactionSuccessful(); } finally { db.endTransaction(); diff --git a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt index e1053d38bc..b0e6fcb59f 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiGroupChatPoller.kt @@ -93,9 +93,12 @@ class LokiGroupChatPoller(private val context: Context, private val group: LokiG val id = group.id.toByteArray() val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) val x2 = SignalServiceDataMessage(message.timestamp, x1, null, message.body) - val senderDisplayName = "${message.displayName} (...${message.hexEncodedPublicKey.takeLast(8)})" val x3 = SignalServiceContent(x2, message.hexEncodedPublicKey, SignalServiceAddress.DEFAULT_DEVICE_ID, message.timestamp, false) + PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.of(message.serverID)) + + val senderDisplayName = "${message.displayName} (...${message.hexEncodedPublicKey.takeLast(8)})" + DatabaseFactory.getLokiUserDatabase(context).setServerDisplayName(group.id, message.hexEncodedPublicKey, senderDisplayName) } fun processOutgoingMessage(message: LokiGroupMessage) { val messageServerID = message.serverID ?: return diff --git a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt index 50a2faee8f..fd71896a09 100644 --- a/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/LokiUserDatabase.kt @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.loki import android.content.ContentValues import android.content.Context +import android.database.sqlite.SQLiteDatabase import org.thoughtcrime.securesms.database.Address import org.thoughtcrime.securesms.database.Database import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper @@ -13,9 +14,12 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database companion object { private val displayNameTable = "loki_user_display_name_database" + private val serverDisplayNameTable = "loki_user_server_display_name_database" private val hexEncodedPublicKey = "hex_encoded_public_key" private val displayName = "display_name" - @JvmStatic val createTableCommand = "CREATE TABLE $displayNameTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);" + private val serverId = "server_id" + @JvmStatic val createDisplayNameTableCommand = "CREATE TABLE $displayNameTable ($hexEncodedPublicKey TEXT PRIMARY KEY, $displayName TEXT);" + @JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($hexEncodedPublicKey TEXT, $serverId TEXT, $displayName TEXT, PRIMARY KEY ($hexEncodedPublicKey, $serverId));" } override fun getDisplayName(hexEncodedPublicKey: String): String? { @@ -37,4 +41,24 @@ class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database database.insertOrUpdate(displayNameTable, row, "${Companion.hexEncodedPublicKey} = ?", arrayOf( hexEncodedPublicKey )) Recipient.from(context, Address.fromSerialized(hexEncodedPublicKey), false).notifyListeners() } + + 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)) + } + } + + fun setServerDisplayName(serverId: String, hexEncodedPublicKey: String, displayName: String) { + val database = databaseHelper.writableDatabase + val values = ContentValues(3) + values.put(Companion.serverId, serverId) + values.put(Companion.hexEncodedPublicKey, hexEncodedPublicKey) + values.put(Companion.displayName, displayName) + try { + database.insertWithOnConflict(serverDisplayNameTable, null, values, SQLiteDatabase.CONFLICT_REPLACE) + } catch (e: Exception) { + print(e) + } + } } \ No newline at end of file diff --git a/src/org/thoughtcrime/securesms/util/GroupUtil.java b/src/org/thoughtcrime/securesms/util/GroupUtil.java index 6b83304ee1..224887fb0a 100644 --- a/src/org/thoughtcrime/securesms/util/GroupUtil.java +++ b/src/org/thoughtcrime/securesms/util/GroupUtil.java @@ -15,7 +15,6 @@ import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; -import org.thoughtcrime.securesms.sms.MessageSender; import org.whispersystems.libsignal.util.guava.Optional; import java.io.IOException; @@ -43,6 +42,11 @@ public class GroupUtil { return Hex.fromStringCondensed(groupId.split("!", 2)[1]); } + public static String getDecodedStringId(String groupId) throws IOException { + byte[] id = getDecodedId(groupId); + return new String(id); + } + public static boolean isEncodedGroup(@NonNull String groupId) { return groupId.startsWith(ENCODED_SIGNAL_GROUP_PREFIX) || groupId.startsWith(ENCODED_MMS_GROUP_PREFIX); }