Improve new convo error messages

This commit is contained in:
Andrew 2024-03-27 12:22:53 +10:30
parent 006c50e38d
commit e25b90b229
6 changed files with 25 additions and 27 deletions

View File

@ -46,7 +46,7 @@ class NewConversationFragment : BottomSheetDialogFragment(), NewConversationDele
val bottomSheetDialog = it as BottomSheetDialog
val parentLayout =
bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
parentLayout?.let { it ->
parentLayout?.let {
val behaviour = BottomSheetBehavior.from(it)
val layoutParams = it.layoutParams
layoutParams.height = defaultPeekHeight

View File

@ -60,8 +60,12 @@ class NewMessageFragment : Fragment() {
}
private fun createPrivateChatIfPossible(onsNameOrPublicKey: String) {
if (PublicKeyValidation.isValid(onsNameOrPublicKey)) {
createPrivateChat(onsNameOrPublicKey)
if (PublicKeyValidation.isValid(onsNameOrPublicKey, isPrefixRequired = false)) {
if (PublicKeyValidation.hasValidPrefix(onsNameOrPublicKey)) {
createPrivateChat(onsNameOrPublicKey)
} else {
Toast.makeText(requireContext(), R.string.accountIdErrorInvalid, Toast.LENGTH_SHORT).show()
}
} else {
// This could be an ONS name
showLoader()
@ -70,9 +74,9 @@ class NewMessageFragment : Fragment() {
createPrivateChat(hexEncodedPublicKey)
}.failUi { exception ->
hideLoader()
var message = getString(R.string.fragment_enter_public_key_error_message)
exception.localizedMessage?.let {
message = it
val message = when (exception) {
is SnodeAPI.Error.Generic -> "We couldnt recognize this ONS. Please check and try again."
else -> exception.localizedMessage ?: getString(R.string.fragment_enter_public_key_error_message)
}
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}
@ -81,12 +85,11 @@ class NewMessageFragment : Fragment() {
private fun createPrivateChat(hexEncodedPublicKey: String) {
val recipient = Recipient.from(requireContext(), Address.fromSerialized(hexEncodedPublicKey), false)
val intent = Intent(requireContext(), ConversationActivityV2::class.java)
intent.putExtra(ConversationActivityV2.ADDRESS, recipient.address)
intent.setDataAndType(requireActivity().intent.data, requireActivity().intent.type)
val existingThread = DatabaseComponent.get(requireContext()).threadDatabase().getThreadIdIfExistsFor(recipient)
intent.putExtra(ConversationActivityV2.THREAD_ID, existingThread)
requireContext().startActivity(intent)
Intent(requireContext(), ConversationActivityV2::class.java).apply {
putExtra(ConversationActivityV2.ADDRESS, recipient.address)
setDataAndType(requireActivity().intent.data, requireActivity().intent.type)
putExtra(ConversationActivityV2.THREAD_ID, DatabaseComponent.get(requireContext()).threadDatabase().getThreadIdIfExistsFor(recipient))
}.let(requireContext()::startActivity)
delegate.onDialogClosePressed()
}

View File

@ -1081,4 +1081,5 @@
<string name="activity_link_load_account">Load Account</string>
<string name="activity_link_camera_permission_permanently_denied_configure_in_settings">Camera Permission permanently denied. Configure in settings.</string>
<string name="activity_link_enter_your_recovery_password_to_load_your_account_if_you_haven_t_saved_it_you_can_find_it_in_your_app_settings">Enter your recovery password to load your account. If you haven\'t saved it, you can find it in your app settings.</string>
<string name="accountIdErrorInvalid">This Account ID is invalid. Please check and try again.</string>
</resources>

View File

@ -91,7 +91,7 @@ object SnodeAPI {
const val useTestnet = false
// Error
internal sealed class Error(val description: String) : Exception(description) {
sealed class Error(val description: String) : Exception(description) {
object Generic : Error("An error occurred.")
object ClockOutOfSync : Error("Your clock is out of sync with the Service Node network.")
object NoKeyPair : Error("Missing user key pair.")

View File

@ -1,7 +1,7 @@
package org.session.libsignal.utilities
enum class IdPrefix(val value: String) {
STANDARD("05"), BLINDED("15"), UN_BLINDED("00"), BLINDEDV2("25");
STANDARD("05"), BLINDED("15"), UN_BLINDED("00"), BLINDEDV2("25"), GROUP("03");
fun isBlinded() = value == BLINDED.value || value == BLINDEDV2.value
@ -11,6 +11,7 @@ enum class IdPrefix(val value: String) {
BLINDED.value -> BLINDED
BLINDEDV2.value -> BLINDEDV2
UN_BLINDED.value -> UN_BLINDED
GROUP.value -> GROUP
else -> null
}
}

View File

@ -1,18 +1,11 @@
package org.session.libsignal.utilities
object PublicKeyValidation {
private val HEX_CHARACTERS = "0123456789ABCDEF".toSet()
private val INVALID_PREFIXES = setOf(IdPrefix.GROUP, IdPrefix.BLINDED, IdPrefix.BLINDEDV2)
@JvmStatic
fun isValid(candidate: String): Boolean {
return isValid(candidate, 66, true)
}
@JvmStatic
fun isValid(candidate: String, expectedLength: Int, isPrefixRequired: Boolean): Boolean {
val hexCharacters = "0123456789ABCDEF".toSet()
val isValidHexEncoding = hexCharacters.containsAll(candidate.uppercase().toSet())
val hasValidLength = candidate.length == expectedLength
val hasValidPrefix = if (isPrefixRequired) IdPrefix.fromValue(candidate) != null else true
return isValidHexEncoding && hasValidLength && hasValidPrefix
}
fun isValid(candidate: String, isPrefixRequired: Boolean = true): Boolean = hasValidLength(candidate) && isValidHexEncoding(candidate) && (!isPrefixRequired || IdPrefix.fromValue(candidate) != null)
fun hasValidPrefix(candidate: String) = IdPrefix.fromValue(candidate) !in INVALID_PREFIXES
private fun hasValidLength(candidate: String) = candidate.length == 66
private fun isValidHexEncoding(candidate: String) = HEX_CHARACTERS.containsAll(candidate.uppercase().toSet())
}