Truncate name to 100 bytes on restore account

This commit is contained in:
bemusementpark 2024-07-08 13:02:30 +09:30
parent 1570e2338d
commit 84b1fb6527

View File

@ -66,6 +66,7 @@ import org.session.libsession.utilities.GroupRecord
import org.session.libsession.utilities.GroupUtil import org.session.libsession.utilities.GroupUtil
import org.session.libsession.utilities.ProfileKeyUtil import org.session.libsession.utilities.ProfileKeyUtil
import org.session.libsession.utilities.SSKEnvironment import org.session.libsession.utilities.SSKEnvironment
import org.session.libsession.utilities.SSKEnvironment.ProfileManagerProtocol.Companion.NAME_PADDED_LENGTH
import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.recipients.Recipient
import org.session.libsession.utilities.recipients.Recipient.DisappearingState import org.session.libsession.utilities.recipients.Recipient.DisappearingState
@ -475,9 +476,11 @@ open class Storage(
val name = userProfile.getName() ?: return val name = userProfile.getName() ?: return
val userPic = userProfile.getPic() val userPic = userProfile.getPic()
val profileManager = SSKEnvironment.shared.profileManager val profileManager = SSKEnvironment.shared.profileManager
if (name.isNotEmpty()) {
TextSecurePreferences.setProfileName(context, name) name.takeUnless { it.isEmpty() }?.truncate(NAME_PADDED_LENGTH)?.let {
profileManager.setName(context, recipient, name) TextSecurePreferences.setProfileName(context, it)
profileManager.setName(context, recipient, it)
if (it != name) userProfile.setName(it)
} }
// update pfp // update pfp
@ -1798,4 +1801,12 @@ open class Storage(
lokiDb.setLastLegacySenderAddress(recipientAddress, null) lokiDb.setLastLegacySenderAddress(recipientAddress, null)
} }
} }
} }
/**
* Truncate a string to a specified number of bytes
*
* This could split multi-byte characters/emojis.
*/
private fun String.truncate(sizeInBytes: Int): String =
toByteArray().takeIf { it.size > sizeInBytes }?.take(sizeInBytes)?.toByteArray()?.let(::String) ?: this