Merge d6c796c96e
into 7da3e4f022
This commit is contained in:
commit
633741f04b
|
@ -98,7 +98,7 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
|
|||
}
|
||||
|
||||
private fun getDisplayName(): String =
|
||||
TextSecurePreferences.getProfileName(this) ?: truncateIdForDisplay(hexEncodedPublicKey)
|
||||
TextSecurePreferences.getProfileName(this)?.let(::maybeTruncateIdForDisplay) ?: truncateIdForDisplay(hexEncodedPublicKey)
|
||||
|
||||
private fun setupProfilePictureView(view: ProfilePictureView) {
|
||||
view.glide = glide
|
||||
|
|
|
@ -7,96 +7,82 @@ import org.session.libsession.messaging.calls.CallMessageType
|
|||
import org.session.libsession.messaging.contacts.Contact
|
||||
import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage
|
||||
import org.session.libsession.utilities.ExpirationUtil
|
||||
import org.session.libsession.utilities.maybeTruncateIdForDisplay
|
||||
import org.session.libsession.utilities.truncateIdForDisplay
|
||||
|
||||
object UpdateMessageBuilder {
|
||||
|
||||
val storage = MessagingModuleConfiguration.shared.storage
|
||||
fun buildGroupUpdateMessage(context: Context, updateMessageData: UpdateMessageData, senderId: String? = null, isOutgoing: Boolean = false): String {
|
||||
var message = ""
|
||||
val updateData = updateMessageData.kind ?: return message
|
||||
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) }
|
||||
val updateData = updateMessageData.kind ?: return ""
|
||||
if (!isOutgoing && senderId == null) return ""
|
||||
|
||||
when (updateData) {
|
||||
is UpdateMessageData.Kind.GroupCreation -> {
|
||||
message = if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_you_created_a_new_group)
|
||||
} else {
|
||||
context.getString(R.string.MessageRecord_s_added_you_to_the_group, senderName)
|
||||
}
|
||||
val senderName: String = context.getDisplayNameOrTruncatedIdOrYou(senderId, isOutgoing)
|
||||
|
||||
return when (updateData) {
|
||||
is UpdateMessageData.Kind.GroupCreation -> when {
|
||||
isOutgoing -> context.getString(R.string.MessageRecord_you_created_a_new_group)
|
||||
else -> context.getString(R.string.MessageRecord_s_added_you_to_the_group, senderName)
|
||||
}
|
||||
is UpdateMessageData.Kind.GroupNameChange -> {
|
||||
message = if (isOutgoing) {
|
||||
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)
|
||||
}
|
||||
is UpdateMessageData.Kind.GroupNameChange -> when {
|
||||
isOutgoing -> 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)
|
||||
}
|
||||
is UpdateMessageData.Kind.GroupMemberAdded -> {
|
||||
val members = updateData.updatedMembers.joinToString(", ") {
|
||||
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it
|
||||
}
|
||||
message = if (isOutgoing) {
|
||||
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)
|
||||
val members = updateData.updatedMembers.joinToString(", ", transform = ::getDisplayNameOrTruncatedId)
|
||||
when {
|
||||
isOutgoing -> 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 -> {
|
||||
val storage = MessagingModuleConfiguration.shared.storage
|
||||
val userPublicKey = storage.getUserPublicKey()!!
|
||||
// 1st case: you are part of the removed members
|
||||
message = if (userPublicKey in updateData.updatedMembers) {
|
||||
if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_left_group)
|
||||
} else {
|
||||
context.getString(R.string.MessageRecord_you_were_removed_from_the_group)
|
||||
when (storage.getUserPublicKey()!!) {
|
||||
// 1st case: you are part of the removed members
|
||||
in updateData.updatedMembers -> when {
|
||||
isOutgoing -> 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
|
||||
val members = updateData.updatedMembers.joinToString(", ") {
|
||||
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it
|
||||
}
|
||||
if (isOutgoing) {
|
||||
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 -> {
|
||||
val members = updateData.updatedMembers.joinToString(", ", transform = ::truncateIdForDisplay)
|
||||
when {
|
||||
isOutgoing -> 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
is UpdateMessageData.Kind.GroupMemberLeft -> {
|
||||
message = if (isOutgoing) {
|
||||
context.getString(R.string.MessageRecord_left_group)
|
||||
} else {
|
||||
context.getString(R.string.ConversationItem_group_action_left, senderName)
|
||||
}
|
||||
is UpdateMessageData.Kind.GroupMemberLeft -> when {
|
||||
isOutgoing -> context.getString(R.string.MessageRecord_left_group)
|
||||
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 {
|
||||
if (!isOutgoing && senderId == null) return ""
|
||||
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) }
|
||||
return if (duration <= 0) {
|
||||
private fun Context.getDisplayNameOrTruncatedIdOrYou(senderId: String?, isOutgoing: Boolean) = when {
|
||||
isOutgoing -> getString(R.string.MessageRecord_you)
|
||||
else -> getDisplayNameOrTruncatedId(senderId!!)
|
||||
}
|
||||
private fun getDisplayNameOrTruncatedId(senderId: String) =
|
||||
storage.getContactWithSessionID(senderId)
|
||||
?.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)
|
||||
else context.getString(R.string.MessageRecord_s_disabled_disappearing_messages, senderName)
|
||||
else context.getString(R.string.MessageRecord_s_disabled_disappearing_messages, getDisplayNameOrTruncatedId(senderId!!))
|
||||
} else {
|
||||
val time = ExpirationUtil.getExpirationDisplayValue(context, duration.toInt())
|
||||
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 {
|
||||
val storage = MessagingModuleConfiguration.shared.storage
|
||||
val senderName = storage.getContactWithSessionID(senderId!!)?.displayName(Contact.ContactContext.REGULAR) ?: truncateIdForDisplay(senderId)
|
||||
fun buildDataExtractionMessage(context: Context, kind: DataExtractionNotificationInfoMessage.Kind, senderId: String): String {
|
||||
val senderName = getDisplayNameOrTruncatedId(senderId)
|
||||
return when (kind) {
|
||||
DataExtractionNotificationInfoMessage.Kind.SCREENSHOT ->
|
||||
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 {
|
||||
val storage = MessagingModuleConfiguration.shared.storage
|
||||
val senderName = storage.getContactWithSessionID(sender)?.displayName(Contact.ContactContext.REGULAR) ?: sender
|
||||
fun buildCallMessage(context: Context, type: CallMessageType, senderId: String): String {
|
||||
val senderName = getDisplayNameOrTruncatedId(senderId)
|
||||
return when (type) {
|
||||
CallMessageType.CALL_MISSED ->
|
||||
context.getString(R.string.MessageRecord_missed_call_from, senderName)
|
||||
|
|
|
@ -1,4 +1,16 @@
|
|||
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 =
|
||||
id.takeIf { it.length > 8 }?.apply{ "${take(4)}…${takeLast(4)}" } ?: id
|
||||
id.takeIf { it.length > 8 }?.run{ "${take(4)}…${takeLast(4)}" } ?: id
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue