Skip to content

Commit

Permalink
Merge pull request #1680 from oxen-io/fix-mention-crash
Browse files Browse the repository at this point in the history
Fix crashes when removing spaces in mention texts
  • Loading branch information
ThomasSession authored Sep 24, 2024
2 parents da83df2 + 7e1a0ad commit 439ec90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
}
}
}

0 comments on commit 439ec90

Please sign in to comment.