From 0956d658089c3df11fd99d6e46148280cd1152bf Mon Sep 17 00:00:00 2001 From: SessionHero01 <180888785+SessionHero01@users.noreply.github.com> Date: Tue, 4 Feb 2025 10:02:15 +1100 Subject: [PATCH] Using member names in the control messages wherever possible (#914) --- .../database/model/MessageRecord.java | 6 ++- .../securesms/home/HomeViewModel.kt | 8 ++-- .../utilities/UpdateMessageBuilder.kt | 43 +++++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/model/MessageRecord.java b/app/src/main/java/org/thoughtcrime/securesms/database/model/MessageRecord.java index a27da20ee4..7e5e9ced9d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/model/MessageRecord.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/model/MessageRecord.java @@ -36,6 +36,7 @@ import org.session.libsession.utilities.IdentityKeyMismatch; import org.session.libsession.utilities.NetworkFailure; import org.session.libsession.utilities.ThemeUtil; import org.session.libsession.utilities.recipients.Recipient; +import org.session.libsignal.utilities.AccountId; import org.thoughtcrime.securesms.dependencies.DatabaseComponent; import network.loki.messenger.R; @@ -137,12 +138,15 @@ public abstract class MessageRecord extends DisplayRecord { public CharSequence getDisplayBody(@NonNull Context context) { if (isGroupUpdateMessage()) { UpdateMessageData updateMessageData = getGroupUpdateMessage(); - if (updateMessageData == null) { + Recipient groupRecipient = DatabaseComponent.get(context).threadDatabase().getRecipientForThreadId(getThreadId()); + + if (updateMessageData == null || groupRecipient == null || !groupRecipient.isGroupV2Recipient()) { return ""; } SpannableString text = new SpannableString(UpdateMessageBuilder.buildGroupUpdateMessage( context, + new AccountId(groupRecipient.getAddress().serialize()), updateMessageData, MessagingModuleConfiguration.getShared().getConfigFactory(), isOutgoing(), diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/HomeViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/home/HomeViewModel.kt index 5891d10f2a..67d80da4f3 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/HomeViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/HomeViewModel.kt @@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapLatest @@ -25,6 +26,7 @@ import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.stateIn import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_HIDDEN +import org.session.libsession.utilities.ConfigUpdateNotification import org.session.libsession.utilities.TextSecurePreferences import org.thoughtcrime.securesms.database.DatabaseContentProviders import org.thoughtcrime.securesms.database.ThreadDatabase @@ -33,7 +35,6 @@ import org.thoughtcrime.securesms.dependencies.ConfigFactory import org.thoughtcrime.securesms.sskenvironment.TypingStatusRepository import org.thoughtcrime.securesms.util.observeChanges import javax.inject.Inject -import dagger.hilt.android.qualifiers.ApplicationContext as ApplicationContextQualifier @HiltViewModel class HomeViewModel @Inject constructor( @@ -116,9 +117,10 @@ class HomeViewModel @Inject constructor( .flowOn(Dispatchers.IO) @OptIn(FlowPreview::class) - private fun reloadTriggersAndContentChanges() = merge( + private fun reloadTriggersAndContentChanges(): Flow<*> = merge( manualReloadTrigger, - contentResolver.observeChanges(DatabaseContentProviders.ConversationList.CONTENT_URI) + contentResolver.observeChanges(DatabaseContentProviders.ConversationList.CONTENT_URI), + configFactory.configUpdateNotifications.filterIsInstance() ) .debounce(CHANGE_NOTIFICATION_DEBOUNCE_MILLS) .onStart { emit(Unit) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt b/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt index 190cc9a3f6..470b72d23e 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt @@ -2,6 +2,7 @@ package org.session.libsession.messaging.utilities import android.content.Context import com.squareup.phrase.Phrase +import network.loki.messenger.libsession_util.getOrNull import org.session.libsession.R import org.session.libsession.messaging.MessagingModuleConfiguration import org.session.libsession.messaging.calls.CallMessageType @@ -37,9 +38,15 @@ object UpdateMessageBuilder { ?.displayName(Contact.ContactContext.REGULAR) ?: truncateIdForDisplay(senderId) + private fun getGroupMemberName(groupId: AccountId, memberId: String) = + storage.getContactWithAccountID(memberId)?.displayName(Contact.ContactContext.REGULAR) + ?: MessagingModuleConfiguration.shared.configFactory.withGroupConfigs(groupId) { it.groupMembers.getOrNull(memberId)?.name } + ?: truncateIdForDisplay(memberId) + @JvmStatic fun buildGroupUpdateMessage( context: Context, + groupId: AccountId, updateMessageData: UpdateMessageData, configFactory: ConfigFactoryProtocol, isOutgoing: Boolean, @@ -79,19 +86,19 @@ object UpdateMessageBuilder { } 1 -> { Phrase.from(context, R.string.legacyGroupMemberNew) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .format() } 2 -> { Phrase.from(context, R.string.legacyGroupMemberTwoNew) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) - .put(OTHER_NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(1))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) + .put(OTHER_NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(1))) .format() } else -> { val newMemberCountMinusOne = newMemberCount - 1 Phrase.from(context, R.string.legacyGroupMemberNewMultiple) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .put(COUNT_KEY, newMemberCountMinusOne) .format() } @@ -119,14 +126,14 @@ object UpdateMessageBuilder { "" // Return an empty string - we don't want to show the error in the conversation } 1 -> Phrase.from(context, R.string.groupRemoved) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .format() 2 -> Phrase.from(context, R.string.groupRemovedTwo) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) - .put(OTHER_NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(1))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) + .put(OTHER_NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(1))) .format() else -> Phrase.from(context, R.string.groupRemovedMultiple) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .put(COUNT_KEY, updateData.updatedMembers.size - 1) .format() } @@ -143,14 +150,14 @@ object UpdateMessageBuilder { "" // Return an empty string - we don't want to show the error in the conversation } 1 -> Phrase.from(context, R.string.groupRemoved) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .format() 2 -> Phrase.from(context, R.string.groupRemovedTwo) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) - .put(OTHER_NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(1))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) + .put(OTHER_NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(1))) .format() else -> Phrase.from(context, R.string.groupRemovedMultiple) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .put(COUNT_KEY, updateData.updatedMembers.size - 1) .format() } @@ -166,14 +173,14 @@ object UpdateMessageBuilder { "" // Return an empty string - we don't want to show the error in the conversation } 1 -> Phrase.from(context, R.string.groupMemberLeft) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .format() 2 -> Phrase.from(context, R.string.groupMemberLeftTwo) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) - .put(OTHER_NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(1))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) + .put(OTHER_NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(1))) .format() else -> Phrase.from(context, R.string.groupMemberLeftMultiple) - .put(NAME_KEY, getSenderName(updateData.updatedMembers.elementAt(0))) + .put(NAME_KEY, getGroupMemberName(groupId, updateData.updatedMembers.elementAt(0))) .put(COUNT_KEY, updateData.updatedMembers.size - 1) .format() } @@ -288,7 +295,7 @@ object UpdateMessageBuilder { } is UpdateMessageData.Kind.GroupInvitation -> { val approved = configFactory.getGroup(AccountId(updateData.groupAccountId))?.invited == false - val inviterName = updateData.invitingAdminName?.takeIf { it.isNotEmpty() } ?: getSenderName(updateData.invitingAdminId) + val inviterName = updateData.invitingAdminName?.takeIf { it.isNotEmpty() } ?: getGroupMemberName(groupId, updateData.invitingAdminId) return if (!approved) { Phrase.from(context, R.string.messageRequestGroupInvite) .put(NAME_KEY, inviterName) @@ -314,7 +321,7 @@ object UpdateMessageBuilder { } } - fun Context.youOrSender(sessionId: String) = if (storage.getUserPublicKey() == sessionId) getString(R.string.you) else getSenderName(sessionId) + private fun Context.youOrSender(sessionId: String) = if (storage.getUserPublicKey() == sessionId) getString(R.string.you) else getSenderName(sessionId) fun buildExpirationTimerMessage( context: Context,