From 09a4da11b8a8c2ae02791f217a5285af48b17fbf Mon Sep 17 00:00:00 2001 From: Giovani Gonzalez <78755438+giovasdistillery@users.noreply.github.com> Date: Thu, 30 Nov 2023 16:46:14 -0600 Subject: [PATCH] Updating topic validation (#141) - Applying the same approach as iOS for the validation of topics Co-authored-by: Naomi Plasterer --- .../xmtp/android/library/ConversationTest.kt | 35 ++++++++----------- .../org/xmtp/android/library/Conversations.kt | 12 +------ .../xmtp/android/library/messages/Topic.kt | 13 +++++++ 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt index f6ff1ddea..0513e8ded 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ConversationTest.kt @@ -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 @@ -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)) } } diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index 2cef2630f..15234674d 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -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()) { diff --git a/library/src/main/java/org/xmtp/android/library/messages/Topic.kt b/library/src/main/java/org/xmtp/android/library/messages/Topic.kt index debb175c9..b9d9b279d 100644 --- a/library/src/main/java/org/xmtp/android/library/messages/Topic.kt +++ b/library/src/main/java/org/xmtp/android/library/messages/Topic.kt @@ -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 + } + } }