mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 11:59:12 +03:00
adding function to extract usernames from full matrix ids
This commit is contained in:
parent
aeb881e8b6
commit
38763d0575
2 changed files with 31 additions and 0 deletions
|
@ -156,6 +156,20 @@ object MatrixPatterns {
|
|||
return matrixId?.substringAfter(":", missingDelimiterValue = "")?.takeIf { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract user name from a matrix id.
|
||||
*
|
||||
* @param matrixId
|
||||
* @return null if the input is not a valid matrixId
|
||||
*/
|
||||
fun extractUserNameFromId(matrixId: String): String? {
|
||||
return if (isUserId(matrixId)) {
|
||||
matrixId.removePrefix("@").substringBefore(":", missingDelimiterValue = "")
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Orders which are not strings, or do not consist solely of ascii characters in the range \x20 (space) to \x7E (~),
|
||||
* or consist of more than 50 characters, are forbidden and the field should be ignored if received.
|
||||
|
|
|
@ -35,6 +35,23 @@ class MatrixPatternsTest {
|
|||
MatrixPatterns.isUserId(input) shouldBeEqualTo expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given matrix id cases, when extracting userName, then returns expected`() {
|
||||
val cases = listOf(
|
||||
MatrixIdCase("foobar", userName = null),
|
||||
MatrixIdCase("@foobar", userName = null),
|
||||
MatrixIdCase("foobar@matrix.org", userName = null),
|
||||
MatrixIdCase("@foobar: matrix.org", userName = null),
|
||||
MatrixIdCase("foobar:matrix.org", userName = null),
|
||||
MatrixIdCase("@foobar:matrix.org", userName = "foobar"),
|
||||
)
|
||||
|
||||
cases.forEach { (input, expected) ->
|
||||
MatrixPatterns.extractUserNameFromId(input) shouldBeEqualTo expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private data class UserIdCase(val input: String, val isUserId: Boolean)
|
||||
private data class MatrixIdCase(val input: String, val userName: String?)
|
||||
|
|
Loading…
Reference in a new issue