This commit is contained in:
Andrew 2023-07-13 12:00:43 +10:00 committed by GitHub
commit 633741f04b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 69 deletions

View file

@ -98,7 +98,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
} }
private fun getDisplayName(): String = private fun getDisplayName(): String =
TextSecurePreferences.getProfileName(this) ?: truncateIdForDisplay(hexEncodedPublicKey) TextSecurePreferences.getProfileName(this)?.let(::maybeTruncateIdForDisplay) ?: truncateIdForDisplay(hexEncodedPublicKey)
private fun setupProfilePictureView(view: ProfilePictureView) { private fun setupProfilePictureView(view: ProfilePictureView) {
view.glide = glide view.glide = glide

View file

@ -7,96 +7,82 @@ import org.session.libsession.messaging.calls.CallMessageType
import org.session.libsession.messaging.contacts.Contact import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage
import org.session.libsession.utilities.ExpirationUtil import org.session.libsession.utilities.ExpirationUtil
import org.session.libsession.utilities.maybeTruncateIdForDisplay
import org.session.libsession.utilities.truncateIdForDisplay import org.session.libsession.utilities.truncateIdForDisplay
object UpdateMessageBuilder { object UpdateMessageBuilder {
val storage = MessagingModuleConfiguration.shared.storage
fun buildGroupUpdateMessage(context: Context, updateMessageData: UpdateMessageData, senderId: String? = null, isOutgoing: Boolean = false): String { fun buildGroupUpdateMessage(context: Context, updateMessageData: UpdateMessageData, senderId: String? = null, isOutgoing: Boolean = false): String {
var message = "" val updateData = updateMessageData.kind ?: return ""
val updateData = updateMessageData.kind ?: return message if (!isOutgoing && senderId == null) return ""
if (!isOutgoing && senderId == null) return message
val storage = MessagingModuleConfiguration.shared.storage
val senderName: String = if (!isOutgoing) {
storage.getContactWithSessionID(senderId!!)?.displayName(Contact.ContactContext.REGULAR) ?: truncateIdForDisplay(senderId)
} else { context.getString(R.string.MessageRecord_you) }
when (updateData) { val senderName: String = context.getDisplayNameOrTruncatedIdOrYou(senderId, isOutgoing)
is UpdateMessageData.Kind.GroupCreation -> {
message = if (isOutgoing) { return when (updateData) {
context.getString(R.string.MessageRecord_you_created_a_new_group) is UpdateMessageData.Kind.GroupCreation -> when {
} else { isOutgoing -> context.getString(R.string.MessageRecord_you_created_a_new_group)
context.getString(R.string.MessageRecord_s_added_you_to_the_group, senderName) else -> context.getString(R.string.MessageRecord_s_added_you_to_the_group, senderName)
}
} }
is UpdateMessageData.Kind.GroupNameChange -> { is UpdateMessageData.Kind.GroupNameChange -> when {
message = if (isOutgoing) { isOutgoing -> context.getString(R.string.MessageRecord_you_renamed_the_group_to_s, updateData.name)
context.getString(R.string.MessageRecord_you_renamed_the_group_to_s, updateData.name) else -> context.getString(R.string.MessageRecord_s_renamed_the_group_to_s, senderName, updateData.name)
} else {
context.getString(R.string.MessageRecord_s_renamed_the_group_to_s, senderName, updateData.name)
}
} }
is UpdateMessageData.Kind.GroupMemberAdded -> { is UpdateMessageData.Kind.GroupMemberAdded -> {
val members = updateData.updatedMembers.joinToString(", ") { val members = updateData.updatedMembers.joinToString(", ", transform = ::getDisplayNameOrTruncatedId)
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it when {
} isOutgoing -> context.getString(R.string.MessageRecord_you_added_s_to_the_group, members)
message = if (isOutgoing) { else -> context.getString(R.string.MessageRecord_s_added_s_to_the_group, senderName, members)
context.getString(R.string.MessageRecord_you_added_s_to_the_group, members)
} else {
context.getString(R.string.MessageRecord_s_added_s_to_the_group, senderName, members)
} }
} }
is UpdateMessageData.Kind.GroupMemberRemoved -> { is UpdateMessageData.Kind.GroupMemberRemoved -> {
val storage = MessagingModuleConfiguration.shared.storage when (storage.getUserPublicKey()!!) {
val userPublicKey = storage.getUserPublicKey()!! // 1st case: you are part of the removed members
// 1st case: you are part of the removed members in updateData.updatedMembers -> when {
message = if (userPublicKey in updateData.updatedMembers) { isOutgoing -> context.getString(R.string.MessageRecord_left_group)
if (isOutgoing) { else -> context.getString(R.string.MessageRecord_you_were_removed_from_the_group)
context.getString(R.string.MessageRecord_left_group)
} else {
context.getString(R.string.MessageRecord_you_were_removed_from_the_group)
} }
} else {
// 2nd case: you are not part of the removed members // 2nd case: you are not part of the removed members
val members = updateData.updatedMembers.joinToString(", ") { else -> {
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it val members = updateData.updatedMembers.joinToString(", ", transform = ::truncateIdForDisplay)
} when {
if (isOutgoing) { isOutgoing -> context.getString(R.string.MessageRecord_you_removed_s_from_the_group, members)
context.getString(R.string.MessageRecord_you_removed_s_from_the_group, members) else -> context.getString(R.string.MessageRecord_s_removed_s_from_the_group, senderName, members)
} else { }
context.getString(R.string.MessageRecord_s_removed_s_from_the_group, senderName, members)
} }
} }
} }
is UpdateMessageData.Kind.GroupMemberLeft -> { is UpdateMessageData.Kind.GroupMemberLeft -> when {
message = if (isOutgoing) { isOutgoing -> context.getString(R.string.MessageRecord_left_group)
context.getString(R.string.MessageRecord_left_group) else -> context.getString(R.string.ConversationItem_group_action_left, senderName)
} else {
context.getString(R.string.ConversationItem_group_action_left, senderName)
}
} }
else -> ""
} }
return message
} }
fun buildExpirationTimerMessage(context: Context, duration: Long, senderId: String? = null, isOutgoing: Boolean = false): String { private fun Context.getDisplayNameOrTruncatedIdOrYou(senderId: String?, isOutgoing: Boolean) = when {
if (!isOutgoing && senderId == null) return "" isOutgoing -> getString(R.string.MessageRecord_you)
val storage = MessagingModuleConfiguration.shared.storage else -> getDisplayNameOrTruncatedId(senderId!!)
val senderName: String? = if (!isOutgoing) { }
storage.getContactWithSessionID(senderId!!)?.displayName(Contact.ContactContext.REGULAR) ?: truncateIdForDisplay(senderId) private fun getDisplayNameOrTruncatedId(senderId: String) =
} else { context.getString(R.string.MessageRecord_you) } storage.getContactWithSessionID(senderId)
return if (duration <= 0) { ?.displayName(Contact.ContactContext.REGULAR)
?.let(::maybeTruncateIdForDisplay)
?: truncateIdForDisplay(senderId)
fun buildExpirationTimerMessage(context: Context, duration: Long, senderId: String? = null, isOutgoing: Boolean = false): String =
if (!isOutgoing && senderId == null) {
""
} else if (duration <= 0) {
if (isOutgoing) context.getString(R.string.MessageRecord_you_disabled_disappearing_messages) if (isOutgoing) context.getString(R.string.MessageRecord_you_disabled_disappearing_messages)
else context.getString(R.string.MessageRecord_s_disabled_disappearing_messages, senderName) else context.getString(R.string.MessageRecord_s_disabled_disappearing_messages, getDisplayNameOrTruncatedId(senderId!!))
} else { } else {
val time = ExpirationUtil.getExpirationDisplayValue(context, duration.toInt()) val time = ExpirationUtil.getExpirationDisplayValue(context, duration.toInt())
if (isOutgoing)context.getString(R.string.MessageRecord_you_set_disappearing_message_time_to_s, time) if (isOutgoing)context.getString(R.string.MessageRecord_you_set_disappearing_message_time_to_s, time)
else context.getString(R.string.MessageRecord_s_set_disappearing_message_time_to_s, senderName, time) else context.getString(R.string.MessageRecord_s_set_disappearing_message_time_to_s, getDisplayNameOrTruncatedId(senderId!!), time)
} }
}
fun buildDataExtractionMessage(context: Context, kind: DataExtractionNotificationInfoMessage.Kind, senderId: String? = null): String { fun buildDataExtractionMessage(context: Context, kind: DataExtractionNotificationInfoMessage.Kind, senderId: String): String {
val storage = MessagingModuleConfiguration.shared.storage val senderName = getDisplayNameOrTruncatedId(senderId)
val senderName = storage.getContactWithSessionID(senderId!!)?.displayName(Contact.ContactContext.REGULAR) ?: truncateIdForDisplay(senderId)
return when (kind) { return when (kind) {
DataExtractionNotificationInfoMessage.Kind.SCREENSHOT -> DataExtractionNotificationInfoMessage.Kind.SCREENSHOT ->
context.getString(R.string.MessageRecord_s_took_a_screenshot, senderName) context.getString(R.string.MessageRecord_s_took_a_screenshot, senderName)
@ -105,9 +91,8 @@ object UpdateMessageBuilder {
} }
} }
fun buildCallMessage(context: Context, type: CallMessageType, sender: String): String { fun buildCallMessage(context: Context, type: CallMessageType, senderId: String): String {
val storage = MessagingModuleConfiguration.shared.storage val senderName = getDisplayNameOrTruncatedId(senderId)
val senderName = storage.getContactWithSessionID(sender)?.displayName(Contact.ContactContext.REGULAR) ?: sender
return when (type) { return when (type) {
CallMessageType.CALL_MISSED -> CallMessageType.CALL_MISSED ->
context.getString(R.string.MessageRecord_missed_call_from, senderName) context.getString(R.string.MessageRecord_missed_call_from, senderName)

View file

@ -1,4 +1,16 @@
package org.session.libsession.utilities package org.session.libsession.utilities
/**
* This function takes an educated guess that a name with length over 60 containing no whitespace is
* probably a session id. If one is received it will be truncated.
*
* @return the name of the user or a truncated id if their name has been set to their id.
*/
fun maybeTruncateIdForDisplay(nameOrId: String): String =
nameOrId.takeIf { it.length < 60 || it.contains(' ') } ?: truncateIdForDisplay(nameOrId)
/**
* @return a truncated user id containing the first 4 and last 4 chars.
*/
fun truncateIdForDisplay(id: String): String = fun truncateIdForDisplay(id: String): String =
id.takeIf { it.length > 8 }?.apply{ "${take(4)}${takeLast(4)}" } ?: id id.takeIf { it.length > 8 }?.run{ "${take(4)}${takeLast(4)}" } ?: id

View file

@ -0,0 +1,25 @@
package org.session.libsession.utilities
import org.junit.Assert
import org.junit.Test
class IdUtilTest {
@Test
fun testTruncate() {
val testString = "123456789"
val result = truncateIdForDisplay(testString)
Assert.assertEquals(result, "1234…6789")
}
@Test
fun testDontTruncateShortMessage() {
val testString = "not much"
val result = truncateIdForDisplay(testString)
Assert.assertEquals(result, "not much")
}
}