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
mentionSpan.text = displayName mentionSpan.text = displayName
} }
} }
// There's no need to do anything for `@room` pills
MentionSpan.Type.EVERYONE -> Unit
// Nothing yet for room mentions // 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(
append("#") append("#")
} }
} }
Type.EVERYONE -> Unit
} }
append(mentionText.substring(0, min(mentionText.length, MAX_LENGTH))) append(mentionText.substring(0, min(mentionText.length, MAX_LENGTH)))
if (mentionText.length > MAX_LENGTH) { if (mentionText.length > MAX_LENGTH) {
@ -98,6 +99,7 @@ class MentionSpan(
enum class Type { enum class Type {
USER, USER,
ROOM, 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(
MentionSpan( MentionSpan(
text = text, text = text,
rawValue = "@room", rawValue = "@room",
type = MentionSpan.Type.USER, type = MentionSpan.Type.EVERYONE,
backgroundColor = otherBackgroundColor, backgroundColor = otherBackgroundColor,
textColor = otherTextColor, textColor = otherTextColor,
startPadding = startPaddingPx, startPadding = startPaddingPx,

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

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

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

@ -156,7 +156,7 @@ class MarkdownTextEditorStateTest {
private fun aMarkdownTextWithMentions(): CharSequence { private fun aMarkdownTextWithMentions(): CharSequence {
val userMentionSpan = MentionSpan("@Alice", "@alice:matrix.org", MentionSpan.Type.USER, 0, 0, 0, 0) 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 { return buildSpannedString {
append("Hello ") append("Hello ")
inSpans(userMentionSpan) { inSpans(userMentionSpan) {

Loading…
Cancel
Save