Browse Source

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

* Fix `@room` mentions crashing in debug builds

* Iterate on previous solution, add `MentionSpan.Type.EVERYONE`
pull/3108/head
Jorge Martin Espinosa 3 months ago committed by GitHub
parent
commit
d6b259b85a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/event/TimelineItemTextView.kt
  2. 2
      libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/mentions/MentionSpan.kt
  3. 2
      libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/mentions/MentionSpanProvider.kt
  4. 22
      libraries/textcomposer/impl/src/main/kotlin/io/element/android/libraries/textcomposer/model/MarkdownTextEditorState.kt
  5. 2
      libraries/textcomposer/impl/src/test/kotlin/io/element/android/libraries/textcomposer/impl/model/MarkdownTextEditorStateTest.kt

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

@ -94,8 +94,10 @@ private fun updateMentionSpans(text: CharSequence?, cache: RoomMemberProfilesCac @@ -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
}
}
}

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

@ -87,6 +87,7 @@ class MentionSpan( @@ -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( @@ -98,6 +99,7 @@ class MentionSpan(
enum class Type {
USER,
ROOM,
EVERYONE,
}
}

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

@ -108,7 +108,7 @@ class MentionSpanProvider @AssistedInject constructor( @@ -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,

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

@ -93,13 +93,16 @@ class MarkdownTextEditorState( @@ -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( @@ -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
}
}
}

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

@ -156,7 +156,7 @@ class MarkdownTextEditorStateTest { @@ -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) {

Loading…
Cancel
Save