limit alias length in candidateAliasFromRoomName()

This commit is contained in:
Benoit Marty 2021-10-04 12:40:43 +02:00
parent c3b65a9c71
commit 7636b4d7a8
2 changed files with 9 additions and 7 deletions

View File

@ -162,12 +162,14 @@ object MatrixPatterns {
return order != null && order.length < 50 && order matches ORDER_STRING_REGEX
}
fun candidateAliasFromRoomName(name: String): String {
return Regex("\\s").replace(name.lowercase(), "_").let {
"[^a-z0-9._%#@=+-]".toRegex().replace(it, "")
}.let { alias ->
if (alias.length > 255) alias.substring(0, 255) else alias
}
fun candidateAliasFromRoomName(roomName: String, domain: String): String {
return roomName.lowercase()
// Replace spaces by '_'
.let { Regex("\\s").replace(it, "_") }
// Remove all invalid chars
.let { "[^a-z0-9._%#@=+-]".toRegex().replace(it, "") }
// limit length
.substring(0, MatrixConstants.maxAliasLocalPartLength(domain))
}
/**

View File

@ -134,7 +134,7 @@ class CreateSpaceViewModel @AssistedInject constructor(
)
} else {
val tentativeAlias =
MatrixPatterns.candidateAliasFromRoomName(action.name)
MatrixPatterns.candidateAliasFromRoomName(action.name, homeServerName)
copy(
nameInlineError = null,
name = action.name,