Skip to content

Commit

Permalink
Updating topic validation (#141)
Browse files Browse the repository at this point in the history
- Applying the same approach as iOS for the validation of topics

Co-authored-by: Naomi Plasterer <[email protected]>
  • Loading branch information
giovasdistillery and nplasterer authored Nov 30, 2023
1 parent 3c6594f commit 09a4da1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -824,18 +824,15 @@ class ConversationTest {
val directMessageV1 = Topic.directMessageV1(validId, "sd").description
val directMessageV2 = Topic.directMessageV2(validId).description
val preferenceList = Topic.preferenceList(validId).description
val conversations = bobClient.conversations

// check if validation of topics accepts all types
assertTrue(
conversations.isValidTopic(privateStore) &&
conversations.isValidTopic(contact) &&
conversations.isValidTopic(userIntro) &&
conversations.isValidTopic(userInvite) &&
conversations.isValidTopic(directMessageV1) &&
conversations.isValidTopic(directMessageV2) &&
conversations.isValidTopic(preferenceList),
)
assertTrue(Topic.isValidTopic(privateStore))
assertTrue(Topic.isValidTopic(contact))
assertTrue(Topic.isValidTopic(userIntro))
assertTrue(Topic.isValidTopic(userInvite))
assertTrue(Topic.isValidTopic(directMessageV1))
assertTrue(Topic.isValidTopic(directMessageV2))
assertTrue(Topic.isValidTopic(preferenceList))
}

@Test
Expand All @@ -852,15 +849,13 @@ class ConversationTest {
val preferenceList = Topic.preferenceList(invalidId).description
val conversations = bobClient.conversations

// check if validation of topics accepts all types
assertFalse(
conversations.isValidTopic(privateStore) &&
conversations.isValidTopic(contact) &&
conversations.isValidTopic(userIntro) &&
conversations.isValidTopic(userInvite) &&
conversations.isValidTopic(directMessageV1) &&
conversations.isValidTopic(directMessageV2) &&
conversations.isValidTopic(preferenceList),
)
// check if validation of topics no accept all types with invalid topic
assertFalse(Topic.isValidTopic(privateStore))
assertFalse(Topic.isValidTopic(contact))
assertFalse(Topic.isValidTopic(userIntro))
assertFalse(Topic.isValidTopic(userInvite))
assertFalse(Topic.isValidTopic(directMessageV1))
assertFalse(Topic.isValidTopic(directMessageV2))
assertFalse(Topic.isValidTopic(preferenceList))
}
}
12 changes: 1 addition & 11 deletions library/src/main/java/org/xmtp/android/library/Conversations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,13 @@ data class Conversations(
}

conversationsByTopic += newConversations.filter {
it.peerAddress != client.address && isValidTopic(it.topic)
it.peerAddress != client.address && Topic.isValidTopic(it.topic)
}.map { Pair(it.topic, it) }

// TODO(perf): use DB to persist + sort
return conversationsByTopic.values.sortedByDescending { it.createdAt }
}

fun isValidTopic(topic: String): Boolean {
val regex = Regex("^[\\x00-\\x7F]+$")
val index = topic.indexOf("0/")
if (index != -1) {
val unwrappedTopic = topic.substring(index + 2, topic.lastIndexOf("/proto"))
return unwrappedTopic.matches(regex)
}
return false
}

fun importTopicData(data: TopicData): Conversation {
val conversation: Conversation
if (!data.hasInvitation()) {
Expand Down
13 changes: 13 additions & 0 deletions library/src/main/java/org/xmtp/android/library/messages/Topic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ sealed class Topic {
addresses.sort()
wrap("dm-${addresses.joinToString(separator = "-")}")
}

is directMessageV2 -> wrap("m-$addresses")
is preferenceList -> wrap("pppp-$identifier")
}
}

private fun wrap(value: String): String = "/xmtp/0/$value/proto"

companion object {
fun isValidTopic(topic: String): Boolean {
val regex = Regex("^[\\x00-\\x7F]+$") // Use this regex to filter non ASCII chars
val index = topic.indexOf("0/")
if (index != -1) {
val unwrappedTopic = topic.substring(index + 2, topic.lastIndexOf("/proto"))
return unwrappedTopic.matches(regex)
}
return false
}
}
}

0 comments on commit 09a4da1

Please sign in to comment.