Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix @room mentions crashing in debug builds #3107

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class MentionSpan(
append("#")
}
}
Type.EVERYONE -> Unit
}
append(mentionText.substring(0, min(mentionText.length, MAX_LENGTH)))
if (mentionText.length > MAX_LENGTH) {
Expand All @@ -98,6 +99,7 @@ class MentionSpan(
enum class Type {
USER,
ROOM,
EVERYONE,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@
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

Check warning on line 105 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/model/MarkdownTextEditorState.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L105 was not covered by tests
}
}
}
Expand All @@ -114,14 +117,9 @@
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

Check warning on line 122 in libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/model/MarkdownTextEditorState.kt

View check run for this annotation

Codecov / codecov/patch

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

Added line #L122 was not covered by tests
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading