Merge pull request #1680 from oxen-io/fix-mention-crash

Fix crashes when removing spaces in mention texts
This commit is contained in:
ThomasSession 2024-09-24 13:14:07 +10:00 committed by GitHub
commit 439ec900f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 3 deletions

View File

@ -202,13 +202,17 @@ class MentionViewModel(
val sb = StringBuilder()
var offset = 0
for ((span, range) in spansWithRanges) {
// Add content before the mention span
sb.append(editable, offset, range.first)
// Add content before the mention span. There's a possibility of overlapping spans so we need to
// safe guard the start offset here to not go over our span's start.
val thisMentionStart = range.first
val lastMentionEnd = offset.coerceAtMost(thisMentionStart)
sb.append(editable, lastMentionEnd, thisMentionStart)
// Replace the mention span with "@public key"
sb.append('@').append(span.member.publicKey).append(' ')
offset = range.last + 1
// Safe guard offset to not go over the end of the editable.
offset = (range.last + 1).coerceAtMost(editable.length)
}
// Add the remaining content

View File

@ -179,6 +179,11 @@ class MentionViewModelTest {
// Should have normalised message with selected candidate
assertThat(mentionViewModel.normalizeMessageBody())
.isEqualTo("Hi @pubkey1 ")
// Should have correct normalised message even with the last space deleted
editable.delete(editable.length - 1, editable.length)
assertThat(mentionViewModel.normalizeMessageBody())
.isEqualTo("Hi @pubkey1 ")
}
}
}