Skip to content

Commit d6b259b

Browse files
authored
Fix @room mentions crashing in debug builds (#3107)
* Fix `@room` mentions crashing in debug builds * Iterate on previous solution, add `MentionSpan.Type.EVERYONE`
1 parent ed00ed4 commit d6b259b

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/event/TimelineItemTextView.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ private fun updateMentionSpans(text: CharSequence?, cache: RoomMemberProfilesCac
9494
mentionSpan.text = displayName
9595
}
9696
}
97+
// There's no need to do anything for `@room` pills
98+
MentionSpan.Type.EVERYONE -> Unit
9799
// Nothing yet for room mentions
98-
else -> Unit
100+
MentionSpan.Type.ROOM -> Unit
99101
}
100102
}
101103
}

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/mentions/MentionSpan.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class MentionSpan(
8787
append("#")
8888
}
8989
}
90+
Type.EVERYONE -> Unit
9091
}
9192
append(mentionText.substring(0, min(mentionText.length, MAX_LENGTH)))
9293
if (mentionText.length > MAX_LENGTH) {
@@ -98,6 +99,7 @@ class MentionSpan(
9899
enum class Type {
99100
USER,
100101
ROOM,
102+
EVERYONE,
101103
}
102104
}
103105

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/mentions/MentionSpanProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class MentionSpanProvider @AssistedInject constructor(
108108
MentionSpan(
109109
text = text,
110110
rawValue = "@room",
111-
type = MentionSpan.Type.USER,
111+
type = MentionSpan.Type.EVERYONE,
112112
backgroundColor = otherBackgroundColor,
113113
textColor = otherTextColor,
114114
startPadding = startPaddingPx,

libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/model/MarkdownTextEditorState.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ class MarkdownTextEditorState(
9393
for (mention in mentions.reversed()) {
9494
val start = charSequence.getSpanStart(mention)
9595
val end = charSequence.getSpanEnd(mention)
96-
if (mention.type == MentionSpan.Type.USER) {
97-
if (mention.rawValue == "@room") {
98-
replace(start, end, "@room")
99-
} else {
96+
when (mention.type) {
97+
MentionSpan.Type.USER -> {
10098
val link = permalinkBuilder.permalinkForUser(UserId(mention.rawValue)).getOrNull() ?: continue
10199
replace(start, end, "[${mention.rawValue}]($link)")
102100
}
101+
MentionSpan.Type.EVERYONE -> {
102+
replace(start, end, "@room")
103+
}
104+
// Nothing to do here yet
105+
MentionSpan.Type.ROOM -> Unit
103106
}
104107
}
105108
}
@@ -114,14 +117,9 @@ class MarkdownTextEditorState(
114117
val mentionSpans = text.getSpans<MentionSpan>(0, text.length)
115118
return mentionSpans.mapNotNull { mentionSpan ->
116119
when (mentionSpan.type) {
117-
MentionSpan.Type.USER -> {
118-
if (mentionSpan.rawValue == "@room") {
119-
Mention.AtRoom
120-
} else {
121-
Mention.User(UserId(mentionSpan.rawValue))
122-
}
123-
}
124-
else -> null
120+
MentionSpan.Type.USER -> Mention.User(UserId(mentionSpan.rawValue))
121+
MentionSpan.Type.EVERYONE -> Mention.AtRoom
122+
MentionSpan.Type.ROOM -> null
125123
}
126124
}
127125
}

libraries/textcomposer/impl/src/test/kotlin/io/element/android/libraries/textcomposer/impl/model/MarkdownTextEditorStateTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class MarkdownTextEditorStateTest {
156156

157157
private fun aMarkdownTextWithMentions(): CharSequence {
158158
val userMentionSpan = MentionSpan("@Alice", "@alice:matrix.org", MentionSpan.Type.USER, 0, 0, 0, 0)
159-
val atRoomMentionSpan = MentionSpan("@room", "@room", MentionSpan.Type.USER, 0, 0, 0, 0)
159+
val atRoomMentionSpan = MentionSpan("@room", "@room", MentionSpan.Type.EVERYONE, 0, 0, 0, 0)
160160
return buildSpannedString {
161161
append("Hello ")
162162
inSpans(userMentionSpan) {

0 commit comments

Comments
 (0)