Fix @room mentions crashing in debug builds (#3107)

* Fix `@room` mentions crashing in debug builds

* Iterate on previous solution, add `MentionSpan.Type.EVERYONE`
This commit is contained in:
Jorge Martin Espinosa
2024-06-27 17:31:19 +02:00
committed by GitHub
parent bef0d85415
commit a95d610464
5 changed files with 17 additions and 15 deletions

View File

@@ -94,8 +94,10 @@ private fun updateMentionSpans(text: CharSequence?, cache: RoomMemberProfilesCac
mentionSpan.text = displayName
}
}
// There's no need to do anything for `@room` pills
MentionSpan.Type.EVERYONE -> Unit
// Nothing yet for room mentions
else -> Unit
MentionSpan.Type.ROOM -> Unit
}
}
}

View File

@@ -87,6 +87,7 @@ class MentionSpan(
append("#")
}
}
Type.EVERYONE -> Unit
}
append(mentionText.substring(0, min(mentionText.length, MAX_LENGTH)))
if (mentionText.length > MAX_LENGTH) {
@@ -98,6 +99,7 @@ class MentionSpan(
enum class Type {
USER,
ROOM,
EVERYONE,
}
}

View File

@@ -108,7 +108,7 @@ class MentionSpanProvider @AssistedInject constructor(
MentionSpan(
text = text,
rawValue = "@room",
type = MentionSpan.Type.USER,
type = MentionSpan.Type.EVERYONE,
backgroundColor = otherBackgroundColor,
textColor = otherTextColor,
startPadding = startPaddingPx,

View File

@@ -93,13 +93,16 @@ class MarkdownTextEditorState(
for (mention in mentions.reversed()) {
val start = charSequence.getSpanStart(mention)
val end = charSequence.getSpanEnd(mention)
if (mention.type == MentionSpan.Type.USER) {
if (mention.rawValue == "@room") {
replace(start, end, "@room")
} else {
when (mention.type) {
MentionSpan.Type.USER -> {
val link = permalinkBuilder.permalinkForUser(UserId(mention.rawValue)).getOrNull() ?: continue
replace(start, end, "[${mention.rawValue}]($link)")
}
MentionSpan.Type.EVERYONE -> {
replace(start, end, "@room")
}
// Nothing to do here yet
MentionSpan.Type.ROOM -> Unit
}
}
}
@@ -114,14 +117,9 @@ class MarkdownTextEditorState(
val mentionSpans = text.getSpans<MentionSpan>(0, text.length)
return mentionSpans.mapNotNull { mentionSpan ->
when (mentionSpan.type) {
MentionSpan.Type.USER -> {
if (mentionSpan.rawValue == "@room") {
Mention.AtRoom
} else {
Mention.User(UserId(mentionSpan.rawValue))
}
}
else -> null
MentionSpan.Type.USER -> Mention.User(UserId(mentionSpan.rawValue))
MentionSpan.Type.EVERYONE -> Mention.AtRoom
MentionSpan.Type.ROOM -> null
}
}
}

View File

@@ -156,7 +156,7 @@ class MarkdownTextEditorStateTest {
private fun aMarkdownTextWithMentions(): CharSequence {
val userMentionSpan = MentionSpan("@Alice", "@alice:matrix.org", MentionSpan.Type.USER, 0, 0, 0, 0)
val atRoomMentionSpan = MentionSpan("@room", "@room", MentionSpan.Type.USER, 0, 0, 0, 0)
val atRoomMentionSpan = MentionSpan("@room", "@room", MentionSpan.Type.EVERYONE, 0, 0, 0, 0)
return buildSpannedString {
append("Hello ")
inSpans(userMentionSpan) {